我有点困惑为什么乳胶会弄乱一个简单的绘图。更准确地说,我使用以下代码:
\begin{tikzpicture}
\begin{axis}[title = {Tex example},
xbar,
ytick = {Part1, Part2, Part3, Part4, Part5, Part6, Part7},
symbolic y coords = {Part1, Part2, Part3, Part4, Part5, Part6, Part7},
nodes near coords,
]
\addplot coordinates {(40.2,Part1)};
\addplot coordinates {(30.9,Part2)};
\addplot coordinates {(10.2,Part3)};
\addplot coordinates {(10.1,Part4)};
\addplot coordinates {(4.3,Part5)};
\addplot coordinates {(2.5,Part6)};
\addplot coordinates {(1.0,Part7)};
\end{axis}
\end{tikzpicture}
答案1
LaTeX 或 PGFPlots 确实不是把一切都搞乱了。正常行为是移动条形图,这样多个图就不会相互重叠。您的数据实际上不需要多个\addplot
,但如果条形图确实被视为可以有单独图例条目的单独图,您可以这样做:
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
title={Tex example},
xbar,
symbolic y coords={Part1, Part2, Part3, Part4, Part5, Part6, Part7},
ytick distance=1,
nodes near coords,
bar shift=0pt,
bar width=18pt,
enlarge x limits={value=0.2, upper},
]
\addplot coordinates {(40.2,Part1)};
\addplot coordinates {(30.9,Part2)};
\addplot coordinates {(10.2,Part3)};
\addplot coordinates {(10.1,Part4)};
\addplot coordinates {(4.3,Part5)};
\addplot coordinates {(2.5,Part6)};
\addplot coordinates {(1.0,Part7)};
\end{axis}
\end{tikzpicture}
\end{document}
更正常的方式(取决于您的数据真正代表什么)是使用:
\addplot coordinates {(40.2,Part1) (30.9,Part2) (10.2,Part3) (10.1,Part4) (4.3,Part5) (2.5,Part6) (1.0,Part7)};