# there is a gap when two line pass the same point, at x=0.3. How to eliminate the gap? Many thanks!

code

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}[
declare function = {
func1(\x)= (\x<=0.3) * (10*\x*\x - 3*\x ) + (\x>0.3) * (\x* \x*\x);
func2(\x)= (\x<=0.3) * (0) + (\x>0.3) * (\x* \x*\x);
}
]

\begin{axis}[
ymin=-0.4,
ymax=1,
xmin=0,
xmax=1,
ytick={-0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1},
grid
]

of = poly and line, split,
every odd segment/.style={red!10},
every even segment/.style={yellow!10}
];
\addplot[blue!10] fill between[of = poly2 and linear];
\node at (axis cs:0.18,-0.05) [anchor=north east] {A};
\node at (axis cs:0.45,0.2) [anchor=south west] {B};
\node at (axis cs:0.8,0.3) [anchor=south west] {C};
\end{axis}
\end{tikzpicture}
\end{document}


This is because of the hard bend near x=0.3 and the smooth option and the then "low" amount of samples where the function is evaluated. You could show that by adding markers to the "func" plots as in the given example below. To circumvent your problem you could simply increase the number of samples where the function is evaluated, e.g. to 101. (The default number of samples is 25.)

% used PGFPlots v1.17
\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{
width=10cm,
}
\begin{document}
\begin{tikzpicture}[
declare function = {
func1(\x)= (\x<=0.3) * (10*\x*\x - 3*\x ) + (\x>0.3) * (\x* \x*\x);
func2(\x)= (\x<=0.3) * (0) + (\x>0.3) * (\x* \x*\x);
},
]
\begin{axis}[
ymin=-0.4,
ymax=1,
xmin=0,
xmax=1,
ytick={-0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1},
grid,
domain=-0.4:1,
%            samples=101,   % <-- uncomment me to improve the result
]

name path=poly,
blue,
smooth,
mark=o,
] {func1(x)};
name path=poly2,
red,
smooth,
mark=+,
] {func2(x)};

of=poly and line,
split,
every odd segment/.style={red!10},
every even segment/.style={yellow!10}
];

\addplot [blue!10] fill between [of = poly2 and linear];

\node at (axis cs:0.18,-0.05) [anchor=north east] {A};
\node at (axis cs:0.45,0.2)   [anchor=south west] {B};
\node at (axis cs:0.8,0.3)    [anchor=south west] {C};
\end{axis}
\end{tikzpicture}
\end{document}


• Thank you so much for your timely reply and wise code. After increasing the sample to 100 or even larger, I noticed there is a kink right after x=0.3, although the two lines of function1 and 2 do become closer to each other. So, your solution solved my question asked here. I actually do not need function 2, but I want to fill the area marked by B. Do you mind giving me some advice on how to do this? Sorry for the simple questions. – Haiyan Oct 17 at 0:56
• You are welcome. The kink is from the smooth option. The closer two points get and the harder/steeper a "bend" is between these points the curve tries to keep "smooth". But the more points you have, the less you need the smooth option. If that doesn't help you to solve the question, please ask a follow-up question since that is a completely other question than the one in the title. – Stefan Pinnow Oct 17 at 19:49
• I C. Great thanks for your patience! – Haiyan Oct 18 at 4:03
• If you like my answer and it was helpful, please consider upvoting (by clicking on the arrows next to the score) and/or marking it as the accepted answer (by clicking on the checkmark ✓). – Stefan Pinnow Oct 18 at 5:15
• Thanks! I am quite new here. Not familiar with those buttons. Already done now. – Haiyan Oct 19 at 1:24