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=14xmin = -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}

相關內容