调整条形图

调整条形图

我正在尝试创建此条形图,但它的起点距离轴的起点太远,有什么方法可以将其“推”得更靠近 y 轴线吗?现在,该图看起来非常挤压,我不得不将 xtick 字体大小调小,这样才能将所有标签都放进去。

这是我迄今为止的代码:

\documentclass[12pt]{article}
\usepackage{fancyhdr}
\renewcommand{\familydefault}{\sfdefault}
\renewcommand*{\ttdefault}{\familydefault}
\usepackage[paperwidth=42cm,paperheight=29.7cm,left =1cm, top = 1cm, right =1cm, bottom = 1cm ,marginparwidth=0cm, includeheadfoot,headheight=66pt, headsep=0cm]{geometry}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\usepackage[table,xcdraw]{xcolor}
\usepackage[document]{ragged2e}
\usepackage[export]{adjustbox}
\usepackage{graphicx}
\usepackage{titlesec}
\usepackage{float}
\usepackage{booktabs}
\usepackage{colortbl}
\usepackage{caption}
\usepackage{calc}
\usepackage{tikz}
\usepackage{pgfplots}

\usepackage{times}
\captionsetup{justification=raggedright,singlelinecheck=false}
\pgfplotsset{width=10cm,compat=newest}
\usetikzlibrary{plotmarks}
\usepgfplotslibrary{units}
\tikzset{every picture/.append style={font=\normalsize}} % size graph font
\usetikzlibrary{arrows, positioning, calc}




\definecolor{customcolor}{HTML}{1d5893}
\begin{document}
\begin{minipage}{0.96\linewidth}
\flushleft
\begin{tikzpicture}
\pgfplotscreateplotcyclelist{defaultCycle}{%
ybar,ybar legend,fill=customcolor,draw=black,opacity=1,thin,solid,mark=no,mark options=solid,\\%
}
\begin{axis}
[
    xbar,
    cycle list name=defaultCycle,
    width=0.96\linewidth,
    height=0.15\textheight,
    use units,
    scale only axis,
    symbolic x coords={-220000, -210000,-200000,-190000,-180000,-170000, -160000,-150000,-140000,-130000,-120000,-110000,-100000,-90000,-80000,-70000,-60000,-50000,-40000,-30000,-20000,-10000,0,10000,20000,30000,40000,50000,60000,70000,80000,90000,100000,110000,120000,130000,140000,150000,160000,170000,180000,190000,200000,210000,220000},
    xtick=data,
    yticklabel style={/pgf/number format/fixed},,
    ytick pos=left,
    axis y line*=left,
    xtick pos=bottom,
    axis x line*=bottom,
    legend style={draw=none,at={(0,1.03)},anchor=south west},
    legend columns=-1,
    xtick align=center,
    ytick align=center,
    xtick distance=,
    ytick distance=,
    x tick label style ={font=\tiny,text width=1.5cm,anchor=north,rotate=15,align=center},
    y tick label style ={font=\normalsize,text width=2cm,anchor=east,rotate=0,align=right},
    scaled y ticks=false,
    bar width=15pt,
    ymajorgrids,
    colorbar=false,
    colormap/viridis,
    x unit=,
    y unit=,
    ]
        \addplot+ table [x={x},y={y},meta index=2,col sep=semicolon] {
        x;  y;  z
        -220000;    4;  0
        -210000;    0;  0
        -200000;    2;  0
        -190000;    0;  0
        -180000;    1;  0
        -170000;    3;  0
        -160000;    1;  0
        -150000;    5;  0
        -140000;    0;  0
        -130000;    2;  0
        -120000;    3;  0
        -110000;    7;  0
        -100000;    4;  0
        -90000; 2;  0
        -80000; 8;  0
        -70000; 11; 0
        -60000; 6;  0
        -50000; 11; 0
        -40000; 8;  0
        -30000; 6;  0
        -20000; 17; 0
        -10000; 15; 0
        0;  16; 0
        10000;  19; 0
        20000;  9;  0
        30000;  15; 0
        40000;  24; 0
        50000;  7;  0
        60000;  11; 0
        70000;  10; 0
        80000;  7;  0
        90000;  5;  0
        100000; 6;  0
        110000; 14; 0
        120000; 8;  0
        130000; 12; 0
        140000; 21; 0
        150000; 3;  0
        160000; 3;  0
        170000; 2;  0
        180000; 9;  0
        190000; 13; 0
        200000; 0;  0
        210000; 2;  0
        220000; 3;  0
        };
