# Using symbolically the coordinates of an intersection point between curves for filling the area below a rectangle

I would like to fill the area under the rectangles that have the origin and the working point (Q_1, H_1) and (Q_2, H_2) respectively obtained as intersections between the two red curves with the blue one. I managed to pinpoint the intersection point and to draw the segments defining the rectangle, and then I tried to use fill between but the output was coloured trapezoid between the segment and the whole horizontal axis. I started thinking the missing element is the right end-point of the interval over which the horizontal axis path is defined (which has to be Q_1 and not xmax) so I looked for a solution to this problem, without any relevant success. I am not very familiar with this package (that is the reason why maybe this question is almost a duplicate but I cannot determine it) so I have not been able to use properly let or pgfextractx functions for this purpose. How can I reference symbolically the abscissa of the intersection point (OP1) and use it for defining properly the useful interval for fill between?

Here follows my attempt:

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usetikzlibrary{arrows.meta,babel,calc,intersections}
\usepgfplotslibrary{fillbetween}

\begin{figure}[H] %% figure is closed
\begin{center}
\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=1, ymin=0, ymax=1.5,
axis x line=middle,
axis y line=middle,
xlabel=$Q$,ylabel=$H$,
ticks=none,
]

\coordinate [label= ${(Q_1,H_1)}$ ] (OP1) at (intersection-1);
\coordinate [label= ${(Q_2,H_2)}$ ] (OP2) at (intersection-1);

\draw[name path=opv1] (OP1) -- (OP1|-0,0);
\draw[name path=oph1] (OP1) -- (0,0 |- OP1);
\draw[name path=opv2] (OP2) -- (OP2|-0,0);
\draw[name path=oph2] (OP2) -- (0,0 |- OP2);

\path[name path=zero]
(\pgfkeysvalueof{/pgfplots/xmin},0) --
(\pgfkeysvalueof{/pgfplots/xmax},0);

\end{axis}

\foreach \point in {OP1,OP2}
\fill [red] (\point) circle (2pt);

\end{tikzpicture}
\end{center}
\caption{System working point}
\end{figure}


• @gernot I was already rather aware that my code lacked something and that the output had to be necessarily a trapezoidal shape. I wanted to understand how to trim suitably the fill between command. – Vexx23 Apr 20 '17 at 22:43

So if all you need is two rectangles, then fill between is kind of an overcomplication, you can just use \fill (x,y) rectangle (u,v);. You already have the coordinates. Below I also added the backgrounds library, and placed the rectangles inside a scope environment with [on background layer], so that the rectangles are placed behind the plot lines.

I used \filldraw, but if you don't want the border drawn, change that to \fill, and remove the draw=black option.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usetikzlibrary{arrows.meta,babel,calc,intersections,backgrounds}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=1, ymin=0, ymax=1.5,
axis x line=middle,
axis y line=middle,
xlabel=$Q$,ylabel=$H$,
ticks=none,
]

\coordinate [label= ${(Q_1,H_1)}$ ] (OP1) at (intersection-1);
\coordinate [label= ${(Q_2,H_2)}$ ] (OP2) at (intersection-1);

\begin{scope}[on background layer]
\filldraw[fill=orange!50,draw=black] (0,0) rectangle node {foo} (OP2);
\filldraw[fill=blue!80!red!50!white,draw=black] (0,0-|OP2) rectangle node {bar} (OP1);
\end{scope}
\end{axis}

\foreach \point in {OP1,OP2}
\fill [red] (\point) circle (2pt);

\end{tikzpicture}
\end{document}

• Yes, this is exactly what I was looking for. I had not even imagined to use rectangles directly, it is a very simple solution. – Vexx23 Apr 20 '17 at 22:31
• @Vexx23, if you find the answers useful you could think of up-voting them and besides that accept the most useful answer (by clicking on the checkmark ✓). – Stefan Pinnow Apr 20 '17 at 23:09
• @StefanPinnow Can't I accept both your answers (since they provide elegant solutions using different approaches)? – Vexx23 Apr 21 '17 at 14:31
• @TorbjørnT. Is there a simple method for placing a text inside the rectangle? – Vexx23 Apr 21 '17 at 15:05
• @Vexx23 Several, for some text in the middle of it, add node {text} right after rectangle, see updated answer. – Torbjørn T. Apr 21 '17 at 15:08

As Torbjørn T. and Gernot already stated, I am also not 100% sure what you really want to achieve. But I guess the answer of Torbjørn T. is pretty much what you are searching for.

So this is pretty much the same answer, but using the fill between features.

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{
intersections,
pgfplots.fillbetween,
}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=1, ymin=0, ymax=1.5,
axis x line=middle,
axis y line=middle,
xlabel=$Q$,ylabel=$H$,
ticks=none,
]

\coordinate [label= ${(Q_1,H_1)}$ ] (OP1) at (intersection-1);
\coordinate [label= ${(Q_2,H_2)}$ ] (OP2) at (intersection-1);

\draw [name path=opv1] (OP1) -- (OP1 |- 0,0);
\draw [name path=oph1] (OP1) -- (0,0 |- OP1);
\draw [name path=opv2] (OP2) -- (OP2 |- 0,0);
\draw [name path=oph2] (OP2) -- (0,0 |- OP2);

