我在 tikz 中有一个带有两个 y 轴的图表。我想在 y 轴上添加一个图例,以便读者可以清楚地看到。我正在寻找解决方案,然后找到了此链接https://tex.stackexchange.com/a/132619/135783。此解决方案适用于单个地块。如何对两个地块执行相同操作?
\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}
\addplot [
color=blue,
solid,
line width=2.0pt,
forget plot
]
table[row sep=crcr]{
1 1\\
2 1\\
3 0.954386951631046\\
4 0.576809148856393\\
5 0.364585676790401\\
6 0.252474690663667\\
7 0.232602174728159\\
8 0.214604424446944\\
9 0.192482189726284\\
10 0.1441132358455193\\
11 0.1261154855643045\\
12 0.1212410948631421\\
13 0.1163667041619797\\
14 0.1126171728533933\\
15 0.1043682039745032\\
16 0.0984964379452568\\
17 0.0973715785526809\\
18 0.0969966254218223\\
19 0.0932470941132358\\
20 0.0913723284589426\\
21 0.0906224221972253\\
};
\end{axis}
\end{tikzpicture}%
\end{document}
这将产生以下内容:
我想在右侧 y 轴上添加蓝色图例,如两条小垂直线;一条蓝色,一条绿色。这可能吗?
我做了这样的事情:
ylabel={\ref{areaplot1}\ref{areaplot2} Percentage of corrected area},
但它会创建两条相继而非平行的线。
答案1
- 您需要
ylabel style = {align=center}
或类似的东西来使其ylabel
能够处理换行符。 - 看我怎样才能使我的 pgfplots ylabel 多行?例如。
下次也许可以提供一个更简单的例子。您的代码中有很多内容对于理解您的问题来说不是必需的(例如数值数据或
grid
和的微调整ticks
)。
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
% \pgfplotsset{compat=1.3} % also works for me
% \pgfplotsset{compat=1.8} % also works for me
\begin{document}
%% Taken from the pgfplots manual
% Revision 1.15 (2017/06/05)
% 4.9.11 Two Ordinates (y-axis) or Multiple Axes
\begin{tikzpicture}
% let both axes use the same layers
\pgfplotsset{set layers}
%
\begin{axis}[
scale only axis,
xmin=-5,xmax=5,
axis y line*=left,
xlabel=$x$,
ylabel style = {align=center},
ylabel={Plot 1 \ref{pgfplots:plot1}},
]
\addplot {x^2};
\label{pgfplots:plot1}
\end{axis}
%
\begin{axis}[
scale only axis,
xmin=-5,xmax=5,
axis y line*=right,
axis x line=none,
ylabel style = {align=center},
ylabel={Plot 2 \ref{pgfplots:plot2} \\ Plot 3 \ref{pgfplots:plot3}},
]
\addplot [red] {3*x};
\label{pgfplots:plot2}
\addplot [black] {1*x};
\label{pgfplots:plot3}
\end{axis}
%
\end{tikzpicture}
\end{document}