\end{axis}
\end{tikzpicture}
\end{minipage}
\end{document}

和当前输出:

标签太小,无法阅读

答案1

您忘记使用 定义最小 y 值ymin。只需将其设置为 即可0。对于 x 轴:定义230000-230000值并将它们分别设置为xmaxxmin

这是代码(仅使用您的代码并进行提到的更改):

\documentclass[12pt]{article}
\usepackage{fancyhdr}
\renewcommand{\familydefault}{\sfdefault}
\renewcommand*{\ttdefault}{\familydefault}
\usepackage[paperwidth=42cm,paperheight=29.7cm,left =1cm, top = 1cm, right =1cm, bottom = 1cm ,marginparwidth=0cm, includeheadfoot,headheight=66pt, headsep=0cm]{geometry}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\usepackage[table,xcdraw]{xcolor}
\usepackage[document]{ragged2e}
\usepackage[export]{adjustbox}
\usepackage{graphicx}
\usepackage{titlesec}
\usepackage{float}
\usepackage{booktabs}
\usepackage{colortbl}
\usepackage{caption}
\usepackage{calc}
\usepackage{tikz}
\usepackage{pgfplots}

\usepackage{times}
\captionsetup{justification=raggedright,singlelinecheck=false}
\pgfplotsset{width=10cm,compat=newest}
\usetikzlibrary{plotmarks}
\usepgfplotslibrary{units}
\tikzset{every picture/.append style={font=\normalsize}} % size graph font
\usetikzlibrary{arrows, positioning, calc}




\definecolor{customcolor}{HTML}{1d5893}
\begin{document}
    \begin{minipage}{0.96\linewidth}
        \flushleft
        \begin{tikzpicture}
            \pgfplotscreateplotcyclelist{defaultCycle}{%
                ybar,ybar legend,fill=customcolor,draw=black,opacity=1,thin,solid,mark=no,mark options=solid,\\%
            }
            \begin{axis}
                [
                xbar,
                cycle list name=defaultCycle,
                width=0.96\linewidth,
                height=0.15\textheight,
                use units,
                scale only axis,
                symbolic x coords={-230000,-220000, -210000,-200000,-190000,-180000,-170000, -160000,-150000,-140000,-130000,-120000,-110000,-100000,-90000,-80000,-70000,-60000,-50000,-40000,-30000,-20000,-10000,0,10000,20000,30000,40000,50000,60000,70000,80000,90000,100000,110000,120000,130000,140000,150000,160000,170000,180000,190000,200000,210000,220000,230000},
                xtick=data,
                yticklabel style={/pgf/number format/fixed},,
                ytick pos=left,
                axis y line*=left,
                xtick pos=bottom,
                axis x line*=bottom,
                legend style={draw=none,at={(0,1.03)},anchor=south west},
                legend columns=-1,
                xtick align=center,
                ytick align=center,
                xtick distance=,
                ytick distance=,
                x tick label style ={font=\tiny,text width=1.5cm,anchor=north,rotate=15,align=center},
                y tick label style ={font=\normalsize,text width=2cm,anchor=east,rotate=0,align=right},
                scaled y ticks=false,
                bar width=15pt,
                ymajorgrids,
                colorbar=false,
                colormap/viridis,
                x unit=,
                y unit=,
                ymin=0,
                xmin=-230000,
                xmax=230000,
                ]
                \addplot+ table [x={x},y={y},meta index=2,col sep=semicolon] {
                    x;  y;  z
                    -220000;    4;  0
                    -210000;    0;  0
                    -200000;    2;  0
                    -190000;    0;  0
                    -180000;    1;  0
                    -170000;    3;  0
                    -160000;    1;  0
                    -150000;    5;  0
                    -140000;    0;  0
                    -130000;    2;  0
                    -120000;    3;  0
                    -110000;    7;  0
                    -100000;    4;  0
                    -90000; 2;  0
                    -80000; 8;  0
                    -70000; 11; 0
                    -60000; 6;  0
                    -50000; 11; 0
                    -40000; 8;  0
                    -30000; 6;  0
                    -20000; 17; 0
                    -10000; 15; 0
                    0;  16; 0
                    10000;  19; 0
                    20000;  9;  0
                    30000;  15; 0
                    40000;  24; 0
                    50000;  7;  0
                    60000;  11; 0
                    70000;  10; 0
                    80000;  7;  0
                    90000;  5;  0
                    100000; 6;  0
                    110000; 14; 0
                    120000; 8;  0
                    130000; 12; 0
                    140000; 21; 0
                    150000; 3;  0
                    160000; 3;  0
                    170000; 2;  0
                    180000; 9;  0
                    190000; 13; 0
                    200000; 0;  0
                    210000; 2;  0
                    220000; 3;  0
                };
            \end{axis}
        \end{tikzpicture}
    \end{minipage}