\path [name path=zero]
(\pgfkeysvalueof{/pgfplots/xmin},0) --
(\pgfkeysvalueof{/pgfplots/xmax},0);

of=oph1 and zero,
% -----------------------------------------------------------------
% in order to achieve the desired result you can add a soft clip'
% path which cuts off the unwanted rest not inside of this soft
% clip' path and you need (in this case) also add reverse=true'
% explicitly, otherwise you get another unwanted result
reverse=true,
soft clip={
% (depending on what you exactly need use one the following
% starting corrdinates)
%                (OP2 |- 0,\pgfkeysvalueof{/pgfplots/ymin})
(\pgfkeysvalueof{/pgfplots/xmin},\pgfkeysvalueof{/pgfplots/ymin})
rectangle
(OP1)
},
% -----------------------------------------------------------------
];

% add some text centered in the rectangle (as requested in the comments)
\path
%            % (to show how it is drawn ...)
%            [draw,dashed]
(\pgfkeysvalueof{/pgfplots/xmin},\pgfkeysvalueof{/pgfplots/ymin})
-- node [pos=0.5] {some text} (OP1);
;

\end{axis}

\foreach \point in {OP1,OP2} {
\fill [red] (\point) circle (2pt);
}

\end{tikzpicture}
\end{document}


• The commented starting coordinate is the right one, the desired area to fill is the one of the rectangle from (0,0) to (Q1,H1) . Your solution is extremely clear and explanatory, thank you. – Vexx23 Apr 20 '17 at 22:38
• Is it possible to add some text centred inside the 'fill between' area? – Vexx23 Apr 21 '17 at 14:44
• @Vexx23, sure. See my edited answer. – Stefan Pinnow Apr 21 '17 at 15:01
• Thank you, so a new stealth line is built after 'fill between' scope. Can I ask you which is the role of [pos=0.5]? It seems it force the node to be positioned at 0.5*length of the path, is it so? – Vexx23 Apr 21 '17 at 15:12
• @Vexx23, yes, [pos=0.5] is the default value and thus can be omitted. But if not someone wants to position the text somewhere else maybe he/she than has an idea -- together with the commented part -- how to achieve it without the need of asking again ... ;) And regarding the stealth line: Yes, you have to do something like this when you are using the fill between feature to fill the area, because there you cannot (to my knowledge) simply add a trailing path. – Stefan Pinnow Apr 21 '17 at 15:25

A PSTricks solution using the pst-plot package:

\documentclass{article}

\usepackage{pst-plot}
\usepackage{xfp}

% The blue-coloured graph.
\def\aA{-0.5}
\def\bA{0}
\def\cA{1}
\def\fA(#1){(\aA*(#1)^2+\bA*(#1)+\cA)}
% The "first" red-coloured graph.
\def\aB{2}
\def\bB{1.6}
\def\cB{0.5}
\def\fB(#1){(\aB*(#1)^2+\bB*(#1)+\cB)}
% The "second" red-coloured graph.
\def\aC{0.5}
\def\bC{0.4}
\def\cC{0.5}
\def\fC(#1){(\aC*(#1)^2+\bC*(#1)+\cC)}
% Intersection points, x-coordinates.
\def\xAB{\fpeval{(-(\bA-\bB)-sqrt((\bA-\bB)^2-4*(\aA-\aB)*(\cA-\cB)))/(2*(\aA-\aB))}}
\def\xAC{\fpeval{(-(\bA-\bC)-sqrt((\bA-\bC)^2-4*(\aA-\aC)*(\cA-\cC)))/(2*(\aA-\aC))}}

\begin{document}

{\psset{
xunit = 6,
yunit = 3,
dimen = m,
algebraic
}
\begin{pspicture}(1.2,1.5)
{\psset{fillstyle = solid}
\psframe[
fillcolor = orange!60
](0,0)(\xAB,\fpeval{\fA(\xAB)})
\rput(\fpeval{0.5*\xAB},\fpeval{0.5*\fA(\xAB)}){cat}
\psframe[
fillcolor = blue!60
](\xAB,0)(\xAC,\fpeval{\fA(\xAC)})
\rput(\fpeval{0.5*(\xAC+\xAB)},\fpeval{0.5*\fA(\xAC)}){dog}}
\psaxes[
ticks = none,
labels = none
]{->}(0,0)(-0.05,-0.1)(0.8,1.5)[$Q$,120][$H$,330]
\psplot[
linecolor = blue
]{0}{0.8}{\fA(x)}
\psplot[
linecolor = red
]{0}{0.4}{\fB(x)}
\psplot[
linecolor = red
]{0}{0.8}{\fC(x)}
\psdots[
dotstyle = o,
fillcolor =red
](\xAB,\fpeval{\fA(\xAB)})(\xAC,\fpeval{\fA(\xAC)})
\end{pspicture}}

\end{document}
`

All you have to do is change the values of the coefficients of the three second degree polynomials and the drawing will be adjusted accordingly. (You have to change the plot ranges and axes ranges manually, if needed.)