我有一个 PGF 条形图,其中条形的值由变量给出,使用\newcounter
。现在,计数器在呈现 PGF 图后更新。因此,绘图无法正确呈现。如何在计数器更新之前呈现绘图,但使 PGF 图包含更新的计数器?
示例(无效)
\documentclass{article}
\usepackage{pgfplots}
\newcounter{criticalfindings}
\newcounter{highfindings}
\newcounter{mediumfindings}
\newcounter{lowfindings}
\newcounter{informationalfindings}
\setcounter{criticalfindings}{0}
\setcounter{highfindings}{0}
\setcounter{mediumfindings}{0}
\setcounter{lowfindings}{0}
\setcounter{informationalfindings}{0}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
title=Overzicht,
ymajorgrids=true,
scale only axis,
xtick={1,2,3,4,5},
xticklabels={Kritiek, Hoog, Midden, Laag, Informatief},
every axis plot/.append style={
ybar,
bar width=20,
bar shift=0pt,
fill
},
ymin=0,
]
\addplot coordinates {(1,\thecriticalfindings)};
\addplot coordinates {(2,\thehighfindings)};
\addplot coordinates {(3,\themediumfindings)};
\addplot coordinates {(4,\thelowfindings)};
\addplot coordinates {(5,\theinformationalfindings)};
\end{axis}
\end{tikzpicture}
\stepcounter{informationalfindings}
\end{document}
此处,informationalfindings
计数器已增加到 1。但是,这并未反映在 PGF 图中。我该如何实现这一点?
答案1
您似乎认为图表是一种小部件,就像您可以在jupyter
笔记本或其他软件中生成的小部件一样。但事实并非如此,在 LaTeX 中,图表是使用参数的当前值生成/绘制的,该参数值在 PDF 文件中“固定”设置。
我改变了你的代码,以便图形生成位于一个宏内,你可以在任何时候想要显示效果时调用它。
如果你想要一个跟踪计数器变化的动态图表,我担心 LaTeX(这是一种排版系统(用于生成文档而不是小部件)并不是正确的工具。
\documentclass{article}
\usepackage{pgfplots}\pgfplotsset{compat=1.18}
\newcounter{criticalfindings}
\newcounter{highfindings}
\newcounter{mediumfindings}
\newcounter{lowfindings}
\newcounter{informationalfindings}
\setcounter{criticalfindings}{0}
\setcounter{highfindings}{0}
\setcounter{mediumfindings}{0}
\setcounter{lowfindings}{0}
\setcounter{informationalfindings}{0}
\newcommand{\domygraph}{%
\begin{tikzpicture}
\begin{axis}[
title=Overzicht,
ymajorgrids=true,
scale only axis,
xtick={1,2,3,4,5},
xticklabels={Kritiek, Hoog, Midden, Laag, Informatief},
every axis plot/.append style={
ybar,
bar width=20pt,% <<<<<< NOTICE!!!
bar shift=0pt,
fill
},
ymin=0,
]
\addplot coordinates {(1,\thecriticalfindings)};
\addplot coordinates {(2,\thehighfindings)};
\addplot coordinates {(3,\themediumfindings)};
\addplot coordinates {(4,\thelowfindings)};
\addplot coordinates {(5,\theinformationalfindings)};
\end{axis}
\end{tikzpicture}%
}
\begin{document}
\stepcounter{informationalfindings}
\domygraph
\stepcounter{highfindings}
\domygraph
\end{document}
我也
- 添加了
compat
pgfplots 版本(您对此有警告) - 为条形宽度添加了一个尺寸值(较新的版本
pgfplot
将把平面视为20
与 x 轴相同的单位)。
您可能还应该添加一个固定的垂直刻度。
答案2
经过一些修改,我有了这个
代码:
\documentclass{article}
\usepackage{pgfplots}
\newcounter{criticalfindings}
\newcounter{highfindings}
\newcounter{mediumfindings}
\newcounter{lowfindings}
\newcounter{informationalfindings}
% changed
\setcounter{criticalfindings}{20}
\setcounter{highfindings}{10}
\setcounter{mediumfindings}{20}
\setcounter{lowfindings}{15}
\setcounter{informationalfindings}{30}
\begin{document} % added
\begin{tikzpicture}
\begin{axis}[
title=Overzicht,
ymajorgrids=true,
scale only axis,
xtick={1,2,3,4,5},
xticklabels={Kritiek, Hoog, Midden, Laag, Informatief},
every axis plot/.append style={
ybar,
bar width=20,
bar shift=0pt,
fill
},
ymin=0,
]
\addplot coordinates {(1,\thecriticalfindings)};
\addplot coordinates {(2,\thehighfindings)};
\addplot coordinates {(3,\themediumfindings)};
\addplot coordinates {(4,\thelowfindings)};
\addplot coordinates {(5,\theinformationalfindings)};
%\stepcounter{informationalfindings}
\end{axis}
\end{tikzpicture}
\end{document}