1

I have inputs with tuple (date, percent) in a csv file: (The date are integers with 44 000 <-> 18 June 2020)

date,pct
44755,0.01925374
44756,0.01925374
44757,0.01925374
[...]
44768,0.018989264
44769,0.018989264

which I load and plot with the following latex code.

\documentclass[11pt, a4paper]{article}
\usepackage{pgfplots}
\usepackage{tikz} 


\begin{document}


    \begin{tikzpicture}

        \centering   
        \begin{axis}[
            width = \textwidth,
            height = 0.5\textheight,
            xlabel = {$$},
            xtick = data,
            xticklabel style={ rotate=+45},
             ymin = 0.01, ymax =0.02,       
            ytick distance = 0.01,
                ]
         % Plot data from a file
        \addplot[
            blue,
        ] table [x=date, y=pct, col sep = comma]{data.csv};
        \addlegendentry{Data} 
        \end{axis}
     
    \end{tikzpicture}


\end{document}

How can I format the xaxis for dates "dd-mmm-yy" and yaxis as percentage "0.00%"

1
  • Not related: \centering at this place does nothing sensible and you should remove it. The pgfplots package` already loads the tikz package, so there is no need to load it separately. xlabel = {$$} will let the above code snippet break, so you might want to place something between the two $. Jul 19, 2023 at 15:11

1 Answer 1

1

You can make use of the xticklabel and yticklabel options to put into the labels whatever you wish. First of all, you can remove the scientific notation with scaled ticks=false.

For the percentages, you could simply multiply 100 with \tick which is the variable that stores the current tick value and output the result of this calulation (with a percent sign added to it) to the label via yticklabel (see: pgfplots with percentage on the axis).

To get a proper date formatting, you could make use of the calendar library that comes with TikZ. With the command \pgfcalendarjuliantodate you can calculate a date (more correctly, three values for year, month and day respectively that can be stored in three commands) from a given integer. We can again grab this integer via the \tick value (which we need to convert to an integer) and add the magic number of 2415019 to it to sync the value of 44000 with the date of 2020-06-18 (I used \numexpr for the calculation because TikZ would run into a dimention-too-large error, but we only need integer calculation here anyways which can be done by TeX directly).

\documentclass[]{article}
\usepackage{pgfplots}
\usetikzlibrary{calendar}
\pgfplotsset{compat=newest}

\begin{filecontents}{data.csv}
date,pct
44755,0.01925374
44756,0.01925374
44757,0.01925374
44768,0.018989264
44769,0.018989264
\end{filecontents}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            width=\textwidth,
            height=0.5\textheight,
            xlabel={$x$},
            xtick=data,
            xticklabel style={
                rotate=45,
                anchor=north east
            },
            xticklabel={%
                \pgfmathparse{int(\tick)}%
                \pgfcalendarjuliantodate{\numexpr\pgfmathresult+2415019\relax}%
                    {\myyear}{\mymonth}{\myday}%
                \myday-\pgfcalendarmonthshortname{\mymonth}-\myyear%
            },
            ymin=0.01, ymax=0.02,       
            ytick distance=0.01,
            yticklabel={%
                \pgfmathparse{\tick*100}%
                \pgfkeys{/pgf/number format/fixed zerofill, /pgf/number format/precision=2}%
                \pgfmathprintnumber{\pgfmathresult}\,\%%
            },
            scaled ticks=false,
        ]
        % Plot data from a file
        \addplot[
            blue,
        ] table [x=date, y=pct, col sep=comma] {data.csv};
        \addlegendentry{Data} 
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

2
  • Hello, thank you, what is the use of \pgfplotsset{compat=newest} ? Jul 20, 2023 at 9:09
  • 1
    @stackoverflower Over time, PGFPlots developed its syntax. In order to preserve backwards compatibility, the key compat was introduced. Per default, PGFPlots will choose a most compatible setup that, however, does not fully support the newest syntax. Hence it is always advisable to set this key, for example to newest, to get all the current features provided by the package. See section 2.2.2 in the current PGFPlots manual. Jul 20, 2023 at 9:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .