5

I'm trying to fill the area between my data and the x-axis using pgfplots and its library fillbetween. My two attempts are shown here:

\documentclass[a4paper,11pt]{article}

\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{width=10cm,compat=1.13}

\pagestyle{empty}
\begin{document}

\vspace{1cm}

Example using x-axis \\ \\
\begin{tikzpicture}
    \begin{axis}[xlabel=A, ylabel=B]
        % data
        \addplot[name path=D] table [y=B, x=A]{dataB.csv};
        % x axis
        \path[name path=X]
        (\pgfkeysvalueof{/pgfplots/xmin},0) --
        (\pgfkeysvalueof{/pgfplots/xmax},0);
        % fill between
        \addplot[gray!20] fill between[of=D and X];
    \end{axis}
\end{tikzpicture}

Example using zero function \\ \\
\begin{tikzpicture}
    \begin{axis}[xlabel=A, ylabel=B]
        % data
        \addplot[name path=D] table [y=B, x=A]{dataB.csv};
        % zero function
        \addplot[name path=N,domain=0:1]{0};
        % fill between
        \addplot[gray!20] fill between[of=D and N];
    \end{axis}
\end{tikzpicture}

\end{document}

The attempts only differ in the way the x-axis is drawn. However, the results look off:

Latex Output

It looks like the x-axis is twisted (left and right are wrong). Does anyone have an idea how this can be resolved? Thanks in advance!

I reckon it could have to do with my data, so here it is: http://pastebin.com/raw/JGKmbtZ5

Edit1: It seems to have to do with the data not forming a proper mathematical function, i.e. one x-value is assigned two (or more) y-values. See the following example:

\documentclass[a4paper,11pt]{article}

\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{width=10cm,compat=1.13}

\pagestyle{empty}
\begin{document}

Not a mathematical function \\ \\
\begin{tikzpicture}
    \begin{axis}[xlabel=A, ylabel=B]
        % data
        \addplot[name path=D] coordinates {(0,1) (0,0.5) (1,2)};%table [y=B, x=A]{dataB.csv};
        % zero function
        \addplot[draw=none, name path=N,domain=0:1]{0};
        % fill between
        \addplot[gray!20] fill between[of=D and N];
    \end{axis}
\end{tikzpicture}

\end{document}

Latex Output

Edit2: The problem only seems to occur when the data starts with a straight line down. When the (mathematically) undefined data point appears in between start and end points, fillbetween works as expected:

\addplot[name path=D] coordinates {(0,1) (1,1) (0.5,0.5) (2,1)};

Latex Output

I guess I'll have to insert an artificial data point to avoid this problem at the beginning of the graph.

2
  • 1
    This is actually a bug / missing support in pgfplots. I filed sourceforge.net/p/pgfplots/bugs/195 and will look into it. A work around is to add reverse=true to the option list after fill between. Nov 14 '17 at 22:09
  • Another workaround is to find a different line to fill between against. E.g. when I expected the filled area to be roughly triangular ◹ and using the vertical straight side gave me the X-shape, I used the horizontal straight side instead and fill between got it right. Jan 28 at 10:47
1

Edit: From new MWE is seems tha nature of data influence on function fill between. It seems that it expect that series of data don't have two different values of path D at the same A. For example, if in given MWE you change path D to

\addplot[name path=D] coordinates {(0,1) (0.000001,0.5) (1,2)};

You will get expected result:

enter image description here

With other words, I cant help you and will remove my answer after while.

6
  • The data is in the pastebin link ;-)
    – user31729
    Jul 13 '16 at 21:07
  • huh, I overlooked this link ... however used simple replacement of them was simpler to me :-). I will correct my answer.
    – Zarko
    Jul 13 '16 at 21:10
  • Thanks for your reply, @Zarko. Unfortunately, the problem remains when plotting with the data. My guess is that it has to do with the data. My data don't form a proper mathematical function. I.e., there are values which get assigned twice (when the graph drops straight down, e.g. at around 0 and 0.8).
    – chris
    Jul 13 '16 at 21:44
  • Than please integrate your data to your MWE. I'm very sorry but haven't rnough time do this instead of you.
    – Zarko
    Jul 13 '16 at 21:47
  • just edited my question with a simpler example ;)
    – chris
    Jul 13 '16 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.