所以我刚开始使用 LaTeX。我创建了这个条形图,但它太宽了,无法放入我的 PDF 文档中。我不想旋转图表。我还需要将此图表与页面的左边缘对齐,这样我就可以弄清楚如何更改宽度以确保我的文本适合。有没有办法让我也可以在不减小文本大小的情况下使 x 轴标签适合,但只需将标签设为双行?
不确定我的问题是否有意义,但我已经花了几个小时试图弄清楚这个问题。任何帮助都将不胜感激!
这是我目前拥有的代码:
\begin{axis}[
ybar,
enlargelimits=0.10,
width=7in,
height=5in,
bar width= 20pt,
legend style={at={(0.5,-0.2)},
anchor=north,legend columns=-1},
ylabel={Counts},
symbolic x coords={Less Than HS, HS incomplete, HS graduate, Some college, Associate degree, Bachelor's, PG incomplete, PG degree},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
]
\addplot [color=gray, fill= lightgray] coordinates {(Less Than HS, 3) (HS incomplete, 21) (HS graduate, 211) (Some college, 127) (Associate degree, 92) (Bachelor's,162) (PG incomplete, 12) (PG degree, 96)};
\addplot [color=gray, fill= darkgray] coordinates {(Less Than HS, 35) (HS incomplete, 32) (HS graduate, 163) (Some college, 122) (Associate degree, 88) (Bachelor's,186) (PG incomplete, 17) (PG degree, 190)};
\legend{Protect,Control}
\end{axis}
\end{tikzpicture}
答案1
- 请始终提供 MWE(最小工作示例),这是一份小而完整的文档,可重现您的问题。通过它,您将帮助我们帮助您。
- 您的
xticklabel
宽度太宽。要缩小宽度,您有以下几种选择:- 使用较小的字体(通过
xticklabel style
在axis
选项中定义) - 将它们旋转一定角度
- 结合上述选项
- 使用较小的字体(通过
可能的解决方案:
\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
enlargelimits=0.10,
width=7in,
height=5in,
bar width= 20pt,
legend style={at={(0.5,-0.13)},
anchor=north,legend columns=-1},
ylabel={Counts},
symbolic x coords={Less Than HS, HS incomplete, HS graduate,
Some college, Associate degree, Bachelor's,
PG incomplete, PG degree},
xticklabel style = {text width=4em, align=flush right, inner sep=1pt,
rotate=30, anchor=north east,
font=\linespread{0.84}\selectfont}, % <---
xtick=data,
nodes near coords,
nodes near coords align={vertical},
]
\addplot [color=gray, fill= lightgray] coordinates {(Less Than HS, 3) (HS incomplete, 21) (HS graduate, 211) (Some college, 127) (Associate degree, 92) (Bachelor's,162) (PG incomplete, 12) (PG degree, 96)};
\addplot [color=gray, fill= darkgray] coordinates {(Less Than HS, 35) (HS incomplete, 32) (HS graduate, 163) (Some college, 122) (Associate degree, 88) (Bachelor's,186) (PG incomplete, 17) (PG degree, 190)};
\legend{Protect,Control}
\end{axis}
\end{tikzpicture}
\end{document}