我正在尝试生成如下的直方图:
在查看示例时pgf 图,我能够生成类似以下的直方图:
使用上面共享的示例链接中的代码:
\documentclass{article}
\usepackage{csvsimple}
\usepackage{longtable}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage{caption}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
x tick label style={
/pgf/number format/1000 sep=},
ylabel=Year,
enlargelimits=0.05,
legend style={at={(0.5,-0.1)},
anchor=north,legend columns=-1},
ybar interval=0.7,
]
\addplot
coordinates {(2012,408184) (2011,408348)
(2010,414870) (2009,412156)};
\addplot
coordinates {(2012,388950) (2011,393007)
(2010,398449) (2009,395972)};
\legend{Men,Women}
\end{axis}
\end{tikzpicture}
\end{document}
有没有什么方法pgf plots
可以生成自定义直方图?
答案1
这是一个很简单的建议。请注意,我将 改为ybar interval
,ybar
请参阅这里。主要思想是使用rounded corners
,但随后必须修复图例,使圆角半径更小。我还nodes near coords
根据要求删除了 y 轴并添加了 。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{/pgfplots/ybar legend/.style={
/pgfplots/legend image code/.code={
\draw [##1,/tikz/.cd,rounded corners=1pt,bar width=3pt,yshift=-0.2em,bar shift=0pt]
plot coordinates {(0cm,0.8em) (2*\pgfplotbarwidth,0.6em)};
},
},}
\begin{axis}[hide y axis,axis x line=bottom,
x tick label style={
/pgf/number format/1000 sep=},
%ylabel=Year,
enlargelimits=0.15,
legend style={at={(0.5,-0.1)}, anchor=north,legend columns=-1},
ybar,bar width=2em,
%symbolic x coords={2009,2010,2011,2012},
ybar legend,
nodes near coords=\pgfmathprintnumber{\pgfplotspointmeta},
every node near coord/.append style={
anchor=mid west,
rotate=70} % from https://tex.stackexchange.com/a/27143/121799
]
\addplot[rounded corners=8pt,fill=blue!60!black,
]
coordinates {(2009,412156) (2010,414870) (2011,408348) (2012,408184)};
\addplot [rounded corners=8pt,fill=red!60!black]
coordinates {(2009,395972) (2010,398449) (2011,393007) (2012,388950)};
\legend{Men,Women}
\end{axis}
\end{tikzpicture}
\end{document}