pgfplots 条形图 xticks 与条形图不对齐

pgfplots 条形图 xticks 与条形图不对齐

看起来 xticks 没有完全对齐条形,缺少第一个 AAA:

\documentclass[]{standalone}
\usepackage{pgfplotstable}
\usepackage{tikz}
\tikzset{font=\footnotesize}
\pgfplotstableread[col sep=comma]{
key,value
AAA,14.9
BBB,16.5
CCC,19.1
DDD,20.2
EEE,22.7
FFF,23.1
GGG,26.3
HHH,26.4
III,27.1
JJJ,30.8
KKK,35.5
LLL,37.3
MMM,110.0
}\mydata
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar,
    xmin = 0,xmax=14,
    %ytick=data,
    xticklabels from table={\mydata}{key},
    xlabel = {category},
    ylabel = {weight},
    x tick label style={rotate=90,anchor=east},
    axis lines=left,
    bar width=30,
    height=14cm
]
\addplot table [x expr=\coordindex,y=value] {\mydata};
\end{axis}
\end{tikzpicture}
\end{document}

输出:

在此处输入图片描述

答案1

  • 首先,您的图缺少关键xtick=data。然后 xtick 标签和数据匹配。

  • 您可能不希望第一个条形图仅打印一半在 y 轴旁边。请xmin = 0,xmax=14用替换xmin = -1,xmax=13

  • 也许你不希望 y 轴从最小值(即 )开始AAA。在这种情况下,请添加ymin=0

  • 要将 x 轴的标签向下移动,请使用xlabel style = {yshift=-5mm}

在此处输入图片描述

\documentclass[]{standalone}
\usepackage{pgfplotstable}
\usepackage{tikz}
\tikzset{font=\footnotesize}
\pgfplotstableread[col sep=comma]{
key,value
AAA,14.9
BBB,16.5
CCC,19.1
DDD,20.2
EEE,22.7
FFF,23.1
GGG,26.3
HHH,26.4
III,27.1
JJJ,30.8
KKK,35.5
LLL,37.3
MMM,110.0
}\mydata
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar,
    xmin = -1,xmax=13,
    ymin = 0,
    %ytick=data,
    xticklabels from table={\mydata}{key},
    xtick=data,
    xlabel = {category},
    xlabel style = {yshift=-5mm},
    ylabel = {weight},
    x tick label style={rotate=90,anchor=east},
    axis lines=left,
    bar width=30,
    height=14cm
]
\addplot table [x expr=\coordindex,y=value] {\mydata};
\end{axis}
\end{tikzpicture}
\end{document}

相关内容