addplot3 [area plot] filling the wrong area

1,240

Solution 1

You will need to cycle the path by hand, which is definitely not the best solution, but it works. So I'm doing the following:

\addplot3[area plot] table[x index=0,y expr=#1,z index=#1] {\datatablePX}
-- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},#1,\pgfkeysvalueof{/pgfplots/zmin})
-- (axis cs:\pgfkeysvalueof{/pgfplots/xmin},#1,\pgfkeysvalueof{/pgfplots/zmin})
-- cycle;

This will look horribly with enlargelimits=true, so it is best to set your limits by hand or using enlargelimits=false.

If you don't want to use zmin for the lower filling boundary, you can specifiy an own key, e.g. zcycle and use the values assigned to this key. See the MWE for details.

Implementation

I also tweaked your code a little, to have a little more speaking axis labels. Use pX.txt, uploaded by OP. I didn't plot all values for better performance.

\documentclass[tikz]{standalone}
\usepackage{pgfplots,pgfplotstable}
\tikzset{
    /pgfplots/zcycle/.initial=,
}
\pgfplotstableread{pX.txt}\datatablePX%          
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
            zmin=0.95*10^5,
        zcycle=10^5,
            area plot/.style={
                fill opacity=0.75,
                draw=black!50!black,
                fill=blue!50,
                mark=none,
            },
            xlabel={Distance $x$ [m]},
            ylabel={Time $t$ [s]},
            zlabel={Pressure $p$ [bar]}
        ]
        \pgfplotsinvokeforeach{120,80,...,1}{
            \addplot3[area plot] table[x index=0,y expr=#1,z index=#1] {\datatablePX}
            -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},#1,\pgfkeysvalueof{/pgfplots/zcycle})
            -- (axis cs:\pgfkeysvalueof{/pgfplots/xmin},#1,\pgfkeysvalueof{/pgfplots/zcycle})
            -- cycle;
        }
    \end{axis}
\end{tikzpicture}
\end{document}

Output

enter image description here

Solution 2

Your intended use-case requires modifications to the input data.

In order to find a "suitable" modification, you need to understand what is going on here. Adding "fill" to the option list means that the path will be closed and the closed segment will be filled. Closing a path means to connect to the most recent "move to" path segment, where "move to" means "the starting point of the current segment" and "connect" means to connect with a straight line. This is what you see: for each segment, the last point is connected with the first one in order to close the path.

Thus, you can add additional coordinates to each scanline of your data - lines which ensure that the "last point" is equal to the first point of the segment.

For two--dimensional plots, pgfplots has the same requirement and it satifies it using the \closedcycle path instruction (compare the manual in section Area Plots). For three-dimensional plots you would need to do this on your own in the input files.

Any automated solution would be a feature request.

Share:
1,240

Related videos on Youtube

user2536125
Author by

user2536125

Updated on April 15, 2020

Comments

  • user2536125
    user2536125 over 3 years

    using some saved data, i use (The data file cane be download from pX.txt where the first column is the x and at each iteration, I plot the other columns with respect to the first column)

        \documentclass{article}
    \usepackage{pgfplots,pgfplotstable}
    
    \pgfplotstableread{pX.txt}\datatablePX%          
    
    \begin{document}
    
    
    \begin{tikzpicture}
    \begin{axis}[
        zmin=10^5,
        area plot/.style={
            fill opacity=0.75,
            draw=black!50!black,
            fill=blue!50,
            mark=none,
        },
        xlabel={$x[m]$},ylabel={Time},zlabel={p[bar]}
    ]
    
    \pgfplotsinvokeforeach{120,119,...,1}{
      \addplot3 [area plot]  table [x index=0, y expr=#1, z index=#1] {\datatablePX};
    }
    
    
    \end{axis}
    \end{tikzpicture}
    
    \end{document}
    

    to plot some data in 3d, where I want the area under the curves to be filled. However, the wrong area is being filled as one can notice in the following plot.

    enter image description here

    It is filling the area to a line plot between the start and the end values. Why doesn't it simply fill with respect to the x-axis?

    • Henri Menke
      Henri Menke over 9 years
      Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.
    • Henri Menke
      Henri Menke over 9 years
      Still not compilable, due to lack of pX.txt.
    • user2536125
      user2536125 over 9 years
      @HenriMenke , I know, but the data file is very large and I cannot trancate. My question is if there is any extra argument to addplot3[area plot] whci will force it to fill to a given axis
    • Henri Menke
      Henri Menke over 9 years
      Use GitHub Gist to upload huge text files and put the link here, like in [`pX.txt`](https://gist.github.com/anonymous/9035028). My approach would be to try to cycle the path manually be using rel axis cs.
    • user2536125
      user2536125 over 9 years
      @HenriMenke , done. But if you can provide a simple example on how to use the 'rel axis cs' , that would be very helpfull, as I can't find it in the doc
    • alfC
      alfC over 9 years
      This has been asked and solved before: tex.stackexchange.com/a/84875/1871, the trick is to close the cycle manually (adding a couple of base points explicitly)
  • Henri Menke
    Henri Menke over 9 years
    I cycled the path "semiautomated". For a limited use case, see my answer.
  • user2536125
    user2536125 over 9 years
    your method is working well for this data. What if I would like to enter the z value manually? I have just changed \pgfkeysvalueof{/pgfplots/zmin} by MyValue , but he is not considering it at all!
  • Henri Menke
    Henri Menke over 9 years
    @user2536125 See my updated answer.