我正在尝试调整标签位置,但似乎已经固定了。出了什么问题?
\documentclass{standalone}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{filecontents}
\begin{filecontents}[overwrite]{weatherdata.dat}
month;insulation 1931-1960;anchor 1;insulation 1981-2010;anchor 2
January;192.2;south;185.8;north
February;173.0;north;186.6;south
March;195.9;south;192.5;north
April;183.2;north;168.7;north
May;197.9;south;179.0;south
June;201.5;west;170.6;north
July;219.9;{south east};184.4;{south east}
August;219.3;west;184.4;{north east}
September;156.8;east;140.8;east
October;152.2;south;144.8;{north west}
November;153.2;south;150.3;{north east}
December;153.5;north;158.2;south
\end{filecontents}
\begin{document}
\pgfplotstableread[col sep=semicolon, header = true]
{weatherdata.dat}\wheatertable
\begin{tikzpicture}
\begin{axis}[
width = 15cm,
height = 7cm,
major x tick style = transparent,
smooth,
line width = 1 pt,
ymajorgrids = true,
yminorgrids = true,
ylabel = {Total insulation [h]},
axis x line=center,
axis y line=left,
nodes near coords = {\pgfmathprintnumber\pgfplotspointmeta},
nodes near coords align={vertical},
xtick = data,
scaled y ticks = false,
xticklabels from table = {\wheatertable}{month},
every node near coord/.append style={%
rotate=0,
font=\scriptsize,
/pgf/number format/precision=1,
/pgf/number format/fixed,
/pgf/number format/fixed zerofill,
/pgf/number format/1000 sep={.},
/pgf/number format/set decimal separator={,}
},%label above bar
enlarge x limits=0.05,
ymin=135,
ymax=230,
ylabel style={rotate=0},
x tick label style={font=\small,align=right, rotate = 45, anchor = east},%text width=3.0cm},
legend cell align=left,
legend style={%
at={(0.98,1.1)},
anchor=south east,
column sep=1ex,
font = \scriptsize
}%%
]
\addplot[style={blue, mark=*},
x = \coordindex,
y = \thisrowno{1},
visualization depends on = {\thisrowno{2} \as \firstanchor},
every node near coord/.append style={anchor=\firstanchor},
]
table[x expr = \coordindex, y index = 1] {\wheatertable};
\addlegendentry{1931-1960};
\addplot[style={red,mark=square*},
visualization depends on = {\thisrowno{4} \as \secondanchor},
every node near coord/.append style={anchor=\secondanchor},
]
table[x expr = \coordindex, y index = 3] {\wheatertable};
\addlegendentry{1981-2010};
% \addplot[top color=red!20, bottom color=cyan!20] fill between[of=min and max];
\end{axis}
\end{tikzpicture}
\end{document}
答案1
我以前遇到过这种情况,但我并不认为以下方法是最好的解决方案。由于某种原因,锚点被 y 坐标(即元值)覆盖。这里有一种可能的解决方法:而不是使用visualization depends on
来scatter/@pre marker code
获取锚点。(回想一下,这nodes near coords
是节点的散点图。)
\documentclass{standalone}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{filecontents}
\begin{filecontents}[overwrite]{weatherdata.dat}
month;insulation 1931-1960;anchor 1;insulation 1981-2010;anchor 2
January;192.2;south;185.8;north
February;173.0;north;186.6;south
March;195.9;south;192.5;north
April;183.2;north;168.7;north
May;197.9;south;179.0;south
June;201.5;west;170.6;north
July;219.9;{south east};184.4;{south east}
August;219.3;west;184.4;{north east}
September;156.8;east;140.8;east
October;152.2;south;144.8;{north west}
November;153.2;south;150.3;{north east}
December;153.5;north;158.2;south
\end{filecontents}
\begin{document}
\pgfplotstableread[col sep=semicolon, header = true]
{weatherdata.dat}\wheatertable
\begin{tikzpicture}
\begin{axis}[
width = 15cm,
height = 7cm,
major x tick style = transparent,
smooth,
line width = 1 pt,
ymajorgrids = true,
yminorgrids = true,
ylabel = {Total insulation [h]},
axis x line=center,
axis y line=left,
nodes near coords= {\pgfmathprintnumber\pgfplotspointmeta},
nodes near coords align={vertical},
scatter/@pre marker code/.code={
\pgfplotstablegetelem{\coordindex}{[index]2}\of\wheatertable%#1=row, #2=column
\edef\firstanchor{\pgfplotsretval}%
\pgfplotstablegetelem{\coordindex}{[index]4}\of\wheatertable%#1=row, #2=column
\edef\secondanchor{\pgfplotsretval}%
},
xtick = data,
scaled y ticks = false,
xticklabels from table = {\wheatertable}{month},
every node near coord/.append style={%
rotate=0,
font=\scriptsize,
/pgf/number format/precision=1,
/pgf/number format/fixed,
/pgf/number format/fixed zerofill,
/pgf/number format/1000 sep={.},
/pgf/number format/set decimal separator={,}
},%label above bar
enlarge x limits=0.05,
ymin=135,
ymax=230,
ylabel style={rotate=0},
x tick label style={font=\small,align=right, rotate = 45, anchor = east},%text width=3.0cm},
legend cell align=left,
legend style={%
at={(0.98,1.1)},
anchor=south east,
column sep=1ex,
font = \scriptsize
}%%
]
\addplot[style={blue, mark=*},
x = \coordindex,
y = \thisrowno{1},
every node near coord/.append style={anchor/.expanded=\firstanchor},
]
table[x expr = \coordindex, y index = 1] {\wheatertable};
\addlegendentry{1931-1960};
\addplot[style={red,mark=square*},
every node near coord/.append style={anchor=\secondanchor},
]
table[x expr = \coordindex, y index = 3] {\wheatertable};
\addlegendentry{1981-2010};
% \addplot[top color=red!20, bottom color=cyan!20] fill between[of=min and max];
\end{axis}
\end{tikzpicture}
\end{document}