我想在 latex 中绘制清晰的 xbar。我生成的图像宽度不相等,并且没有命名所有 y 标签。我做错了什么?
\begin{figure}
\begin{center}
\begin{tikzpicture}
\begin{axis} [
xbar,
symbolic y coords = {{Apples},{Oranges},{Bananas},{Grapes},{Strawberries},{Blueberries}},
ytick= data,
nodes near coords,
nodes near coords align = {horizontal}
]
\addplot [draw=black, fill=cyan!40!black] coordinates {
(1, {Apples}) (2, {Oranges}) (5, {Bananas}) (10, {Grapes}) (100, {Strawberries}) (1000, {Blueberries})
};
\end{axis}
\end{tikzpicture}
\end{center}
\end{figure}
答案1
请尝试以下操作:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
title={Test: monocolor},
xbar,
ytick distance=1, % <---
title={Tex example},
nodes near coords,
symbolic y coords = {Apples,Oranges,Bananas,
Grapes,Strawberries,Blueberries},
xmin=0,
enlarge x limits={0.2, upper} % <---
]
\addplot [fill=cyan!40!black] coordinates {(1,Apples) (2,Oranges) (5,Bananas)
(10,Grapes) (100,Strawberries) (1000,Blueberries)
};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
我的代码片段与您所截取的代码的不同之处在于% <---
。编译代码时,您始终应该考虑错误。