我有以下代码,部分构建于答案之上这里。
\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}
我的目标是让 x 轴的标签采用以下形式:mm-yy
表示mm
月份的短文本形式(例如,Jan、Aug)和yy
年份的最后两位数字。因此,标签应采用例如 Jan-20 的形式。
我究竟做错了什么?
另外,我希望能够定义间隔距离(仅应显示每 8 个日期)。
答案1
据我所知,\pgfcalendarshorthand
它没有提供仅选择年份最后两位数字的方法。所以我们需要自己做。
\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}