所以我在从数据表绘制图表时遇到了一些麻烦。因此,在我桌面上的一个文件夹中,我有大量不同的 LaTeX 数据表,这些表是我使用 Excel2LaTeX 从 Microsoft Excel 转换而来的。我不仅想绘制这些数据集的图表,还想在实际文档中隐藏数据表,因为它们太大了。我觉得我应该使用包pgfplotstable
,但我不知道如何使用。我也不知道如何在文档中隐藏表格并只留下图表。
示例表:
\begin{table}[htbp]
\centering
\caption{Air, Surface, and Bottom Temperature}
\begin{tabular}{rrrr}
\toprule
Date & Air (°C) & Surface (°C) & Bottom (°C) \\
\midrule
19-Sep-13 & 18.5 & & \\
26-Sep-13 & & & \\
3-Oct-13 & 19.2 & 18.7 & 20.3 \\
10-Oct-13 & 18.3 & 18.8 & 13 \\
17-Oct-13 & 20.2 & 18.9 & 16 \\
24-Oct-13 & 18.1 & 18.1 & 16.6 \\
31-Oct-13 & 22.9 & 16.9 & 16 \\
7-Nov-13 & 23 & 18.5 & 16 \\
14-Nov-13 & 20.9 & 18.4 & 16.6 \\
21-Nov-13 & & & \\
28-Nov-13 & & & \\
5-Dec-13 & 13.7 & 15.5 & 16.6 \\
12-Dec-13 & 16.8 & 15.8 & 12.5 \\
19-Dec-13 & & & \\
26-Dec-13 & & & \\
2-Jan-14 & & & \\
9-Jan-14 & 14.6 & 15.5 & 15 \\
16-Jan-14 & 27.3 & 16 & 14.6 \\
23-Jan-14 & 16.2 & 15.9 & \\
30-Jan-14 & 15.7 & 15.8 & 15 \\
6-Feb-14 & & & \\
13-Feb-14 & 17.9 & 16 & 15 \\
20-Feb-14 & 23.6 & 17.5 & 14.4 \\
27-Feb-14 & & & \\
6-Mar-14 & 17.7 & 17.1 & 14.7 \\
13-Mar-14 & 16.7 & 17.7 & 15.6 \\
20-Mar-14 & 17.1 & 17.5 & 15.2 \\
27-Mar-14 & 13.5 & 14.2 & 12 \\
\bottomrule
\end{tabular}
\label{tab:addlabel}
\end{table}
现在它运行正常,并且表格显示没有错误,但我的主要问题是如何在从文档中物理省略表格的同时绘制数据图表?
答案1
\documentclass[tikz, border=2]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\begin{filecontents}{data.csv}
Date,Air,Surface,Bottom
19-Sep-13, 18.5, ,
3-Oct-13 , 19.2, 18.7, 20.3
10-Oct-13, 18.3, 18.8, 13
17-Oct-13, 20.2, 18.9, 16
24-Oct-13, 18.1, 18.1, 16.6
31-Oct-13, 22.9, 16.9, 16
7-Nov-13 , 23 , 18.5, 16
14-Nov-13, 20.9, 18.4, 16.6
5-Dec-13 , 13.7, 15.5, 16.6
12-Dec-13, 16.8, 15.8, 12.5
9-Jan-14 , 14.6, 15.5, 15
16-Jan-14, 27.3, 16 , 14.6
23-Jan-14, 16.2, 15.9,
30-Jan-14, 15.7, 15.8, 15
13-Feb-14, 17.9, 16 , 15
20-Feb-14, 23.6, 17.5, 14.4
6-Mar-14 , 17.7, 17.1, 14.7
13-Mar-14, 16.7, 17.7, 15.6
20-Mar-14, 17.1, 17.5, 15.2
27-Mar-14, 13.5, 14.2, 12
\end{filecontents}
\pgfplotstableread[col sep=comma]{data.csv}\datatable
\makeatletter
\pgfplotsset{
/pgfplots/xticklabels/.code n args={3}{%
\pgfplotstableread[#3]{#1}\coordinate@table
\pgfplotstablegetcolumn{#2}\of{\coordinate@table}\to\pgfplots@xticklabels
\let\pgfplots@xticklabel=\pgfplots@user@ticklabel@list@x
}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width = \linewidth,
grid = major,
xlabel = Dates,
ylabel = Temperature ($^\circ$C),
xticklabels = {data.csv}{Date}{col sep=comma},
xticklabel style = {rotate=90},
xtick = data,
]
\addplot table[x expr=\coordindex,y=Air]{\datatable};
\addplot table[x expr=\coordindex,y=Surface]{\datatable};
\addplot table[x expr=\coordindex,y=Bottom]{\datatable};
\legend{Air,Surface,Bottom}
\end{axis}
\end{tikzpicture}
\end{document}