我创建了以下直方图,但所有标签都乱了。
\documentclass{standalone}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\pgfplotsset{
compat=newest,
xlabel near ticks,
ylabel near ticks
}
\begin{document}
\begin{tikzpicture}[font=\small]
\begin{axis}[
ybar,
bar width=20pt,
ymin=0,
%ymax=100,
xtick=data,
axis x line=bottom,
axis y line=left,
enlarge x limits=0.2,
symbolic x coords={First Variable, Second Variable, Third Variable, Fourth Variable, Fifth Variable},
xticklabel style={anchor=base,yshift=-\baselineskip},
nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}
]
\addplot[fill=orange,fill opacity=0.5, text opacity=1] coordinates {
(First Variable,42)
(Second Variable,36)
(Third Variable,23)
(Fourth Variable,22)
(Fifth Variable,61)
};
\end{axis}
\end{tikzpicture}
\end{document}
然后我缩短了标签以使其更适合它们,但这需要我添加图例。但图例只给出了一个标签,而不是其他标签。我需要有关如何安装标签或解释图例中标签的帮助。
\documentclass{standalone}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\pgfplotsset{
compat=newest,
xlabel near ticks,
ylabel near ticks
}
\begin{document}
\begin{tikzpicture}[font=\small]
\begin{axis}[
ybar,
bar width=20pt,
ymin=0,
%ymax=100,
xtick=data,
axis x line=bottom,
axis y line=left,
enlarge x limits=0.2,
symbolic x coords={A, B, C, D, E},
xticklabel style={anchor=base,yshift=-\baselineskip},
nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}
]
\addplot[fill=orange,fill opacity=0.5, text opacity=1] coordinates {
(A,42)
(B,36)
(C,23)
(D,22)
(E,61)
};
\legend{A=First Variable, B=Second Variable, C=Third Variable, D=Fourth Variable,E=Fifth Variable}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
您可能不需要缩短标签。如果将标签添加text width=1.3cm
到,xticklabel style
标签将跨越多行。如果您还通过设置使图表稍微宽一些,则会有更多的空间。使用的确切长度将取决于您的具体情况,因此请根据需要width
调整width
和设置。text width
\documentclass{standalone}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\pgfplotsset{
compat=newest,
xlabel near ticks,
ylabel near ticks
}
\begin{document}
\begin{tikzpicture}[font=\small]
\begin{axis}[
width=10cm, % increase width of diagram a bit
height=6cm,
ybar,
bar width=20pt,
ymin=0,
%ymax=100,
xtick=data,
axis x line=bottom,
axis y line=left,
enlarge x limits=0.2,
symbolic x coords={First Variable, Second Variable, Third Variable, Fourth Variable, Fifth Variable},
xticklabel style={
anchor=base,
yshift=-\baselineskip,
text width=1.3cm, % means longer labels will be broken across multiple lines
align=center, % center align
% font=\scriptsize % if necessary, you could reduce the font size further
},
nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}
]
\addplot[fill=orange,fill opacity=0.5, text opacity=1] coordinates {
(First Variable,42)
(Second Variable,36)
(Third Variable,23)
(Fourth Variable,22)
(Fifth Variable,61)
};
\end{axis}
\end{tikzpicture}
\end{document}