使用 pgfplots 包绘制直方图的问题

使用 pgfplots 包绘制直方图的问题

以下代码使用 pgfplots 绘制直方图。

\documentclass[a4paper]{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
  \centering
  \begin{axis}[
    ybar,
    enlargelimits=0.15,
    legend style={at={(0.5,-0.15)},
    anchor=north,legend columns=-1},
    ylabel={\#percentage},
    symbolic x coords={mod2,mod3,mod5,mod7,mod11,mod13,mod17,mod19,mod23},
    xtick=data,
    nodes near coords,
    nodes near coords align={vertical},
    ]
    \addplot coordinates {(mod2,75.4064) (mod3,89.7961) (mod5,94.4597)
      (mod7,96.6786) (mod11,97.5600) (mod13,98.2339)
      (mod17,98.6138) (mod19,98.9129) (mod23,99.0970)};
    \addplot coordinates {(mod2,30.5101) (mod3,34.5384) (mod5,36.3324)
      (mod7,37.3570) (mod11,37.9158) (mod13,38.3514)
      (mod17,38.6484) (mod19,38.9125) (mod23,39.1067)};
    \legend{Residue,non-Residue}
  \end{axis}
\end{tikzpicture}

\end{document}

然而问题是输出看起来像这样。

直方图示例

有没有办法调整 x 轴的间距,使得 x 轴上的刻度和条形不重叠?

答案1

您可以做几件事。示例的完整源代码位于本文底部。请注意,您应该始终ymin=0明确指定,否则将使用最低数据点来确定绘图的范围。

让情节更加广泛: 您可以使用widthheight选项使绘图足够宽以容纳 x 刻度标签。为避免数据标签重叠,您可以使用旋转它们every node near coord/.append style={ anchor=mid west, rotate=70 }。在下面的示例中,我还使用关闭了上部和右侧轴边框axis lines*=left(如果没有*,轴线将带有箭头提示),因此旋转的标签不会与轴边框重叠。除此之外,您还可以设置enlarge y limits={upper,value=0.3}为将绘图在正 y 方向上放大 1.3 倍。

旋转 x 刻度标签,对数据标签值进行四舍五入:如果您想保持格式不变,您可以使用 旋转 x 刻度标签xticklabel style={ inner sep=0pt, anchor=north east, rotate=45 }(需要inner sepanchor选项才能正确对齐);使用 减小条形的宽度bar width,并使用 将数据标签四舍五入为整数nodes near coords={\pgfmathprintnumber[fixed,precision=0]{\pgfplotspointmeta}}

交换轴:或者,您可以交换坐标的 x 轴和 y 轴(如果您以表格形式提供数据,这将非常简单,因为您可以重新定义要用于 x 和 y 的列),并使用xbar而不是ybar。这可能是最明智的方法,因为它可以更好地比较值:


旋转的数据标签,宽图

\documentclass[a4paper]{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}% <-- moves axis labels near ticklabels (respects tick label widths)

\begin{document}

\begin{tikzpicture}
  \centering
  \begin{axis}[
        ybar,
    height=6cm,
    width=13cm,
    enlarge y limits=false,
    axis lines*=left,
    ymin=0,
    ymax=100,
     legend style={at={(0.5,-0.2)},
        anchor=north,legend columns=-1},
        ylabel={\#percentage},
        symbolic x coords={mod2,mod3,mod5,mod7,mod11,mod13,mod17,mod19,mod23},
     xtick=data,
        nodes near coords,
    every node near coord/.append style={
        anchor=mid west,
        rotate=70
    }
    ]
    \addplot coordinates {(mod2,75.4064) (mod3,89.7961) (mod5,94.4597)
      (mod7,96.6786) (mod11,97.5600) (mod13,98.2339)
      (mod17,98.6138) (mod19,98.9129) (mod23,99.0970)};
    \addplot coordinates {(mod2,30.5101) (mod3,34.5384) (mod5,36.3324)
      (mod7,37.3570) (mod11,37.9158) (mod13,38.3514)
      (mod17,38.6484) (mod19,38.9125) (mod23,39.1067)};
    \legend{Residue,non-Residue}
  \end{axis}
\end{tikzpicture}
\end{document}

旋转 x 标签,节点标签四舍五入为整数:

\documentclass[a4paper]{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
  \centering
  \begin{axis}[ybar,
    bar width=0.25cm,
    ymin=0,
    enlarge y limits={upper,value=0.15},
    legend style={at={(0.5,-0.25)},
    anchor=north,legend columns=-1},
    ylabel={\#percentage},
    symbolic x coords={mod2,mod3,mod5,mod7,mod11,mod13,mod17,mod19,mod23},
    xtick=data,
    xticklabel style={
        inner sep=0pt,
        anchor=north east,
        rotate=45
    },
    nodes near coords={\pgfmathprintnumber[fixed,precision=0]{\pgfplotspointmeta}},
    ]
    \addplot coordinates {(mod2,75.4064) (mod3,89.7961) (mod5,94.4597)
      (mod7,96.6786) (mod11,97.5600) (mod13,98.2339)
      (mod17,98.6138) (mod19,98.9129) (mod23,99.0970)};
    \addplot coordinates {(mod2,30.5101) (mod3,34.5384) (mod5,36.3324)
      (mod7,37.3570) (mod11,37.9158) (mod13,38.3514)
      (mod17,38.6484) (mod19,38.9125) (mod23,39.1067)};
    \legend{Residue,non-Residue}
  \end{axis}
\end{tikzpicture}

\end{document}

交换 x 轴和 y 轴:

\documentclass[a4paper]{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
  \centering
  \begin{axis}[
    xbar,
    bar width=0.2cm,
    legend style={at={(0.5,-0.15)},
    enlarge x limits={upper,value=0.19},
    xmin=0,xmax=100,
    anchor=north,legend columns=-1},
    ylabel={\#percentage},
    symbolic y coords={mod2,mod3,mod5,mod7,mod11,mod13,mod17,mod19,mod23},
    ytick=data,
    nodes near coords,
    nodes near coords align=horizontal,
    ]
    \addplot coordinates {(75.4064,mod2) (89.7961,mod3) (94.4597,mod5)
      (96.6786,mod7) (97.5600,mod11) (98.2339,mod13)
      (98.6138,mod17) (98.9129,mod19) (99.0970,mod23)};
    \addplot coordinates {(30.5101,mod2) (34.5384,mod3) (36.3324,mod5)
      (37.3570,mod7) (37.9158,mod11) (38.3514,mod13)
      (38.6484,mod17) (38.9125,mod19) (39.1067,mod23)};
    \legend{Residue,non-Residue}
  \end{axis}
\end{tikzpicture}

\end{document}

相关内容