1

I have the following code, which builds in part off the answer here.

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.8}

%colors
\usepackage{color} % colors
\usepackage{xcolor} 
\definecolor{colourone}{RGB}{22,131,198}
\definecolor{colourtwo}{RGB}{202,211,43}

\begin{document}

\pgfplotstableread[col sep=comma]{
date,vix
2017-01-01,13.0875
2017-01-08,11.9225
2017-01-15,11.416
2017-01-22,12.1675
2017-01-29,10.972
2017-02-05,11.716
2017-02-12,11.168
2017-02-19,11.406
2017-02-26,11.6225
2017-03-05,12.064
2017-03-12,11.702
2017-03-19,11.554
2017-03-26,12.54
2017-04-02,11.872
2017-04-09,12.464
2017-04-16,15.2125
2017-04-23,14.558
2017-04-30,10.726
}\chartthree

\pgfplotsset{/pgfplots/ybar legend/.style={
        /pgfplots/legend image code/.code={%
            \draw[ ##1,/tikz/.cd,yshift=-0.25em]
            (0cm,0cm) rectangle (0.6em,0.6em);},},
}

\begin{tikzpicture}
\begin{axis}[
x=0.5mm,
axis lines=left,
enlarge x limits={0,upper},
enlarge y limits={0.1, upper},
stack plots = y,
%
% y ticks style and label
ylabel={Index},
ytick distance = 5,
y tick label style={/pgf/number format/.cd, fixed, fixed zerofill, precision=1, /tikz/.cd},
ymin=5,
%
% x axis ticks and style
date coordinates in=x,
%xtick=data,
xticklabels from table={\chartthree}{date}, 
xticklabel={\pgfcalendar{tickcal}{\tick}{\tick}{\pgfcalendarshorthand{\month-\year}{.}}},
x tick label style = {rotate=90},
%
% done with the axis, now the plots
\addplot [colourone, fill = colourone]
table [x=date,y=vix] from \chartthree \closedcycle;
\end{axis}
\end{tikzpicture}

\end{document}

The goal is for my x-axis to have the labels in the form mm-yy where mm indicates the short text form of the month (e.g. Jan, Aug) and yy the last two digits of the year. So the labels should be in the form Jan-20, for instance.

What am I doing wrong?

Also, I'd like to be able to define the interval distance (only every 8th date should show).

2
  • 2
    mm has only 2 chars but the abbreviation of months has 3 chars. – manooooh Mar 11 '20 at 3:31
  • 1
    One thing that you are doing wrong is to miss a ] after x tick label style = {rotate=90}. – user194703 Mar 11 '20 at 3:44
2

As far as I can see, \pgfcalendarshorthand does not provide us with means to select only the two last digits of the year. So we need to do that ourselves.

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calendar}
\usepackage{pgfplotstable}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.16}

%colors
\definecolor{colourone}{RGB}{22,131,198}
\definecolor{colourtwo}{RGB}{202,211,43}

\begin{document}

\pgfplotstableread[col sep=comma]{
date,vix
2017-01-01,13.0875
2017-01-08,11.9225
2017-01-15,11.416
2017-01-22,12.1675
2017-01-29,10.972
2017-02-05,11.716
2017-02-12,11.168
2017-02-19,11.406
2017-02-26,11.6225
2017-03-05,12.064
2017-03-12,11.702
2017-03-19,11.554
2017-03-26,12.54
2017-04-02,11.872
2017-04-09,12.464
2017-04-16,15.2125
2017-04-23,14.558
2017-04-30,10.726
}\chartthree

\pgfplotsset{/pgfplots/ybar legend/.style={
        /pgfplots/legend image code/.code={%
            \draw[ ##1,/tikz/.cd,yshift=-0.25em]
            (0cm,0cm) rectangle (0.6em,0.6em);},},
}

\begin{tikzpicture}
\def\pft#1#2#3#4-#5-#6;{\edef\myshortyear{#3#4}}%

\begin{axis}[
x=0.5mm,
axis lines=left,
enlarge x limits={0,upper},
enlarge y limits={0.1, upper},
stack plots = y,
%
% y ticks style and label
ylabel={Index},
ytick distance = 5,
y tick label style={/pgf/number format/.cd, fixed, fixed zerofill, precision=1, /tikz/.cd},
ymin=5,
%
% x axis ticks and style
date coordinates in=x,
%xtick=data,
xticklabels from table={\chartthree}{date}, 
xticklabel={\pgfcalendar{tickcal}{\tick}{\tick}{\pgfcalendarshorthand{m}{.}}%
\expandafter\pft\tick;%
--\myshortyear},
x tick label style = {rotate=90}]
%
% done with the axis, now the plots
\addplot [colourone, fill = colourone]
table [x=date,y=vix] from \chartthree \closedcycle;
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

1
  • Thank you - this was perfect! – Thev Mar 11 '20 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.