为什么我的 x 轴上的标签打印了两次?

为什么我的 x 轴上的标签打印了两次?

我需要它来\usepackage[top=3cm,left=2.2cm, right=2.2cm, bottom=1.7cm]{geometry}完成我的整个论文。

但是,当我将其添加到我bar chart的 x 轴时,它会显示两次每个条目。

在此处输入图片描述

我的 MWE:

\documentclass[12pt,oneside]{book} 

\usepackage{siunitx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{arrows.meta,
                backgrounds,
                calc, chains,
                decorations.pathreplacing,
                fit,
                matrix,
                positioning,
                }
 \usepackage[top=3cm,left=2.2cm, right=2.2cm, bottom=1.7cm]{geometry} 

\usepackage{tabularx,seqsplit, caption} %for table spacing to second row
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,calc, positioning,matrix,fit,calc, arrows.meta,
                backgrounds,
                chains}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}              

\begin{document}

\begin{figure}[h!]
\pgfplotsset{width=\textwidth,height=8cm,compat=1.16,
/pgfplots/aan ybar legend/.style={
/pgfplots/legend image code/.code={
\draw [##1,/tikz/.cd,bar width=4.5pt,yshift=-0.3em,bar shift=1pt,yscale=2]
plot coordinates {(0cm,0.4em) };}}}



\begin{tikzpicture}
\begin{axis}[symbolic x coords={SuperCollider,Puredata,Other,prerecorded,Max/MSP,Csound,ChucK,Matlab,Built-in MIDI},
    x tick label style={anchor=north east,rotate=30},
    ylabel=Number,
    xlabel=testting,
    enlargelimits=0.05,
    legend style={at={(0.5,0.95)},
    anchor=north,legend columns=-1,
    /tikz/every even column/.append style={column sep=1.2em}},
    ybar,aan ybar legend
]


\addplot [fill=ForestGreen!30,draw=ForestGreen]
    coordinates {(SuperCollider,9) (Puredata,7)(Other,5) (prerecorded,3) (Max/MSP,2) (Csound,2) (ChucK,1) (Matlab,0) (Built-in MIDI,0)};
\addplot [fill=Purple!30,draw=Purple]
    coordinates {(SuperCollider,3) (Puredata,5)(Other,3) (prerecorded,3) (Max/MSP,2) (Csound,0) (ChucK,0) (Matlab,2) (Built-in MIDI,6)};
\end{axis}
\end{tikzpicture}
\end{figure}

%  https://smartech.gatech.edu/bitstream/handle/1853/44402/Bearman_ICAD2012.pdf



\end{document}

答案1

为偶数刻度设置空标签。

\begin{tikzpicture}
\begin{axis}[
  symbolic x coords={
    {},SuperCollider,,
    Puredata,,
    Other,,
    prerecorded,,
    Max/MSP,,
    Csound,,
    ChucK,,
    Matlab,,
    Built-in MIDI
  },

答案2

一个更简单的解决方案是简单地添加xtick distance=1axis选项中(参见下面的代码。请注意,我已将您的代码简化为必要部分)。

% used PGFPlots v1.17
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        compat=1.17,
        width=17cm,
        height=8cm,
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        symbolic x coords={
            SuperCollider,
            Puredata,
            Other,
            prerecorded,
            Max/MSP,
            Csound,
            ChucK,
            Matlab,
            Built-in MIDI%
        },
        x tick label style={anchor=north east,rotate=30},
        ylabel=Number,
        xlabel=testing,
        enlargelimits=0.05,
        ybar,
        xtick distance=1,       % <-- added
    ]
        \addplot coordinates {
            (SuperCollider,9)
            (Puredata,7)
            (Other,5)
            (prerecorded,3)
            (Max/MSP,2)
            (Csound,2)
            (ChucK,1)
            (Matlab,0)
            (Built-in MIDI,0)
        };
        \addplot coordinates {
            (SuperCollider,3)
            (Puredata,5)
            (Other,3)
            (prerecorded,3)
            (Max/MSP,2)
            (Csound,0)
            (ChucK,0)
            (Matlab,2)
            (Built-in MIDI,6)
        };
    \end{axis}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果


这里我提出了一种不使用 的替代解决方案symbolic coords。我个人不喜欢它,因为它需要输入更多内容,因此更容易出错。以下代码的结果与上面的完全相同。

% used PGFPlots v1.17
    % moved data to a file with named headers
    \begin{filecontents*}{data.txt}
        x               y1  y2
        SuperCollider   9   3
        Puredata        7   5
        Other           5   3
        prerecorded     3   3
        Max/MSP         2   2
        Csound          2   0
        ChucK           1   0
        Matlab          0   2
        {Built-in MIDI} 0   6
    \end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        compat=1.17,
        width=17cm,
        height=8cm,
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        x tick label style={anchor=north east,rotate=30},
        ylabel=Number,
        xlabel=testing,
        enlargelimits=0.05,
        ybar,
        % ---------------------------------------------------------------------
        % added stuff
        % ---------------------------------------------------------------------
        % use the coordinate index as x coordinate for the `\addplot`s
        table/x expr={\coordindex},
        % use the values from the column named x as labels for the x axis
        xticklabels from table={data.txt}{x},
        % use the data as positions for the x ticks
        xtick=data,
    ]
        \addplot table [y=y1] {data.txt};
        \addplot table [y=y2] {data.txt};
    \end{axis}
\end{tikzpicture}
\end{document}

相关内容