![如何绘制以日期为横坐标的曲线?](https://linux22.com/image/472641/%E5%A6%82%E4%BD%95%E7%BB%98%E5%88%B6%E4%BB%A5%E6%97%A5%E6%9C%9F%E4%B8%BA%E6%A8%AA%E5%9D%90%E6%A0%87%E7%9A%84%E6%9B%B2%E7%BA%BF%EF%BC%9F.png)
我正在尝试使用库 {datavisualization} 来跟踪取决于当前日期的数据的演变。
我不知道如何获取横坐标上的日期。
以下示例中的日期采用法语格式 dd/mm/yyyy
% !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}
答案1
这里有两个版本,一个是read from file
,另一个来自data
部分,结果相同。
从文件读取
\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}
数据部分
代码相同,但数据已移动。
\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}