是否可以制作一个模板,pgfplots
仅通过定义平台时间(在下面的例子中为 20 分钟、25 分钟和 3 分钟)和温度(此处为 58 °C、63 °C、73 °C 和 78 °C)来生成如下图所示的图表。平台之间段的斜率始终为 1 °C/分钟。
谢谢你!
\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=15cm,height=10cm,
xmin=0,ymax=100,
xlabel=Zeit / min,
ylabel=Temperatur / °C
]
%\addplot[color=red]{exp(x)};
\addplot[color=red,ultra thick] coordinates {(0,58) (5,63)};
\addplot[color=red,ultra thick] coordinates {(5,63) (25,63)};
\addplot[color=red,ultra thick] coordinates {(25,63) (35,73)};
\addplot[color=red,ultra thick] coordinates {(35,73) (60,73)};
\addplot[color=red,ultra thick] coordinates {(60,73) (65,78)};
\addplot[color=red,ultra thick] coordinates {(65,78) (68,78)};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
它的作用就是类似这样的操作。你只需要指定高原值和高原值持续的时间(以及第一个高原的坐标)。
\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
\tikzset{add temperature coordinates/.code={%
\pgfplotstablegetelem{#1}{[index]0}\of\loadedtable%
\let\myt\pgfplotsretval
\pgfplotstablegetelem{#1}{[index]1}\of\loadedtable%
\let\myp\pgfplotsretval
\ifnum#1=0\relax
\pgfmathsetmacro{\myx}{\xmin}%
\pgfmathsetmacro{\myy}{\myt-(\xstart-\xmin)}%
\pgfmathsetmacro{\tmpx}{\xstart+\myp}%
\edef\mytempcoords{(\myx,\myy) (\xstart,\myt) (\tmpx,\myt)}%
\else
\pgfmathsetmacro{\myx}{\tmpx+(\myt-\oldt)}%
\pgfmathsetmacro{\tmpx}{\myx+\myp}%
\edef\mytempcoords{\mytempcoords (\myx,\myt) (\tmpx,\myt)}%
\fi
\let\oldt\myt
}}
\pgfplotstableread{% this table contains the temperatures and plateau values
63 20
73 25
78 3
}\loadedtable
\pgfplotstablegetrowsof{\loadedtable}% find out how many rows the table has
\pgfmathtruncatemacro{\numrows}{\pgfplotsretval-1}
\edef\xstart{5}% you need to specify the x value of the start of the first plateau
\edef\xmin{0}% plot starts at xmin
% create macro in which the coordinats are stored
\tikzset{add temperature coordinates/.list={0,...,\numrows}}
% plot
\begin{axis}[width=15cm,height=10cm,
xmin=\xmin,ymax=100,
xlabel=time,
ylabel=temperature]
\addplot[color=red,ultra thick] coordinates {\mytempcoords};
\end{axis}
\end{tikzpicture}
\end{document}
你也可以将其设为宏。然后你需要做的就是说
\TempPlot{63 20\\
73 25\\
78 3\\
}
完整代码:
\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
\tikzset{temp plot/.cd,
add temperature coordinates/.code={%
\pgfplotstablegetelem{#1}{[index]0}\of\loadedtable%
\let\myt\pgfplotsretval
\pgfplotstablegetelem{#1}{[index]1}\of\loadedtable%
\let\myp\pgfplotsretval
\ifnum#1=0\relax
\pgfmathsetmacro{\myx}{\pgfkeysvalueof{/tikz/temp plot/xmin}}%
\pgfmathsetmacro{\myy}{\myt-(\pgfkeysvalueof{/tikz/temp plot/xstart}-\pgfkeysvalueof{/tikz/temp plot/xmin})}%
\pgfmathsetmacro{\tmpx}{\pgfkeysvalueof{/tikz/temp plot/xstart}+\myp}%
\edef\mytempcoords{(\myx,\myy) (\pgfkeysvalueof{/tikz/temp plot/xstart},\myt) (\tmpx,\myt)}%
\else
\pgfmathsetmacro{\myx}{\tmpx+(\myt-\oldt)}%
\pgfmathsetmacro{\tmpx}{\myx+\myp}%
\edef\mytempcoords{\mytempcoords (\myx,\myt) (\tmpx,\myt)}%
\fi
\let\oldt\myt
},xmin/.initial=0,xstart/.initial=5}
\newcommand{\TempPlot}[2][]{\begin{tikzpicture}
\tikzset{temp plot/.cd,#1}%
\pgfplotstableread[row sep=\\]{% this table contains the temperatures and plateau values
#2
}\loadedtable
\pgfplotstablegetrowsof{\loadedtable}% find out how many rows the table has
\pgfmathtruncatemacro{\numrows}{\pgfplotsretval-1}
% create macro in which the coordinats are stored
\tikzset{temp plot/add temperature coordinates/.list={0,...,\numrows}}
% plot
\begin{axis}[width=15cm,height=10cm,
xmin=\pgfkeysvalueof{/tikz/temp plot/xmin},ymax=100,
xlabel=time,
ylabel=temperature]
\addplot[color=red,ultra thick] coordinates {\mytempcoords};
\end{axis}
\end{tikzpicture}}
\begin{document}
\TempPlot{63 20\\
73 25\\
78 3\\
}
\end{document}