# Mix coordinate systems in PGFPlots

I try to understand coordinate systems of pgfplots, an MWE is as follows:

\documentclass[12pt,a4paper]{article}
\usepackage{tikz, pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[width=\textwidth, height=0.7\textwidth, restrict x to domain = 1:10, xmin=1, xmax=10, clip=false]
\addplot gnuplot[raw gnuplot, color=red, no marks, thick]{set samples 1000; plot x**2*sin(x)};
\filldraw[blue!50] (rel axis cs:-0.1,1.1) rectangle +(axis cs:2,1);
\end{axis}
\end{tikzpicture}
\end{document}


Actually I need to draw some graph (function or table with points) and several primitives (lines, rectangles) above of that graph. Because I don't know in advance maximum y-value I use relative coordinates for primitives. But I also need to use axis cs: coordinates for x in primitives. Is it possible to mix different coordinate systems: \draw (a,b) rectangle (c,d), where a and c are in rel axis cs:, but b and d are in axis cs:?

And the second question: how to clip primitives to same x limits as for graph? In my MWE rectangle starts at some coordinate (say, x=0) and I want it to be clipped at x=1, as the graph. If I use clip=true this will completely erase the rectangle.

• You can access the min/max values of the axis with e.g. \pgfkeysvalueof{/pgfplots/xmin}, see tex.stackexchange.com/questions/22018/… – Torbjørn T. Aug 31 '17 at 13:10
• I don't really understand your last question. Your entire rectangle is outside the axis, so naturally the whole thing disappears with (the default) clip=true. Try \filldraw[blue!50] (rel axis cs:-0.1,1.1) rectangle (axis cs:2,1);, you'll see that the blue rectangle does appear. (You have +(axis cs:2,1) in your code.) – Torbjørn T. Aug 31 '17 at 13:16
• This is not what I need, sorry, or I don't understand how to use that answer. It is said there that xmin, ... etc. should be set explicitly, but I dont know ymax before the graph. So, e.g. I need to draw rectangle from (0,70) to (2,75). How can I do this if I don't know "70" -- value depending on ymax? – Alx Aug 31 '17 at 13:27
• Oh sorry, that part is a bit outdated, it also works if xmin etc. is not set explicitly. \draw (0,\pgfkeysvalueof{/pgfplots/ymax}-5) rectangle (2,\pgfkeysvalueof{/pgfplots/ymax});? – Torbjørn T. Aug 31 '17 at 13:31
• Concerning my second question. I draw somehow rectangle from (0,70) to (2,75), and graph below. And I want to clip this rectangle to be from (1,70) to (2,75). In other words, initially I have graph and rectngle start from x=0, and then I want to clip both to start from x=1. How to do this? – Alx Aug 31 '17 at 13:32

Unfortunately your question isn't very precise so I am a bit guessing what you really want. If this isn't what you are asking for, please edit your question accordingly.

Please have a look at the comments of the code for details.

% used PGFPlots v.1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
% use this compat' level or higher so you don't need to prepend
% TikZ coordinates by axis cs:' because it is the default coordinate
% system then
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=1,
xmax=10,
% use this key--value only, if you really need to draw something
% outside the axis limits
clip=false,
% just in case you cross the axis border, it should be drawn on top
% of everything
axis on top,
]
raw gnuplot,
color=red,
no marks,
thick,
% there is no need for the 1000 samples, the default is fine
% when used together with smooth'
smooth,
]{
plot [1:10] x**2*sin(x);
};

\filldraw [blue!50]
% -----------------------------------------------------------------
% here is a way how you can mix different coordinate systems
({rel axis cs:-0.05,0} |- {axis cs:0,15})
rectangle
% (as written above: when using an appropriate compat' level
%  there is no need to write axis cs:')
({rel axis cs:0.3,0} |- {0,35})
% -----------------------------------------------------------------
;

% ---------------------------------------------------------------------
% use a scope and \clip' to solve problem b
% (of course you can also use the above shown method of mixing
%  coordinate systems here, but in addition you can access the axis
%  limits and use them directly)
\begin{scope}
\clip
(\pgfkeysvalueof{/pgfplots/xmin},\pgfkeysvalueof{/pgfplots/ymin})
rectangle
(rel axis cs:1,1)
;
\fill [green!50] ({rel axis cs:0.1,0} |- {axis cs:0,-50})
circle (50pt);
\end{scope}
% ---------------------------------------------------------------------
\end{axis}
\end{tikzpicture}
\end{document}


• Thank you! I can use your method of scope and clip with some modifications. – Alx Aug 31 '17 at 14:01