# How to plot a graph with separate scale reference lines?

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)

-

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{
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)}}
]

@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
@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