1

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

\documentclass[12pt,a4paper]{article}
\usepackage{tikz, pgfplots}
\pgfplotsset{compat=newest}

\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.

Alx
  • 737
  • You can access the min/max values of the axis with e.g. \pgfkeysvalueof{/pgfplots/xmin}, see https://tex.stackexchange.com/questions/22018/how-to-access-xmin-xmax-ymin-ymax-from-within-pgfplots-axis-environment/22021#22021 – 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
  • 1
    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
  • Seems to me that clip=true,xmin=1 would do that, but perhaps I misunderstand what you're saying. – Torbjørn T. Aug 31 '17 at 13:34
  • As already stated in my answer, your question is a bit unclear. Is there any need to draw outside the axis rectangle? If not, then Torbjørn is totally right: Just use clip=true (or just omit this key, because it is true by default). – Stefan Pinnow Aug 31 '17 at 13:36
  • Yes, thanks, \pgfkeysvalueof{/pgfplots/ymax} really works! I can use it as (0, \pgfkeysvalueof{/pgfplots/ymax}*1.1) etc. – Alx Aug 31 '17 at 13:37
  • Yes, I need to draw outside axis and clip only in x. Unfortunately, clip=true erases in both coordinates, and I want y direction remain unclipped: clip only width of rectangle, not its height. – Alx Aug 31 '17 at 13:41
  • Right, I see. What about having clip=false,xmin=1 and drawing from \pgfkeysvalueof{/pgfplots/xmin} then? (Or, see Stefan's answer.) – Torbjørn T. Aug 31 '17 at 13:48
  • OK, Stefan's approach with scope and clip works. But another small thing: how to set desired width of rectangle when using \pgfkeysvalueof? \draw (...) rectangle + (2, \pgfkeysvalueof{/pgfplots/ymax}) gives odd output ... – Alx Aug 31 '17 at 14:00
  • For the question in you last comment have a look at https://tex.stackexchange.com/a/348822. – Stefan Pinnow Aug 31 '17 at 14:04
  • Or even better: https://tex.stackexchange.com/a/358706. (And have a look who has asked that question :)) – Stefan Pinnow Aug 31 '17 at 14:06
  • Yes, right! I'm so stupid ... how could I forget axis direction cs: in my own question. I'm so sorry. – Alx Aug 31 '17 at 14:15
  • *g, not a problem. – Stefan Pinnow Aug 31 '17 at 14:20

1 Answers1

4

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,
    ]
        \addplot gnuplot [
            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,
        ]{
            % added plot limits here
            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}

image showing the result of above code

Stefan Pinnow
  • 29,535