\end{document}

在此处输入图片描述

如果您进一步旋转 x-tick 标签,则可以放大字体。

编辑:或者,您可以添加选项enlarge x limits,以创建所需的左右距离。以下是相应的代码,我还从中删除了此示例不需要的所有内容:

\documentclass[12pt]{article}
\usepackage[paperwidth=42cm,paperheight=29.7cm,margin = 1cm ,marginparwidth=0cm, includeheadfoot,headheight=66pt, headsep=0cm]{geometry}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=newest}
\definecolor{customcolor}{HTML}{1d5893}

\begin{document}
    \begin{tikzpicture}
        \pgfplotscreateplotcyclelist{defaultCycle}{fill=customcolor,draw=black,thin,solid,\\}
        \begin{axis}
            [
            ybar,
            cycle list name=defaultCycle,
            width=0.80\linewidth,
            height=0.15\textheight,
            scale only axis,
            symbolic x coords={-220000, -210000,-200000,-190000,-180000,-170000, -160000,-150000,-140000,-130000,-120000,-110000,-100000,-90000,-80000,-70000,-60000,-50000,-40000,-30000,-20000,-10000,0,10000,20000,30000,40000,50000,60000,70000,80000,90000,100000,110000,120000,130000,140000,150000,160000,170000,180000,190000,200000,210000,220000},
            xtick=data,
            yticklabel style={/pgf/number format/fixed},
            axis y line*=left,
            xtick pos=bottom,
            axis x line*=bottom,
            x tick label style ={font=\small,text width=1.5cm,anchor=east,rotate=90,align=right},
            bar width=8pt,
            ymajorgrids,
            ymin=0,
            xmin=-220000,
            xmax=220000,
            enlarge x limits=0.01,
            ]
            \addplot+ table [x={x},y={y},meta index=2,col sep=semicolon] {
                x;  y;  z
                -220000;    4;  0
                -210000;    0;  0
                -200000;    2;  0
                -190000;    0;  0
                -180000;    1;  0
                -170000;    3;  0
                -160000;    1;  0
                -150000;    5;  0
                -140000;    0;  0
                -130000;    2;  0
                -120000;    3;  0
                -110000;    7;  0
                -100000;    4;  0
                -90000; 2;  0
                -80000; 8;  0
                -70000; 11; 0
                -60000; 6;  0
                -50000; 11; 0
                -40000; 8;  0
                -30000; 6;  0
                -20000; 17; 0
                -10000; 15; 0
                0;  16; 0
                10000;  19; 0
                20000;  9;  0
                30000;  15; 0
                40000;  24; 0
                50000;  7;  0
                60000;  11; 0
                70000;  10; 0
                80000;  7;  0
                90000;  5;  0
                100000; 6;  0
                110000; 14; 0
                120000; 8;  0
                130000; 12; 0
                140000; 21; 0
                150000; 3;  0
                160000; 3;  0
                170000; 2;  0
                180000; 9;  0
                190000; 13; 0
                200000; 0;  0
                210000; 2;  0
                220000; 3;  0
            };
        \end{axis}
    \end{tikzpicture}
\end{document}

相关内容