条形图的简单问题

条形图的简单问题

我正在尝试制作一个简单的条形图,但无法删除 x 轴图例中的其他元素。图形函数在 x 轴图例的两端添加了“2001”和“2022”。我做错了什么?

在此处输入图片描述

前言:

\usepackage{pgfplots}
\pgfplotsset{width=14cm,compat=1.15}
\begin{tikzpicture}
    \begin{axis}[
        ybar, 
        %enlargelimits=0.15,
        xlabel=Year,
        ylabel=Number of publications,
        % remove comma in xticklabels
        xticklabel style={rotate=270, /pgf/number format/set thousands separator={}},
        % one tick every year
        xtick distance=1
        ]
        \addplot table {
            Date GDP 
            2002    1
            2003    0
            2004    0
            2005    0
            2006    1
            2007    0
            2008    0
            2009    1
            2010    2
            2011    3
            2012    3
            2013    2
            2014    4
            2015    2
            2016    3
            2017    3
            2018    3
            2019    2
            2020    7
            2021    8
        };
        % if you have the file, you can do
        % \addplot table {datafile.csv};
    \end{axis}
\end{tikzpicture}

答案1

只需按照已经给出的提示,查看%<<==,并将其放置xtick-option在中的某处axis-definition

\documentclass[11pt,oneside,openany]{book}% whatever your class may be
\usepackage{pgfplots}
\pgfplotsset{width=14cm,compat=1.15}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ybar, 
        %enlargelimits=0.15,
        xlabel=Year,
        ylabel=Number of publications,
        % remove comma in xticklabels
        xticklabel style={rotate=270, /pgf/number format/set thousands separator={}},
        xtick={2002,...,2021},% <<==
        % one tick every year
        xtick distance=1
        ]
        \addplot table {
            Date GDP 
            2002    1
            2003    0
            2004    0
            2005    0
            2006    1
            2007    0
            2008    0
            2009    1
            2010    2
            2011    3
            2012    3
            2013    2
            2014    4
            2015    2
            2016    3
            2017    3
            2018    3
            2019    2
            2020    7
            2021    8
        };
        % if you have the file, you can do
        % \addplot table {datafile.csv};
    \end{axis}
\end{tikzpicture}
\end{document}

结果

结果

相关内容