如何消除条形图中条形之间的令人讨厌的空白?

如何消除条形图中条形之间的令人讨厌的空白?

我从文件中删除了一行数据,但找不到去除图表中心空白的方法。减小宽度会将条形图移到图表之外,而获得良好外观的唯一方法是将宽度扩大到不合理的值。如何纠正这个问题?

\documentclass{standalone}
\usepackage{pgfplotstable}
\usepackage{filecontents} 
\begin{document}

%==========================
\begin{filecontents}{data0.csv}
{Product-Code},{Total parts},{Active parts}
{0728}, 2414,0
{0510},9931,1510
\end{filecontents}
%===========================
\pgfplotstableread[col sep=comma]{data0.csv}{\datatable}

\begin{tikzpicture}
\begin{axis}[height=6cm, width=12 cm,
%grid=both,
ybar,
enlarge y limits={value= 0.3, upper},
enlarge x limits=true,
bar width=20pt,
xtick=data,
x tick label style={major tick length=0pt},
xticklabels=data,
xticklabels from table={\datatable}{[index]0},
%every even column/.append style={column sep=1cm},
nodes near coords, every node near coord/.append style={font=\footnotesize, /pgf/number format/fixed,
/pgf/number format/1000 sep = \thinspace },
title={Number of parts per product code},
xlabel={PRODUCT CODE},
ymin=0,
ytick style={draw=none},
scaled y ticks = false,
yticklabels={\empty},
legend style={nodes=right},
legend pos= north west,
legend entries={ Active, Total},
]
\addplot [fill=orange!80, draw=cyan!50!black] table [
x expr=\coordindex,
y index=2] {\datatable};
\addplot [fill=cyan!80, draw=cyan!50!black] table [
x expr=\coordindex,
y index=1] {\datatable};
\end{axis}
\end{tikzpicture}
%============================

 \end{document}

答案1

减少时width您需要enlarge x limits明智地调整:

示例输出

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{filecontents} 
\begin{document}

%==========================
\begin{filecontents}{data0.csv}
{Product-Code},{Total parts},{Active parts}
{0728}, 2414,0
{0510},9931,1510
\end{filecontents}
%===========================
\pgfplotstableread[col sep=comma]{data0.csv}{\datatable}

\begin{tikzpicture}
  \begin{axis}[height=6cm, width=5.5 cm,
    % grid=both,
    ybar,
    enlarge y limits={value=0.3, upper},
    enlarge x limits=0.4,
    bar width=20pt,
    xtick=data,
    x tick label style={major tick length=0pt},
    xticklabels=data,
    xticklabels from table={\datatable}{[index]0},
    nodes near coords, every node near coord/.append style={font=\footnotesize, /pgf/number format/fixed, /pgf/number format/1000 sep = \thinspace },
    title={Number of parts per product code},
    xlabel={PRODUCT CODE},
    ymin=0,
    ytick style={draw=none},
    scaled y ticks = false,
    yticklabels={\empty},
    legend style={nodes=right},
    legend pos= north west,
    legend entries={ Active, Total},
    ]
    \addplot [fill=orange!80, draw=cyan!50!black] table [
    x expr=\coordindex,
    y index=2] {\datatable};
    \addplot [fill=cyan!80, draw=cyan!50!black] table [
    x expr=\coordindex,
    y index=1] {\datatable};
  \end{axis}
\end{tikzpicture}
%============================

\end{document}

答案2

嗯,虽然不是那么丑,但也不太优雅。然而,你的宽度才是真正的罪魁祸首。

由于您使用的\coordindex是 x 值

\documentclass{standalone}
\usepackage{pgfplotstable}

\pgfplotstableread[col sep=comma]{
{Product-Code},{Total parts},{Active parts}
0728, 2414,0
0510,9931,1510
}{\datatable}

\pgfplotstablegetrowsof{\datatable}
\let\myrowno\pgfplotsretval

\begin{document}



\begin{tikzpicture}
\begin{axis}[height=6cm, 
            width=12 cm,
            ybar,
            enlarge y limits={value= 0.3, upper},
            %enlarge x limits=true,
            bar width=20pt,
            xtick={0,...,\myrowno},xmax=\myrowno,xmin=-1,
            x tick label style={major tick length=0pt},
            xticklabels=data,
            xticklabels from table={\datatable}{[index]0},
            nodes near coords, 
            every node near coord/.append style={
                font=\footnotesize, /pgf/number format/fixed,
                /pgf/number format/1000 sep = \thinspace 
                },
            title={Number of parts per product code},
            xlabel={PRODUCT CODE},
            ymin=0,
            ytick style={draw=none},
            scaled y ticks = false,
            yticklabels={\empty},
            legend style={nodes=right},
            legend pos= north west,
            legend entries={ Active, Total},
]
\addplot [fill=orange!80, draw=cyan!50!black] table [x expr=\coordindex,y index=2] {\datatable};
\addplot [fill=cyan!80, draw=cyan!50!black] table [x expr=\coordindex,y index=1] {\datatable};
\end{axis}
\end{tikzpicture}
 \end{document}

在此处输入图片描述

相关内容