我有以下多 y 轴图,结果显示图像位于顶部图像上。但是,我想在每个 Y 轴上添加一种图例,类似于我在底部图像中手动绘制的图例。您知道如何在 Y 轴标签中添加颜色标签吗?
\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}%
答案1
您可以使用PGFPlots 提供的\label
和\ref
功能将图例图像放入轴标签中:放在\label{<some text>}
每个\addplot
命令后面,并放在文本\ref{<some text>}
中ylabel
。这样,图例图像将与绘图线的颜色和样式保持同步:
\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}
答案2
以这种方式添加到您的ylabel
代码:TikZ
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}