我怎样才能使 dateplot x-ticks 步骤达到 4 年?

我怎样才能使 dateplot x-ticks 步骤达到 4 年?

我目前正在尝试为巴伐利亚州创建一个漂亮的选举图表。这是我现在得到的:

在此处输入图片描述

LaTeX 源

包含 Makefile 和数据的完整源代码是在 Github 上。这是没有 csv 文件的 LaTeX 部分:

\documentclass[varwidth=false, border=2pt]{standalone}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf
\usepackage[margin=2.5cm]{geometry} %layout

\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\usetikzlibrary{pgfplots.dateplot}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
            date coordinates in=x,
            xticklabel={\year},
            x tick label style={align=center, rotate=45},
            yticklabel=$\pgfmathprintnumber{\tick}$\,\%,
            date ZERO=1946-06-30,
            xmin={1946-01-01},
            xmax={2010-01-01},
            extra y ticks={5},
            /pgfplots/ytick={0,10,...,100},
            ymin=0, ymax=100,
            width=15cm, height=8cm,     % size of the image
            grid = major,
            grid style={dashed, gray!30},
            legend style={at={(1.15,1)}, anchor=north}
         ]
          \addplot[blue, dashed, mark=triangle*] table [x=Wahltag, y=Wahlbeteiligung, col sep=comma] {landtagswahlen-in-bayern.csv};
          \addplot[black,mark=square*] table [x=Wahltag, y=CSU, col sep=comma] {landtagswahlen-in-bayern.csv};
          \addplot[red,mark=square*] table [x=Wahltag, y=SPD, col sep=comma] {landtagswahlen-in-bayern.csv};
          \addplot[green,mark=square*] table [x=Wahltag, y=GRÜNE, col sep=comma] {landtagswahlen-in-bayern.csv};
          \addplot[yellow,mark=square*] table [x=Wahltag, y=FDP, col sep=comma] {landtagswahlen-in-bayern.csv};
          \legend{Wahlbeteiligung,CSU,SPD,Grüne,FDP}
    \end{axis}
\end{tikzpicture}
\end{document}

问题

我怎样才能将 x 个刻度设为 4 年的步长?

我可以在 csv 文件中为每一年创建一个标签吗?

答案1

我无法让 pgfplot 处理我从他的网站复制的文件,所以我只是注释掉了那部分代码。

普格夫

\documentclass[varwidth=false, border=2pt]{standalone}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf
\usepackage[margin=2.5cm]{geometry} %layout

\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\usetikzlibrary{pgfplots.dateplot}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
            date coordinates in=x,
            xticklabel={\year},
            x tick label style={align=center, rotate=45},
            yticklabel=$\pgfmathprintnumber{\tick}$\,\%,
            date ZERO=1946-06-30,
            xtick={1946-01-01,1950-01-01,1954-01-01,1958-01-01,1962-01-01,1966-01-01,1970-01-01,1974-01-01,
                       1978-01-01,1982-01-01,1986-01-01,1990-01-01,1994-01-01,1998-01-01,2002-01-01,2006-01-01,2010-01-01},
            xmin={1946-01-01},
            xmax={2010-01-01},
            extra y ticks={5},
            /pgfplots/ytick={0,10,...,100},
            ymin=0, ymax=100,
            width=15cm, height=8cm,     % size of the image
            grid = major,
            grid style={dashed, gray!30},
            legend style={at={(1.15,1)}, anchor=north}
         ]
%    \addplot[blue, dashed, mark=triangle*] table [x=Wahltag, y=Wahlbeteiligung, col sep=comma] {landtagswahlen-in-bayern.csv};
%    \addplot[black,mark=square*] table [x=Wahltag, y=CSU, col sep=comma] {landtagswahlen-in-bayern.csv};
%    \addplot[red,mark=square*] table [x=Wahltag, y=SPD, col sep=comma] {landtagswahlen-in-bayern.csv};
%    \addplot[green,mark=square*] table [x=Wahltag, y=GRÜNE, col sep=comma] {landtagswahlen-in-bayern.csv};
%    \addplot[yellow,mark=square*] table [x=Wahltag, y=FDP, col sep=comma] {landtagswahlen-in-bayern.csv};
%    \legend{Wahlbeteiligung,CSU,SPD,Grüne,FDP}
    \end{axis}
\end{tikzpicture}
\end{document}

注意:错误消息如下:

Package pgfplots notification 'compat/show suggested version=true': document ha
s been generated with the most recent feature set (\pgfplotsset{compat=1.10}).

PGFPlots: reading {landtagswahlen-in-bayern.csv}
! Undefined control sequence.
\GenericError  ...                                
                                                    #4  \errhelp \@err@     ...
l.32 ...sep=comma] {landtagswahlen-in-bayern.csv};


The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

相关内容