问题
我有一组来自.dat 文件的数据,其中每个值可以是“历史”或“预测”。
year emision data_type
2008 9.24309 hist
2010 8.50724 hist
2012 8.06490 hist
2014 7.84779 hist
2016 7.22237 pred
2018 6.71319 pred
2020 6.26255 pred
我需要在同一张图中绘制一条实线,表示历史数据,一条虚线,表示预测数据。下面是我想要的示例图。
我有检查这个答案但我不知道如何使用文件中的数据来应用它。
我拥有的:
以下是代码:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{lipsum}
%\pgfplotstableread{data/example_data.dat}\datatable
\pgfplotstableread{
year emision data_type
2008 9.24309 hist
2010 8.50724 hist
2012 8.06490 hist
2014 7.84779 hist
2016 7.22237 pred
2018 6.71319 pred
2020 6.26255 pred
}\datatable
\begin{document}
\lipsum[1]
\begin{figure}[t]
\centering
\begin{tikzpicture}
\pgfplotsset{every axis legend/.append style={
at={(0.5,-0.1)},
anchor=north}}
\begin{axis}[
width=\textwidth,
height=\axisdefaultheight,
legend columns=4,
grid=major,
cycle list name=exotic,
]
\addplot table[y=emision] from \datatable;
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
提前致谢!
答案1
一个粗鲁的解决方案:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{lipsum}
%\pgfplotstableread{data/example_data.dat}\datatable
\pgfplotstableread{
year emission data_type
2008 9.24309 hist
2010 8.50724 hist
2012 8.06490 hist
2014 7.84779 hist
2016 7.22237 pred
2018 6.71319 pred
2020 6.26255 pred
}\datatable
\begin{document}
\lipsum[1]
\begin{figure}[t]
\centering
\begin{tikzpicture}
\pgfplotsset{every axis legend/.append style={
at={(0.5,-0.1)},
anchor=north}}
\begin{axis}[
width=\textwidth,
height=\axisdefaultheight,
legend columns=4,
grid=major,
cycle list name=exotic,
]
\addplot table[y=emission, x=year, restrict x to domain=2008:2014] from \datatable;
\addplot +[dashed] table[y=emission, x=year, restrict x to domain=2014:2020] from \datatable;
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}