# Aligning stacked function plot and another tikzpicture

I have a function plotted pgfplots, but I need to stack or overlay a bar below and parallel to x-axis, from origin the first zero of the function. The problem I have is that I can't explicitly set the axis length (not the plot width) of pgfplot. The relevant code I have is

\begin{tikzpicture}
% 1.28 is the approximate zero of the black function
\draw[very thick] (0, -1) -- (\textwidth*0.4/1.5*1.28, -1);
\begin{axis}[
ticks=none,
domain=0:1.5,
xmin=0,
xmax=1.5,
ymin=0,
x=\textwidth*0.4/1.5, % I thought this would make it work
axis lines=left,
xlabel={$r$ \color{red}$r^2$},
ylabel=$m$,
y label style={at={(axis description cs:0.15, 0.5)}, anchor=south},
x label style={at={(axis description cs:0.5, 0.1)}},
axis equal,
]
\addplot[mark=none, thick] (x, {1 - x^2 + x/2});
\addplot[mark=none, color=red] (x^2, {1 - x^2 + x/2});
\end{axis}
\end{tikzpicture}


and the result I got was (ignore the circle please)

it's obvious that the two doesn't really have the same length. In fact, if shifted, the result was (This is not the desired diagram, just to show how the two don't align)

In simple words, the effect I want is to have the black bar touch with the black function exactly on that point. I would also like to know if there's any way to not hard code the solution of the function and let tikz determine it.

• Using the intersections Tikz library is the way to go to automatically determine the intersections of the lines. To just draw it because you know the points to connect, you can use \draw[very thick] (axis cs: 0,0) -- (axis cs: 1.28, 0); inside the axis environment and, as you can see, you can refer to the cartesian plane using the axis coordinate system. – Axel Krypton Mar 22 '18 at 22:00

Here is one way: compute the intersection with the x-axis and draw the line then. UPDATE: Accommodated the revised question (I think).

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{intersections,calc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ticks=none,
domain=0:1.5,
xmin=0,
xmax=1.5,
ymin=0,
axis lines=left,
xlabel={$r$ \color{red}$r^2$},
ylabel=$m$,
y label style={at={(axis description cs:0, 0.5)}, anchor=south},
x label style={at={(axis description cs:0.5, 0.1)}},
axis equal,
]
\addplot[mark=none, thick,name path=B] (x, {1 - x^2 + x/2});
\addplot[mark=none, color=red] (x^2, {1 - x^2 + x/2});
\path [name intersections={of=A and B,by={X}}];
\coordinate (O) at (0,-0.1);
\end{axis}
\draw[very thick,blue] (O-|X) circle (1pt);
\draw[very thick,blue] (O) circle (1pt);
\draw[very thick,blue] (O) -- (O-|X) coordinate[midway] (Y);
\draw[blue] ($(Y)+(0,2pt)$) -- ($(Y)-(0,2pt)$);
\end{tikzpicture}
\end{document}


• I vote +1 for your answer. I have not understood the question of the OP. – Sebastiano Mar 22 '18 at 21:58
• @Sebastiano Grazie! I am also not 100% sure but I was under the impression that "In simple words, the effect I want is to have the black bar touch with the black function exactly on that point. " means (s)he wants to draw the line all the way to the intersection of the black curve with the x-axis. – marmot Mar 22 '18 at 22:00
• @marmot I also understood it in this way and you were quicker than me! ;) I do not understand why the label in your plot are inside the plot, though. Completing and compiling the code of the OP, the labels are as shown in his/her picture. – Axel Krypton Mar 22 '18 at 22:10
• @AxelKrypton I was also wondering about this, but how can you compile the code of the OP? You need to add \documentclass and so on, and that's what I did. Anyway, I added an alternative. – marmot Mar 22 '18 at 22:12
• @marmot As I said, completing and compiling the OP's code. I just added \documentclass[border=3mm]{standalone} and \usepackage{tikz, pgfplots} (together with the document environment). I was wondering about the position of your label not because they look bad (even if they do), but because your code is almost identical to the OP one. At least, the x and y label styles are identical (those of your first code). And, then, I do not understand who is the responsible of the shift. Any clue? – Axel Krypton Mar 22 '18 at 22:23

this is rather extended comment than answer... so please don't vote it.

code in emarmot answer can be shorter:

• by considering pgfplots 1.15
• by move common addplot options to axis options
• by considering that default position of y label is the same as it is defined by y label style

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
ticks=none,
domain=0:1.5,
xmin=0,
xmax=1.5,
ymin=0,
axis lines=left,
xlabel={$r$ \color{red}$r^2$},
ylabel=$m$,
x label style={at={(0.5, 0.1)}},
axis equal,
mark=none,
every axis plot post/.append style={very thick}
]
\addplot[name path=B] {1 - x^2 + x/2};
\addplot[color=red] (x^2, {1 - x^2 + x/2});
\path [name intersections={of=A and B,by={X}}];
\end{axis}
\filldraw[very thick,blue] (0,-0.25) circle (1pt) -- (X |- 0,-0.25) circle (1pt);
\end{tikzpicture}
\end{document}

• I have upvoted same :-( +1 – Sebastiano Mar 24 '18 at 20:38
• I believe that those who dedicate their precious time to provide an adequate and comprehensive response must be rewarded. – Sebastiano Mar 24 '18 at 20:47