格式化 xaxis 标签

格式化 xaxis 标签

有没有办法在符号 xcoords 中使用逗号。我想将它们添加到 xaxis 上的数字中,但我不确定如何进行。

这是我目前的代码

\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

假设您想用逗号分隔千位,例如200,000?如果是这种情况,我建议symbolic x coords根本不要使用。

您的 x 值间距均匀,因此我认为没有必要引入符号坐标。相反,您只需要对默认刻度的打印方式进行一些小调整,使用

scaled x ticks=false,
xticklabel style={/pgf/number format/fixed},

我还要补充一点

enlarge x limits=0.02

稍微减少轴内的空间。

在此处输入图片描述

\documentclass[12pt]{article}
\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}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=newest}
\usepgfplotslibrary{units}
\tikzset{every picture/.append style={font=\normalsize}} % size graph font


\definecolor{customcolor}{HTML}{1d5893}
\begin{document}
\begin{minipage}{0.96\linewidth}
\begin{tikzpicture}
\pgfplotscreateplotcyclelist{defaultCycle}{%
ybar,ybar legend,fill=customcolor,draw=black,opacity=1,thin,solid,mark=no,mark options=solid,\\%
}
\begin{axis}
[
    cycle list name=defaultCycle,
    width=0.96\linewidth,
    height=0.15\textheight,
    use units,
    scale only axis,
    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,
    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,
    scaled x ticks=false,
    xticklabel style={/pgf/number format/fixed},
    enlarge x limits=0.02
    ]
        \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 expr=\coordindex与相结合xticklabels from table={<table or file>}{<column name>}。这将需要您手动添加逗号。这是一个有点愚蠢的例子。

在此处输入图片描述

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
\pgfplotstableread[col sep=semicolon]{
        x;  y;  z
        $-22,00,00$;    4;  0
        $-2100,00$;    0;  0
        $-2000,00$;    2;  0
}\plotdata
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  ybar,
  xtick=data,
  xticklabels from table={\plotdata}{x}
  ]
        \addplot+ table [x expr=\coordindex,y={y},meta index=2] {\plotdata};
\end{axis}
\end{tikzpicture}
\end{document}

要回答您的具体问题:您可以使用symbolic x coords此类数据,只需将每个坐标括在{}

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
\pgfplotstableread[col sep=semicolon]{
        x;  y;  z
        $-22,00,00$;    4;  0
        $-2100,00$;    0;  0
        $-2000,00$;    2;  0
}\plotdata
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  ybar,
  xtick=data,
  symbolic x coords={%
    {$-22,00,00$},
    {$-2100,00$},
    {$-2000,00$}
    }
  ]
        \addplot+ table [x=x,y={y},meta index=2] {\plotdata};
\end{axis}
\end{tikzpicture}
\end{document}

相关内容