Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have searched through different plots generated by pgfplots and could not find my desired style as below: I know how to hide axes, but would like to know does PGFplots have an option to automatically generate scale reference lines (seen at southeast of the image)

enter image description here

share|improve this question

1 Answer 1

up vote 8 down vote accepted

You can use after end axis/.code to execute some drawing code after everything else is done, and the axis direction cs: to define lines in terms of axis units.

Here's a key called scale reference that draws a scale reference like the one you showed. You can customise the lengths of the bars using scale reference x length, the line style and position using scale reference style, and the labels using scale reference x unit.

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}

\begin{document}

\pgfplotsset{
    compat=newest,
    plot coordinates/math parser=false,
    every axis/.append style={
        tick label style={font=\scriptsize},
        label style={font=\scriptsize}
    }
}

\pgfplotsset{
    scale reference style/.style={
        every scale reference/.append style={#1}
    },
    every scale reference/.style={
        shift={(rel axis cs:0,0)},
        thick
    },
    scale reference/x unit/.initial={},
    scale reference/y unit/.initial={},
    scale reference/x length/.initial=1,
    scale reference/y length/.initial=1,
    scale reference/.style={
        /pgfplots/scale reference/.cd,
        #1,
        /pgfplots/after end axis/.code={
            \draw [/pgfplots/every scale reference] (0,0) --
                ++(axis direction cs:\pgfkeysvalueof{/pgfplots/scale reference/x length},0)
                    node [/pgfplots/every tick label, pos=0.5, below] {\pgfkeysvalueof{/pgfplots/scale reference/x length} \pgfkeysvalueof{/pgfplots/scale reference/x unit}}
                -- ++(0,5pt);
            \draw [/pgfplots/every scale reference] (0,0) -- ++(axis direction cs:0,\pgfkeysvalueof{/pgfplots/scale reference/y length}) node [/pgfplots/every tick label, pos=0.5, left, ] {\pgfkeysvalueof{/pgfplots/scale reference/y length}\pgfkeysvalueof{/pgfplots/scale reference/y unit}} -- ++(5pt,0);
        }
    }
}

\begin{tikzpicture}
\begin{axis}[
    domain=0:pi,
    width=10cm, height=4cm,
    enlargelimits=false,
    hide axis,
    scale reference={
        x unit=secs,
        y unit=$\mu$V,
        x length=0.5,
        y length=50
    },
    scale reference style={shift={(rel axis cs:0,-0.1)}}
]
\addplot [samples=500] {50*sin(deg(x*20))*sin(deg(x*20/7*rnd/2))+rnd};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
Thanks Jake! Really helpful. How can I configure this code to automatically use the font setting defined for tick labels and axis labels for scale reference? –  M.Reza Jan 25 '13 at 4:33
1  
@M.Reza: Glad it helps! I've edited the code so the x and y tick label styles are now automatically used for the scale reference labels, and the syntax has been cleaned up a bit (you don't have to say scale reference x unit any more, you can just pass x unit to the scale reference key). –  Jake Jan 25 '13 at 11:44
    
Thanks Jake for the update. I used the `\pgfplotsset{ compat=newest, plot coordinates/math parser=false, every axis/.append style={ tick label style={font=\scriptsize} label style={font=\scriptsize} }' To adjust the global fonts, however it seems not effective on the scale reference fonts. –  M.Reza Jan 28 '13 at 6:20
1  
@M.Reza: I've changed the code so the labels now respect the style set in tick label style. –  Jake Jan 28 '13 at 8:21
    
A small add on to the answer, If you would like to rotate ylabel: change node [/pgfplots/every tick label, pos=0.5, left, ] to node [/pgfplots/every tick label,rotate=90, pos=0.5, above] –  M.Reza Jan 28 '13 at 12:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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