I have a parabola and 4 lines. What I need to do is fill the outer area delimited by the parabola and these lines. Here is what I currently have
I would like the orange fill to be only in the rectangle delimited by the segments AH
and AB
.
I have tried clipping the fill but to no luck. I am using PGFplots. Here is my current relevant code
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepgfplotslibrary{fillbetween}
% axis style, ticks, etc
\pgfplotsset{every axis/.append style={
axis x line=middle, % put the x axis in the middle
axis y line=middle, % put the y axis in the middle
axis line style={<->}, % arrows on the axis
xlabel={$x$}, % default put x on x-axis
ylabel={$y$}, % default put y on y-axis
axis equal, % 1:1 ratio
grid=both, % coordinate grid
grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!50},
ticks=both, % ticks for integers
minor tick num=5, % number of subticks
ticklabel style={font=\small,fill=white},
xlabel style={at={(ticklabel* cs:1)},anchor=north west},
ylabel style={at={(ticklabel* cs:1)},anchor=south west},
}
}
\tikzset{>=stealth}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[xmin=-1.5,ymin=-1,xmax=2.5,ymax=6]
\coordinate (O) at (0,0);
\node[fill=white,circle,inner sep=0pt] (O-label) at ($(O)+(-35:10pt)$) {$O$};
\coordinate (A) at (-1.12,1.24);
\LabelPoint[mark=*][above right]{-1.09}{1.18}{$A$}
\coordinate (B) at (-1.4,1.96);
\LabelPoint[mark=*][above right]{1.4}{1.96}{$B$}
\coordinate (H) at (-0.63,-0.21);
\LabelPoint[mark=*][below left]{-0.63}{-0.21}{$H$}
\addplot[name path = f,red,thick,samples=500] {x^2};
\addplot[name path = l1,thick] {1/3*x+1.5};
\addplot[name path = l2,thick] {1/3*x};
\addplot[name path = p1,thick,green] {-3*x-2.1};
\addplot[name path = p2,thick,green] {-3*x+6.2};
\addplot [
thick,
color=blue,
fill=none,
fill opacity=0.5
]
fill between[
of=f and l1,
split,
every segment no 1/.style={
fill=blue,
},
];
\clip (A) -- (B) -- (1.86,0.62) -- (H) -- cycle;
\clip[domain=-1.5:2] plot (\x,{\x^2}) -- (2,0) -- (-1.5,0) -- cycle;
\addplot[
thick,
color=orange,
fill=orange,
fill opacity=0.5
]
fill between[
of=f and l2,
soft clip={domain=-1.12:1.86}
];
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
(\LabelPoint
does just a mark at the given coordinate and puts a node with some text).
Both with and without clipping, the result remains unchanged. I'm still quite new to PGF I previously had a solution with only Tikz but looked very bad and was curious to try out PGF for graphs.
Is there a general way of dealing with filling areas between more than 2 plots? Is that general case applicable here?
Thanks for any help.
Best Answer
So this is what you are searching for. For details please have a look at the comments in the code. The trick is to draw two orange fillings, i.e. one for the left part and one for the right part.
To do so I use the
intersection segments
feature (which is roughly the TikZ equivalent of PGFPlotsfill between
). Thensequence
allows us to draw an arbitrary path of the both paths given toof
. In short:L
andR
stand for "left" and "right" which correspond to the paths given toof
. The numbers refer to the path elements.1
means "from the start to the first intersection point",2
means "from the first intersection point to the second intersection point", etc.(I have added some commented "debugging" code at the end of the code with some descriptions. Try it once and you will quite easily find out how it works. Unfortunately sometimes there is some "magic" left, as also is in this example.)