![带有 pgfplots 的条形图:将所有条形图放在 x 轴上](https://linux22.com/image/421068/%E5%B8%A6%E6%9C%89%20pgfplots%20%E7%9A%84%E6%9D%A1%E5%BD%A2%E5%9B%BE%EF%BC%9A%E5%B0%86%E6%89%80%E6%9C%89%E6%9D%A1%E5%BD%A2%E5%9B%BE%E6%94%BE%E5%9C%A8%20x%20%E8%BD%B4%E4%B8%8A.png)
这是目前的结果:
\documentclass[border=10pt]{standalone}
\usepackage{graphicx}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.8}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
enlargelimits=0.15,
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
ylabel={\#mountainbikes},
symbolic x coords={April, Mai, Juni, Juli, August},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
]
\addplot+[ybar] plot coordinates {(April,0.36)};
\addplot+[ybar] plot coordinates {(Mai,0.4)};
\addplot+[ybar] plot coordinates {(Juni,0.5)};
\addplot+[ybar] plot coordinates {(Juli,0.55)};
\addplot+[ybar] plot coordinates {(August,0.52)};
\legend{\strut April, \strut Mai, \strut Juni, \strut Juli, \strut August}
\end{axis}
\end{tikzpicture}
\end{document}
我需要帮助的是:x 轴。
答案1
此类pgfplots
图表通常绘制如下:
\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17, % the last version at time of the answer
width=7cm}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width = 4mm,
enlargelimits=0.15,
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
ylabel={\#mountainbikes},
symbolic x coords={April, Mai, Juni, Juli, August},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
nodes near coords style={/pgf/number format/.cd, fixed, precision=2,
/tikz/.cd, font=\scriptsize},
]
\addplot coordinates {(April,0.36) (Mai,0.4) (Juni,0.5) (Juli,0.55) (August,0.52)};
\end{axis}
\end{tikzpicture}
\end{document}
如果您喜欢每个月使用不同的颜色,那么使用pgfplots
包的一个可能解决方案是将其\xtick
清空,然后使用\legend
:
\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17,
width=7cm}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width = 4mm,
enlarge x limits=0.8,
legend style={at={(0.5,-0.05)},
anchor=north,legend columns=-1,
/tikz/every even column/.append style={column sep=3pt},
/tikz/.cd, font=\footnotesize},
ylabel={\#mountainbikes},
xtick=\empty,
nodes near coords,
nodes near coords align={vertical},
nodes near coords style={/pgf/number format/.cd, fixed, precision=2,
/tikz/.cd, font=\scriptsize},
]
\addplot coordinates {(1,0.36)};
\addplot coordinates {(2,0.4)};
\addplot coordinates {(3,0.5)};
\addplot coordinates {(4,0.55)};
\addplot coordinates {(5,0.52)};
\legend{April, Mai, Juni, Juli, August}
\end{axis}
\end{tikzpicture}
\end{document}