直方图的图例

直方图的图例

我想设置图例中一个正方形的尺寸,然后能够将 xticks 和 yticks 之间的距离设置为该正方形的长度。

\documentclass{report}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
        \begin{axis}[ybar,ytick=data,ymajorgrids=true,yminorgrids=true,
        ymin=0,ymax=6.2,xmax=2250,xtick=data,
        area style,axis x line=bottom,
        axis y line = none,
        xlabel=\texttt{Salaire},ylabel=\texttt{Effectif},
        legend style={at={(1,0.5)},draw=none},
           /pgf/number format/.cd,    
        use comma,
        1000 sep={},
        legend image code/.code={%
            \draw[#1, draw] (0cm,-0.5cm) rectangle ++ (1cm,1cm);}
        ]
        \addplot+[ybar interval,mark=no] plot coordinates { (1400, 2) (1500, 5) (1600, 4) (1700, 3) (1800, 1) (2200,1)  };
        \legend{\texttt{20 employés}}
        \end{axis}
\end{tikzpicture}
\end{document}

谢谢

答案1

bar width您可以使用(并删除)设置栏的宽度ybar interval。在您的情况下,请设置bar width=1cm

(我缩放了图表并改变了 x 限制以避免条形重叠)

\begin{tikzpicture}
\begin{axis}[
scale=2,
ybar,
ytick=data,
ymajorgrids=true,
yminorgrids=true,
ymin=0,
ymax=6.2,
xmin=1300,
xmax=2300,
xtick=data,
area style,
axis x line=bottom,
axis y line = none,
xlabel=\texttt{Salaire},
ylabel=\texttt{Effectif},
legend style={at={(1,0.5)},draw=none},
/pgf/number format/.cd,    
use comma,
1000 sep={},
legend image code/.code={%
\draw[#1, draw] (0cm,-0.5cm) rectangle ++ (1cm,1cm);}
]
\addplot+[mark=no, bar width=1cm] plot coordinates { (1400, 2) (1500, 5) (1600, 4) (1700, 3) (1800, 1)  (2200, 1) };


\legend{\texttt{20 employés}}

\end{axis}
\end{tikzpicture}

在此处输入图片描述

相关内容