是否可以使用坐标附近的节点来旋转文本?

是否可以使用坐标附近的节点来旋转文本?

我正在绘制 ybar,坐标附近的节点很丑陋。是否可以将条形图上方的文本旋转 90 度?

\begin{tikzpicture}
\begin{axis}[area legend,
legend pos=north west,
legend columns=4,
legend style={draw=none},
symbolic x coords={D,F,R,M,K,S,C},
major tick length=0cm,
xtick=data,
nodes near coords,
ybar,
bar width = 6pt,
]
\addplot[area legend] %[ybar,,fill=blue]  fill=color1
coordinates {(D,0.78) (F,0.23) (R,0.93) (M,0.73) (K,0.00) (S,0.37) (C,0.36)};
\addplot[area legend] %[ybar,,fill=blue]
coordinates {(D,0.52) (F,0.62) (R,0.60) (M,0.56) (K,0.64) (S,0.166) (C,0.170)};
\addplot[area legend] %[ybar,,fill=blue]
coordinates {(D,0.635) (F,0.437) (R,0.744) (M,0.345) (K,0.48) (S,0.652) (C,0.955)};
\end{axis}
\end{tikzpicture}

答案1

您可以使用以下方式旋转节点

every node near coord/.append style={rotate=90, anchor=west}

请注意,此选项需要在之后添加ybar,因为它ybar会覆盖every node near coord样式。

它们nodes near coords不会自动扩大绘图区域来容纳它们,所以您必须做一些事情,比如enlarge y limits={upper,value=0.2}调整绘图区域的顶部边框。

area legend(未请求的额外信息:必须将选项传递给addplots,它不是一个axis选项。要对所有图执行此操作,您必须使用axis选项every axis plot post/.style={area legend}

带旋转标签的 pgfplots

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    symbolic x coords={D,F,R,M,K,S,C},
    major tick length=0cm,
    xtick=data,
    enlarge y limits={upper,value=0.2},
    nodes near coords,
    ybar,
    every node near coord/.append style={rotate=90, anchor=west},
    bar width = 6pt,
]
\addplot
coordinates {(D,0.78) (F,0.23) (R,0.93) (M,0.73) (K,0.00) (S,0.37) (C,0.36)};
\addplot
coordinates {(D,0.52) (F,0.62) (R,0.60) (M,0.56) (K,0.64) (S,0.166) (C,0.170)};
\addplot
coordinates {(D,0.635) (F,0.437) (R,0.744) (M,0.345) (K,0.48) (S,0.652) (C,0.955)};
\end{axis}
\end{tikzpicture}
\end{document}

答案2

您可以使用\rotatebox{<angle>}{Text}(从已加载的graphicx)包来旋转文本。更复杂的东西,如逐字文本,可以使用包{adjustbox}{angle=<angle>}提供的环境进行旋转adjustbox。只需在节点内使用这些宏,但 TikZ 可能有自己的选项。

相关内容