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 the following multiple y-axis plot that results in the image on the top image. However, I want to add a sort of legend in each of the Y axis, similar to what I manually drew in the bottom image. Any idea how can I add color tags into the Y axis labels?

enter image description here enter image description here

\pgfplotsset{compat=1.5}
\begin{tikzpicture}

\begin{axis}[%
grid=major,grid style={dashed},
width=3in,
height=3in,
scale only axis,
xmin=2,
xmax=21,
separate axis lines,
every outer y axis line/.append style={black},
every y tick label/.append style={font=\color{black}},
ymin=0,
ymax=0.02,
ytick       ={    0, 0.005,  0.01, 0.015,  0.02},
ylabel      ={Maximun displacement error},
yticklabels ={    0, 0.005,  0.010, 0.015,  0.020},
xtick       ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21},
xticklabels ={0.00,,,,,0.005,,,,,0.010,,,,,0.015,,,,,0.020},
%xtick      ={0,2.5,5,7.5,10,12.5,15,17.5,20,21},
%xticklabels    ={0.00,,0.005,,0.010,,0.015,,0.020,},
]
\addplot [
color=red,
solid,
line width=2.0pt,
forget plot
]
table[row sep=crcr]{
1 0\\
2 0\\
3 0.00199752\\
4 0.00299752\\
5 0.00399893\\
6 0.00499837\\
7 0.00599192\\
8 0.00699623\\
9 0.00799115\\
10 0.00899543\\
11 0.00999383\\
12 0.0109399\\
13 0.0119664\\
14 0.0129645\\
15 0.0139899\\
16 0.0148372\\
17 0.0159921\\
18 0.0164528\\
19 0.0179454\\
20 0.018987\\
21 0.0197936\\
};
\end{axis}

\begin{axis}[%
width=3in,
height=3in,
scale only axis,
xmin=2,
xmax=21,
every outer y axis line/.append style={black},
every y tick label/.append style={font=\color{black}},
ymin=0,
ymax=1,
ytick={   0, 0.25,  0.5, 0.75,    1},
yticklabels ={    0$\%$, 25$\%$,  50$\%$, 75$\%$,  100$\%$},
ylabel={Percentatge of corrected area},
axis x line*=bottom,
axis y line*=right,
xtick={},
xticklabels={},
]
\addplot [
color=green,
solid,
line width=2.0pt,
forget plot
]
table[row sep=crcr]{
1 1\\
2 1\\
3 0.904386951631046\\
4 0.526809148856393\\
5 0.314585676790401\\
6 0.202474690663667\\
7 0.182602174728159\\
8 0.164604424446944\\
9 0.142482189726284\\
10 0.0941132358455193\\
11 0.0761154855643045\\
12 0.0712410948631421\\
13 0.0663667041619797\\
14 0.0626171728533933\\
15 0.0543682039745032\\
16 0.0284964379452568\\
17 0.0273715785526809\\
18 0.0269966254218223\\
19 0.0232470941132358\\
20 0.0213723284589426\\
21 0.0206224221972253\\
};
\end{axis}
\end{tikzpicture}%
share|improve this question
    
When I've done this, if it's really just 1 series per axis, I've changed the text colour of the y axis labels. IMO it looks nicer (though not with that bright green on screen). –  Chris H Sep 10 '13 at 16:03

2 Answers 2

up vote 5 down vote accepted

You can use the \label and \ref functionality that PGFPlots provides for putting legend images in your axis labels: Put \label{<some text>} after each of the \addplot commands, and \ref{<some text>} in the ylabel text. That way, the legend image will stay synchronised with the color and style of the plot lines:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}

\begin{axis}[%
grid=major,grid style={dashed},
width=3in,
height=3in,
scale only axis,
xmin=2,
xmax=21,
separate axis lines,
every outer y axis line/.append style={black},
every y tick label/.append style={font=\color{black}},
ymin=0,
ymax=0.02,
ytick       ={    0, 0.005,  0.01, 0.015,  0.02},
ylabel      ={\ref{displacementplot} Maximum displacement error},
yticklabels ={    0, 0.005,  0.010, 0.015,  0.020},
xtick       ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21},
xticklabels ={0.00,,,,,0.005,,,,,0.010,,,,,0.015,,,,,0.020},
%xtick      ={0,2.5,5,7.5,10,12.5,15,17.5,20,21},
%xticklabels    ={0.00,,0.005,,0.010,,0.015,,0.020,},
]
\addplot [
color=red,
solid,
line width=2.0pt,
]
table[row sep=crcr]{
1 0\\
2 0\\
3 0.00199752\\
4 0.00299752\\
5 0.00399893\\
6 0.00499837\\
7 0.00599192\\
8 0.00699623\\
9 0.00799115\\
10 0.00899543\\
11 0.00999383\\
12 0.0109399\\
13 0.0119664\\
14 0.0129645\\
15 0.0139899\\
16 0.0148372\\
17 0.0159921\\
18 0.0164528\\
19 0.0179454\\
20 0.018987\\
21 0.0197936\\
}; \label{displacementplot}
\end{axis}

\begin{axis}[%
width=3in,
height=3in,
scale only axis,
xmin=2,
xmax=21,
every outer y axis line/.append style={black},
every y tick label/.append style={font=\color{black}},
ymin=0,
ymax=1,
ytick={   0, 0.25,  0.5, 0.75,    1},
yticklabels ={    0$\%$, 25$\%$,  50$\%$, 75$\%$,  100$\%$},
ylabel={\ref{areaplot} Percentage of corrected area},
axis x line*=bottom,
axis y line*=right,
xtick={},
xticklabels={},
]
\addplot [
color=green,
solid,
line width=2.0pt,
forget plot
]
table[row sep=crcr]{
1 1\\
2 1\\
3 0.904386951631046\\
4 0.526809148856393\\
5 0.314585676790401\\
6 0.202474690663667\\
7 0.182602174728159\\
8 0.164604424446944\\
9 0.142482189726284\\
10 0.0941132358455193\\
11 0.0761154855643045\\
12 0.0712410948631421\\
13 0.0663667041619797\\
14 0.0626171728533933\\
15 0.0543682039745032\\
16 0.0284964379452568\\
17 0.0273715785526809\\
18 0.0269966254218223\\
19 0.0232470941132358\\
20 0.0213723284589426\\
21 0.0206224221972253\\
}; \label{areaplot}
\end{axis}
\end{tikzpicture}%
\end{document}
share|improve this answer

Adding to your ylabel a TikZ code in this way:

ylabel={\tikz\draw[green,fill=green] (0,0) circle (.35em);\ Percentatge of corrected area}
ylabel={\tikz\draw[red,fill=red] (0,0) circle (.35em);\ Maximun displacement error}

enter image description here

share|improve this answer
    
Brilliant! Exactly what I need! Thanks! –  Dan Sep 10 '13 at 16:06

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.