在 pgfplots 中向上/向下移动 ylabel

在 pgfplots 中向上/向下移动 ylabel

我正在使用 pgfplots 创建一个有两个 y 轴的图表,一个在左边,一个在右边。我想知道如何上下移动 ylabel 以垂直对齐刻度线的中间:

截屏

这是我的代码:

\documentclass{book}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{set layers}
\begin{axis}[
width=5.3in,
date coordinates in=x,
xmin={2012-08-01},
xmax={2012-11-06},
ymin=-3,
ymax=3,
xtick={2012-08-01,2012-09-01,2012-10-01,2012-11-01},
xticklabels={August, September, October, November},
ytick={0,1,2,3},
ylabel={Depth (m)},
tick align=outside,
scale only axis,
xtick pos=left,
ytick pos=left,
]
\addplot[color=black]table[col sep=comma,y=depth]{depth.csv};
\end{axis}
\begin{axis}[
width=5.3in,
date coordinates in=x,
xmin={2012-08-01},
xmax={2012-11-06},
ymin=0,
ymax=50,
axis x line=none,
ytick={0,5,10,15,20},
ylabel={Rainfall (mm)},
ylabel near ticks,
yticklabel pos=right,
tick align=outside,
scale only axis,
xtick pos=left,
ytick pos=right,
]
\addplot[ybar=,bar width=3pt,fill=black]table[col sep=comma,y=rainfall]{rainfall.csv};
\end{axis}
\end{tikzpicture}
\end{document}

答案1

使用来ticklabel cs定位 ylabel:

第一个情节:

\begin{axis}[
width=5.3in,
date coordinates in=x,
xmin={2012-08-01},
xmax={2012-11-06},
ymin=-3,
ymax=3,
xtick={2012-08-01,2012-09-01,2012-10-01,2012-11-01},
xticklabels={August, September, October, November},
ytick={0,1,2,3},
ylabel={Depth (m)},
ylabel style={at={(ticklabel cs:0.75)}},% <- added code
tick align=outside,
scale only axis,
xtick pos=left,
ytick pos=left,
]
\addplot[color=black]table[col sep=comma,y=depth]{depth.csv};
\end{axis}

第二个情节:

\begin{axis}[
width=5.3in,
date coordinates in=x,
xmin={2012-08-01},
xmax={2012-11-06},
ymin=0,
ymax=50,
axis x line=none,
ytick={0,5,10,15,20},
ylabel={Rainfall (mm)},
ylabel style={at={(ticklabel cs:0.2)}},% <- added code
%ylabel near ticks,% <- remove this line
yticklabel pos=right,
tick align=outside,
scale only axis,
xtick pos=left,
ytick pos=right,
]
\addplot[ybar=,bar width=3pt,fill=black]table[col sep=comma,y=rainfall]{rainfall.csv};
\end{axis}

在此处输入图片描述

(由于 MWE 中缺少数据,因此该图为空。)

相关内容