pgfplots 条形图仅显示第一个 x 刻度

pgfplots 条形图仅显示第一个 x 刻度

我有以下代码:

\begin{figure}
\pgfplotsset{ every axis/.append style={font=\tiny}}
\centering

\begin{tikzpicture}

\pgfplotstableread[col sep=comma,header=false]{test.txt}\mydata;

  \begin{axis}[
        ybar=0pt,
        bar width=4pt,
        ymajorgrids=false,
        no markers,
        ybar, 
     bar shift=0.3pt,
        enlarge x limits=0.05,
        ymin=0,
         major x tick style = transparent,
        xticklabels from table={\mydata}{0},
     xtick=data,
        x tick label style={rotate=90,anchor=east},
        ymajorgrids = false,
        bar width=2mm, 
        width=20cm, 
     height=10cm, 
        ylabel={\%},
       /pgf/number format/.cd,fixed,fixed zerofill,precision=1,/tikz/.cd  ]
\pgfplotsinvokeforeach{0,...,10}{\addplot table [select row=#1, x expr=#1] {\mydata};}

\end{axis}
\end{tikzpicture}
\end{figure}

test.tx 文件是

a, 1
b, 2
c, 3
d, 4
e, 5
f, 6
g, 7
h, 8
i, 9
j, 10
k, 11

在结果图中显示了所有 11 个条形图,但仅显示 x 轴刻度“a”,我已尽力解决问题但无济于事,非常欢迎任何帮助

答案1

xtick=data仅使用第一个\addplot命令来确定刻度位置。您应该改用xtick={0,...,10}

\documentclass[tikz, border=5pt]{standalone}
\usepackage{pgfplots, pgfplotstable}
\begin{document}
\begin{tikzpicture}

\pgfplotstableread[col sep=comma,header=false]{
a, 1
b, 2
c, 3
d, 4
e, 5
f, 6
g, 7
h, 8
i, 9
j, 10
k, 11
}\mydata;

\pgfplotsset{
    select row/.style={
        x filter/.code={\ifnum\coordindex=#1\else\def\pgfmathresult{}\fi}
    }
}

  \begin{axis}[
        ybar=0pt,
        bar width=4pt,
        ymajorgrids=false,
        no markers,
        ybar, 
     bar shift=0.3pt,
        enlarge x limits=0.05,
        ymin=0,
         major x tick style = transparent,
        xticklabels from table={\mydata}{0},
     xtick={0,...,10},
        x tick label style={rotate=90,anchor=east},
        ymajorgrids = false,
        bar width=2mm, 
        width=10cm, 
     height=10cm, 
        ylabel={\%},
       /pgf/number format/.cd,fixed,fixed zerofill,precision=1,/tikz/.cd  ]
\pgfplotsinvokeforeach{0,...,10}{\addplot table [select row=#1, x expr=#1] {\mydata};}

\end{axis}
\end{tikzpicture}
\end{document}

相关内容