我正在尝试从类似于以下的 csv 文件创建一个简单的图:
date,anumber
01/09/2021,123
...
我从网上复制了一些代码:
\documentclass[10pt,a4paper]{article}
\usepackage[a4paper,margin=0.5in]{geometry}
\usepackage{csvsimple}
\usepackage{siunitx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\sisetup{
round-mode = places,
round-precision = 2,
}
\begin{document}
\begin{figure}[h!]
\begin{center}
\begin{tikzpicture}
\begin{axis}[
width=15cm, height=8cm,
grid=major,
grid style={dashed,gray!30},
xlabel=Gaming Day,
ylabel=USD,
legend style={at={(0.5,-0.2)},anchor=north},
xmode=log,
log ticks with fixed point,
xtick=data,
ybar, %added here
]
\addplot[fill] table[x index={0},y index={1}, col sep=comma] {myfile.csv};
\end{axis}
\end{tikzpicture}
\caption{Performance Comparison Histogram}
\end{center}
\end{figure}
\end{document}
但是我收到一个错误:“Package PGF Math Error: Could not parse input '01/09/2021'”,这是有道理的,因为这不是一个数字。我想要实现的是一个图表,其中日期标记 x 轴,数字沿 y 轴绘制。抱歉,如果这是基本的,但我该怎么做呢?
答案1
像这样:
\documentclass[10pt,a4paper]{article}
\usepackage[a4paper,margin=0.5in]{geometry}
\usepackage{csvsimple}
\usepackage{siunitx}
\usepackage{pgfplots}
\usetikzlibrary{
pgfplots.dateplot,
}
\pgfplotsset{compat=1.12}
\sisetup{
round-mode = places,
round-precision = 2,
}
\begin{filecontents}{file1.csv}
date,anumber
2021-10-07, 123
2021-10-08, 345
2021-10-09, 123
2021-10-10, 345
\end{filecontents}
\begin{document}
\begin{figure}[h!]
\begin{center}
\begin{tikzpicture}
\begin{axis}[
date coordinates in=x,
table/col sep=comma,
xtick=data,
xticklabel style={
rotate=90,
anchor=near xticklabel,
},
xticklabel=\day.\month.\year\,
width=15cm, height=8cm,
grid=major,
grid style={dashed,gray!30},
xlabel=Gaming Day,
ylabel=USD,
]
\addplot table[x=date,y=anumber] {file1.csv};;
\end{axis}
\end{tikzpicture}
\caption{Performance Comparison Histogram}
\end{center}
\end{figure}
\end{document}