LaTeX柱状图标注优化:文本对齐、标签显示及冗余移除问题
解决LaTeX柱状图的X轴标签问题
修改后的完整代码
\begin{figure} \centering \begin{tikzpicture} \begin{axis}[ ybar, symbolic x coords={CRITICAL, HIGH, MEDIUM, LOW}, xtick={CRITICAL, HIGH, MEDIUM, LOW}, % 明确指定X轴刻度,避免重复标签 x tick label style={rotate=50, anchor=south, align=center}, % 调整标签锚点,实现居中对齐 ymin=0, bar width=0.8cm, nodes near coords, axis x line=bottom, axis y line=left, enlarge x limits=0.2, % 缩小轴范围扩展比例,避免标签挤压 ylabel={Anzahl}, ] \addplot[draw=black, fill=red] coordinates {(CRITICAL, 3)}; \addplot[draw=black, fill=orange] coordinates {(HIGH, 17)}; \addplot[draw=black, fill=yellow] coordinates {(MEDIUM, 33)}; \addplot[draw=black, fill=blue] coordinates {(LOW, 54)}; \legend{} % 移除自动生成的图例(可选,若不需要图例则添加) \end{axis} \end{tikzpicture} \caption{Severity from the difference between Grype/Snyk and Docker Scout} \label{balkendiagramm_grype_scout} \end{figure}
关键修改说明
- X轴文本居中:将
x tick label style中的anchor=north east改为anchor=south,并添加align=center,让标签锚定在柱子正下方并居中对齐。 - 修复HIGH标签显示:把
enlarge x limits=1调整为0.2,原参数会过度扩展X轴范围,导致标签被挤压或偏移,缩小比例后可确保所有标签正常显示。 - 移除重复LOW标签:添加
xtick={CRITICAL, HIGH, MEDIUM, LOW}明确指定X轴刻度项,避免因多\addplot触发重复刻度渲染。
内容的提问来源于stack exchange,提问作者53845714nF
相关产品推荐
相关产品推荐

