8

I have a rather standard tikz plot. To clarify that low values on the y axis are actually faster/better I want to draw an additional arrow outside of the actually plotting area as shown in the attachment. I found various ways of drawing using \draw but they all operate only within the content area of the plot - how can I draw outside of this area?

Here is a MWE of how I create my plot at the moment:

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

\begin{document}
\begin{figure}
\begin{tikzpicture}[scale=0.75]
         \pgfplotsset{grid style={dashed,gray}}
        \begin{axis}[
        xlabel={X}, 
        ylabel=time,
        xmin=0.8,
        xmax=1.0,
        ymin=1,     
        ymax=200,   
        xmajorgrids=true,
        ymajorgrids=true,
    ]      

\addplot+[
            black,
            mark options={fill= black},
            only marks,
            mark size=2,
            mark=square*,
        ] 
        coordinates {
            (0.805, 10)
            (0.85, 20)

        };



    \end{axis}
    \end{tikzpicture}   

\end{figure}

\end{document}

enter image description here

  • 2
    Are you using pgfplots or is this drawn in plain tikz? Please provide a minimum working example with some data to create the plot. – pschulz Jan 27 '17 at 9:17
  • 2
    Standard TikZ plot, or standard pgfplots plot? (I.e. an axis environment.) If the latter, add clip=false to the axis options, or draw the arrow outside the axis environment. For that method, it might be useful to add e.g. name=myaxis to the axis options, so you can access the anchors like myaxis.outer north west. – Torbjørn T. Jan 27 '17 at 9:17
  • I added a MWE of how I create the plot at the moment – toobee Jan 27 '17 at 9:51
  • 2
    @TorbjørnT. Also clip mode=individual is very handy (I have it on by default in my templates). This will clip graphs but not the added things (nodes, paths etc.) – Rmano Jan 27 '17 at 11:24
  • @Rmano Yes, I'm aware of that, but neglected to mention it in my comment.. – Torbjørn T. Jan 27 '17 at 11:40
13

We name the axis, so as to be able to access it outside the axis environment.

Then, we draw a line between some key points of it.

The output

enter image description here

The code

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows}

\begin{document}
\begin{tikzpicture}[scale=0.75, >=stealth']
  \pgfplotsset{grid style={dashed,gray}}
  \begin{axis}
    [
      name=myGraph,
      xlabel={X}, 
      ylabel=time,
      xmin=0.8,
      xmax=1.0,
      ymin=1,     
      ymax=200,   
      xmajorgrids=true,
      ymajorgrids=true,
    ]      

    \addplot+
    [
      black,
      mark options={fill= black},
      only marks,
      mark size=2,
      mark=square*,
    ] 
    coordinates 
    {
      (0.805, 10)
      (0.85, 20)
    };
  \end{axis}

  \def\myShift{-2cm}
  \draw [red, very thick, ->] ([xshift=\myShift]myGraph.north west) -- ([xshift=\myShift]myGraph.south west) node [midway, rotate=90, fill=white, yshift=2pt] {faster} ;
  %\draw [red, very thick, ->] (myGraph.left of north west) -- (myGraph.left of south west) node [midway, rotate=90, fill=white, yshift=2pt] {faster} ; % an alternative way
\end{tikzpicture}
\end{document}

Cheers,

  • 1
    I prefer your commented alternative solution, because this has the advantage that one doesn't have to adjust a "one time set" xshift in case the (horizontal) size of an x label or the size of the yticklabels changes. – Stefan Pinnow Jan 27 '17 at 11:52
  • Thank you for the good advice. Should I edit the answer ? – marsupilam Jan 27 '17 at 12:09
  • That's up to you. It is your answer. Both solutions are valid and could be better depending on the situation. (Every solution has its pros and cons.) I (personally) just think that the commented answer is the one that more often would be "easier" compared to the non-commented one ;) – Stefan Pinnow Jan 27 '17 at 12:15
12

EDIT: Changed rel axis cs to axis description cs as suggested in the comments.

You can use the axis description cs coordinate system for stuff like this:

\documentclass{article}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}
        [
            grid style = {dashed, gray},
            xlabel={X}, 
            ylabel=time,
            xmin=0.8,
            xmax=1.0,
            ymin=1,     
            ymax=200,   
            xmajorgrids=true,
            ymajorgrids=true,
            clip = false % <--- Important
        ]      
        \addplot+
        [
            black,
            mark options={fill= black},
            only marks,
            mark size=2,
            mark=square*,
        ] 
        coordinates { (0.805, 10) (0.85, 20) };
        % add annotation here:
        \draw[red, ->] ([xshift = -2cm] axis description cs:0, 1) -- node[left] { Faster } ([xshift = -2cm] axis description cs:0, 0);
    \end{axis}
\end{tikzpicture}   
\end{document}

Picture of answer The point (axis description cs:0, 1) is the upper left point, (axis description cs:0, 0) is the lower left point. Both points are shifted by 2cm (arbitrary, choose for your taste). Note that you have to turn off clipping in the axis environment, otherwise all stuff drawn outside the axis will be not visible.

I always have the feeling, things like this can be solved better. In my opinion, you should always choose your units and things you want to show to reflect your point. In this case, maybe it would be possible to use the inverse of time (perhaps frequency) to plot the data? I don't know what you are showing, but keep in mind that it's better (in my opinion) to solve things not by clever technical/texnical tricks (adding a custom annotation) than by using the right visualization method.

  • Good point about frequency vs. (response ?) time. One could also add a title stating "lower time is faster". – marsupilam Jan 27 '17 at 10:20
  • 1
    I would prefer to use axis description cs instead of rel axis cs, because it is independent of axis reversals or skewed axes. To show what I mean, add x dir=reverse, to the axis options and compare both results ... – Stefan Pinnow Jan 27 '17 at 12:22
  • Ah, interesting. I was always wondering about the difference of the two. – pschulz Jan 27 '17 at 15:55
  • 1
    You should also change the description below the figure ... – Stefan Pinnow Jan 27 '17 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.