Wie zeichnet man eine Kurve mit dem Datum auf der Abszisse?

Wie zeichnet man eine Kurve mit dem Datum auf der Abszisse?

Ich versuche mit der Bibliothek {datavisualization} die Entwicklung der Daten in Abhängigkeit vom aktuellen Datum nachzuvollziehen.

Ich weiß nicht, wie ich das Datum auf die Abszisse bekomme.

Im folgenden Beispiel ist das Datum im französischen Format TT/MM/JJJJ

% !TeX program =Xelatex+shell-escape
% !TeX encoding = utf8
%  !TeX spellcheck = fr 

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}[overwrite]{plaquette.csv}
date,plaquettes
30/10/2023,1200000
02/10/2023,1229000
06/11/2023,980000
09/11/2023,570000
13/11/2023,308000
16/11/2023,259000
20/11/2023,117000
23/11/2023,65000
27/11/2023,21000
30/11/2023,39000
04/12/2023,590000
07/12/2023,1034000
11/12/2023,1960000
14/12/2023,1339000
18/12/2023,990000
02/01/2024,800000
06/01/2024,520000
12/01/2024,350000
\end{filecontents*}

\usepackage{tikz}
\usetikzlibrary {datavisualization}
\usepackage{csvsimple-l3}

\begin{document}

\csvautotabular{plaquette.csv}

\end{document}

Antwort1

Hier sind zwei Versionen, eine als read from file, die andere aus dem dataAbschnitt, mit demselben Ergebnis.

Ergebnis

aus Datei lesen

\documentclass[10pt,border=3mm,tikz]{standalone}
\usetikzlibrary{datavisualization}  %

% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\newcounter{NX}

\makeatletter
    % ~~~ declaring the dateX-format needed for the visualizer ~~~~~~~~~~~~~~~~~~
    \pgfdeclaredataformat{dateX}%                   <<<
        {}%                                         no catcode changes
        {\xdef\label@list{}}%                       initialise a global label list
        {#1,#2}%                                    data format as used below
        {%                                          which format?
            \pgfmathaddtocounter{NX}{1}%            advance counter
            \pgfkeyssetvalue{/data point/x}{\theNX}%store counters result as x-data 
            \pgfkeyssetvalue{/data point/y}{#2}%    the y-data (Value)          
            \xdef\label@list{\label@list,"#1"}%     append label to list
            \pgfdatapoint%                          save assigned x and y
        }%
        {}{}

    % ~~~ typesetting the x-axis: putting labels instead of 1,2,3, ... ~~~~~~~~
    \def\ticksfromlabellist#1{%
        \pgfmathparse{{\label@list}[#1]}%           it's 30/10/2023 for the first tick etc.
        \pgfmathresult
    }    
\makeatother

\def\dvbb{data visualization bounding box}  % shortcut to this box

% ~~~ data file ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\begin{filecontents*}[overwrite]{plaquette.csv}
    %date,plaquettes
    30/10/2023,1200000
    02/10/2023,1229000
    06/11/2023,980000
    09/11/2023,570000
    13/11/2023,308000
    16/11/2023,259000
    20/11/2023,117000
    23/11/2023,65000
    27/11/2023,21000
    30/11/2023,39000
    04/12/2023,590000
    07/12/2023,1034000
    11/12/2023,1960000
    14/12/2023,1339000
    18/12/2023,990000
    02/01/2024,800000
    06/01/2024,520000
    12/01/2024,350000
\end{filecontents*}

% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\begin{document}
 \begin{tikzpicture}
    % ~~~ let's plot some data ~~~~~~~~~~~~~~~~~~~~
    \datavisualization
    [
        scientific axes=clean,
        visualize as line,
        % ~~~ axes ~~~~~~~~~~~~~~~~~~~~~~~~
        x axis={
            label=Date,         % name it
            ticks={ 
                step=2,         % or use step=1 to show all labels
                tick typesetter/.code=\ticksfromlabellist{##1}, % typesetting labels
                node style={rotate=90,anchor=east,teal},        % rotating etc. 
            }, 
            min value=0,        % if you like
        },  
        y axis={
            label=Plaquettes,   % name it
            min value = 0, max value=2e6
        },
        % ~~~ grids ~~~~~~~~~~~~~~~~~~~~~~~
        all axes=grid,      
    ] data [format=dateX, read from file=plaquette.csv]
    info{
        % ~~~ Title ~~~~~~~~~~~
        \node[purple,anchor=south] at (\dvbb.north) {Example};
    };
 \end{tikzpicture}
\end{document}

Datenabschnitt

Gleicher Code, aber Daten verschoben.

\documentclass[10pt,border=3mm,tikz]{standalone}
\usetikzlibrary{datavisualization}  %

% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\newcounter{NX}

\makeatletter
    % ~~~ declaring the dateX-format needed for the visualizer ~~~~~~~~~~~~~~~~~~
    \pgfdeclaredataformat{dateX}%                   <<<
        {}%                                         no catcode changes
        {\xdef\label@list{}}%                       initialise a global label list
        {#1,#2}%                                    data format as used below
        {%                                          which format?
            \pgfmathaddtocounter{NX}{1}%            advance counter
            \pgfkeyssetvalue{/data point/x}{\theNX}%store counters result as x-data 
            \pgfkeyssetvalue{/data point/y}{#2}%    the y-data (Value)          
            \xdef\label@list{\label@list,"#1"}%     append label to list
            \pgfdatapoint%                          save assigned x and y
        }%
        {}{}

    % ~~~ typesetting the x-axis: putting labels instead of 1,2,3, ... ~~~~~~~~
    \def\ticksfromlabellist#1{%
        \pgfmathparse{{\label@list}[#1]}%           it's 30/10/2023 for the first tick etc.
        \pgfmathresult
    }    
\makeatother

\def\dvbb{data visualization bounding box}  % shortcut to this box

% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\begin{document}
 \begin{tikzpicture}
    % ~~~ let's plot some data ~~~~~~~~~~~~~~~~~~~~
    \datavisualization
    [
        scientific axes=clean,
        visualize as line,
        % ~~~ axes ~~~~~~~~~~~~~~~~~~~~~~~~
        x axis={
            label=Date,         % name it
            ticks={ 
                step=2,         % or use step=1 to show all labels
                tick typesetter/.code=\ticksfromlabellist{##1}, % typesetting labels
                node style={rotate=90,anchor=east,teal},        % rotating etc. 
            }, 
            min value=0,        % if you like
        },  
        y axis={
            label=Plaquettes,   % name it
            min value = 0, max value=2e6
        },
        % ~~~ grids ~~~~~~~~~~~~~~~~~~~~~~~
        all axes=grid,      
    ] data [format=dateX] {     % declared above, to meet this data format
%       date,plaquettes         % skipping header
        30/10/2023,1200000
        02/10/2023,1229000
        06/11/2023,980000
        09/11/2023,570000
        13/11/2023,308000
        16/11/2023,259000
        20/11/2023,117000
        23/11/2023,65000
        27/11/2023,21000
        30/11/2023,39000
        04/12/2023,590000
        07/12/2023,1034000
        11/12/2023,1960000
        14/12/2023,1339000
        18/12/2023,990000
        02/01/2024,800000
        06/01/2024,520000
        12/01/2024,350000
    }
    info{
        % ~~~ Title ~~~~~~~~~~~
        \node[purple,anchor=south] at (\dvbb.north) {Example};
    };
 \end{tikzpicture}
\end{document}

verwandte Informationen