You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LaTeX TikZ绘图:数据点与边框间距随条目数增大异常问题

TikZ条形图首尾间距异常问题排查与解决

问题现象

用LaTeX的TikZ/PGFPlots绘制水平条形图时,数据条目较少时,首尾数据点与图表边框的间距显示正常;但数据条目增多后,首尾数据点与边框的间距会明显变大,破坏整体排版。

原因分析

  1. 整体缩放的冲突:你使用\resizebox{\textwidth}{!}对整个tikzpicture进行缩放,同时在axis选项里固定了y=0.7cm(每个分类的垂直高度)。当数据条目增加,图表原始高度大幅提升,缩放后每个分类的实际高度被压缩,但PGFPlots自动计算的轴上下边距是基于原始尺寸的比例,缩放后这些边距被放大,导致首尾间距异常。
  2. framed样式的干扰:tikzpicture的framed样式基于原始画布尺寸生成边框,缩放后边框和轴内容的相对位置失调,进一步凸显间距问题。

解决方法

方案1:用PGFPlots原生宽度设置替代整体缩放(推荐)

移除\resizebox,直接在axis选项中设置width=\textwidth,让PGFPlots自动计算合适的高度,避免整体缩放带来的比例失调。同时可通过yticklabel style调整标签大小,确保排版美观。

方案2:固定首尾边距(适合必须缩放的场景)

如果一定要用\resizebox,可以通过enlarge y limits={abs=0.5cm}手动设置首尾的固定边距,替代PGFPlots的自动边距计算,确保无论数据多少,首尾间距一致。

修改后的代码示例

方案1代码(原生宽度设置)

\documentclass[11pt,a4paper]{scrreprt}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{backgrounds}

\begin{document}
\begin{tikzpicture}[tlabel/.style={yshift=2pt,above right,font=\small}]
    \begin{axis}[
        clip=false,
        axis line style={draw=none},
        legend style={at={(1.15,-0.2)},anchor=south west},
        reverse legend,
        xmin = 0,
        xmax = 15,
        xbar,
        bar width=2.5pt,
        width=\textwidth, % 用原生宽度设置替代resizebox
        xlabel = Label,
        ylabel = Label,
        symbolic y coords = {
            content1, content2, content3, content4,
            content5, content6, content7, content8,
            content9, content10, content11, content12,
            content13, content14, content15, content16,
            content17, content18, content19, content20
        },
        y dir = reverse,
        y=0.7cm,
        xtick = {1,2,3,4},
        xtick pos = bottom,
        ytick = data,
        ytick pos = right,
        yticklabel pos = right,
        yticklabel style={font=\small} % 调整标签大小适配宽度
    ]
        \addplot[draw=red!80!white,fill=red!80!white] coordinates {
            (1,content1) (2,content2) (3,content3) (4,content4)
            (1,content5) (2,content6) (3,content7) (4,content8)
            (1,content9) (2,content10) (3,content11) (4,content12)
            (4,content13) (4,content14) (4,content15) (4,content16)
            (4,content17) (4,content18) (4,content19) (4,content20)
        };
        \addplot[draw=blue!80!white,fill=blue!80!white,nodes near coords] coordinates {
            (4,content1) (3,content2) (2,content3) (1,content4)
            (1,content5) (2,content6) (3,content7) (4,content8)
            (1,content9) (2,content10) (3,content11) (4,content12)
            (4,content13) (4,content14) (4,content15) (4,content16)
            (4,content17) (4,content18) (4,content19) (4,content20)
        };
        \legend{label1, label2}
    \end{axis}
\end{tikzpicture}
\end{document}

方案2代码(固定边距+缩放)

\documentclass[11pt,a4paper]{scrreprt}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{backgrounds}

\begin{document}
\resizebox{\textwidth}{!}{
    \begin{tikzpicture}[tlabel/.style={yshift=2pt,above right,font=\small}]
        \begin{axis}[
            clip=false,
            axis line style={draw=none},
            legend style={at={(1.15,-0.2)},anchor=south west},
            reverse legend,
            xmin = 0,
            xmax = 15,
            xbar,
            bar width=2.5pt,
            xlabel = Label,
            ylabel = Label,
            symbolic y coords = {
                content1, content2, content3, content4,
                content5, content6, content7, content8,
                content9, content10, content11, content12,
                content13, content14, content15, content16,
                content17, content18, content19, content20
            },
            y dir = reverse,
            y=0.7cm,
            xtick = {1,2,3,4},
            xtick pos = bottom,
            ytick = data,
            ytick pos = right,
            yticklabel pos = right,
            enlarge y limits={abs=0.5cm} % 固定首尾边距
        ]
            \addplot[draw=red!80!white,fill=red!80!white] coordinates {
                (1,content1) (2,content2) (3,content3) (4,content4)
                (1,content5) (2,content6) (3,content7) (4,content8)
                (1,content9) (2,content10) (3,content11) (4,content12)
                (4,content13) (4,content14) (4,content15) (4,content16)
                (4,content17) (4,content18) (4,content19) (4,content20)
            };
            \addplot[draw=blue!80!white,fill=blue!80!white,nodes near coords] coordinates {
                (4,content1) (3,content2) (2,content3) (1,content4)
                (1,content5) (2,content6) (3,content7) (4,content8)
                (1,content9) (2,content10) (3,content11) (4,content12)
                (4,content13) (4,content14) (4,content15) (4,content16)
                (4,content17) (4,content18) (4,content19) (4,content20)
            };
            \legend{label1, label2}
        \end{axis}
    \end{tikzpicture}
}
\end{document}

内容的提问来源于stack exchange,提问作者morzan1001

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 08:55:37