我有一系列数据图,下面显示了其中的一个示例,我想在每个图上绘制一条穿过特定 x 坐标的垂直线,以便箭头的头部与具有最大 y 坐标的点对齐。浏览手册后,我发现没有任何东西可以帮助我直接实现这一点(axis cs:<x>,<y>
只允许我为每个图单独执行此操作,我将使用一个模板生成数千个图)。
提取图顶部的 y 坐标(使用\pgfextract
with s.north
)超出了我的努力,但即使这样也无法解决问题,因为我仍然需要从中减去默认的 y 限制放大值。
\documentclass{article}
\usepackage{filecontents}
\usepackage[top=2cm,bottom=2cm,left=2cm,right=2cm]{geometry}
\usepackage{pgfplots}
\usepackage{tikz}
\begin{filecontents}{sample-data.dat}
{Data Values} Count
0 6
0.109827 86
0.219654 300
0.329481 662
0.439308 1085
0.549135 1366
0.658962 1470
0.768789 1333
0.878616 1200
0.988443 904
1.09827 620
1.2081 417
1.31792 247
1.42775 164
1.53758 72
1.64741 30
1.75723 21
1.86706 8
1.97689 6
2.08671 2
2.19654 1
2.41619 0
\end{filecontents}
\providecommand{\lineplotxy}[2]{
\begin{tikzpicture}
\begin{axis}[
xlabel=Data Values,
ylabel=Count,
title=#1,
]
\addplot+[
sharp plot
]
table [
x=Data Values,
y=Count
] {#2};
\end{axis}
\end{tikzpicture}
}
\providecommand{\lineplotxymarked}[2]{
\begin{tikzpicture}
\begin{axis}[
xlabel=Data Values,
ylabel=Count,
title=#1,
]
\addplot+[
sharp plot
]
table [
x=Data Values,
y=Count
] {#2};
\draw[red,->,shorten >=2pt,>=stealth] (axis cs:2,0) -- (axis cs:2,1500);
\end{axis}
\end{tikzpicture}
}
\begin{document}
\centering
\begin{figure}[!h]
\resizebox{\textwidth}{!}{
\lineplotxy{Initial Diagram}{sample-data.dat}
\lineplotxymarked{Desired Output}{sample-data.dat}
}
\end{figure}
\end{document}
感谢您的帮助!
答案1
您可以使用pgfplotstable
PGFplots 包,通过对表进行排序并提取排序表的第一个元素来查找数据中的最大值
\documentclass{article}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{filecontents}{sample-data.dat}
{Data Values} Count
0 6
0.109827 86
0.219654 300
0.329481 662
0.439308 1085
0.549135 1366
0.658962 1470
0.768789 1333
0.878616 1200
0.988443 904
1.09827 620
1.2081 417
1.31792 247
1.42775 164
1.53758 72
1.64741 30
1.75723 21
1.86706 8
1.97689 6
2.08671 2
2.19654 1
2.41619 0
\end{filecontents}
\pgfplotstablesort[sort key={[index] 1},sort cmp={float >}]{\sorted}{sample-data.dat}
\pgfplotstablegetelem{0}{[index] 1}\of{\sorted}
\let\maxvalue=\pgfplotsretval
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=Data Values,
ylabel=Count,
]
\addplot table [
x=Data Values,
y=Count
] {sample-data.dat};
\draw[red,->,>=stealth] (axis cs:2,0) -- (axis cs:2,\maxvalue)
node [anchor=west,black] at (axis cs:2,\maxvalue) {\maxvalue};
\end{axis}
\end{tikzpicture}
\end{document}
为了展示还有什么其他用途pgfplotstable
,这里有一个宏,它以 x 值作为参数,在表中查找具有最接近 x 值的数据点,并组合这两个点以形成箭头:
\documentclass{article}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{filecontents}{sample-data.dat}
{Data Values} Count
0 6
0.109827 86
0.219654 300
0.329481 662
0.439308 1085
0.549135 1366
0.658962 1470
0.768789 1333
0.878616 1200
0.988443 904
1.09827 620
1.2081 417
1.31792 247
1.42775 164
1.53758 72
1.64741 30
1.75723 21
1.86706 8
1.97689 6
2.08671 2
2.19654 1
2.41619 0
\end{filecontents}
\newcommand{\assemblearrowpoints}[1]{
% Read table, save column names
\pgfplotstableread{sample-data.dat}{\datatable}
\pgfplotstablegetcolumnnamebyindex{0}\of{\datatable}\to{\xname}
\pgfplotstablegetcolumnnamebyindex{1}\of{\datatable}\to{\yname}
% Find maximum value in table by sorting and then extracting first element of sorted table
\pgfplotstablesort[sort key={\yname},sort cmp={float >}]{\sorted}{sample-data.dat}
\pgfplotstablegetelem{0}{\yname}\of{\sorted}
\let\maxy=\pgfplotsretval
% Create new "target" column with squared difference between x values and target x
\pgfplotstablecreatecol[
create col/expr={(\thisrow{\xname}-#1)^2}]{target}{\datatable}
% Sort from lowest to highest squared difference
\pgfplotstablesort[sort key={target},sort cmp={float <}]{\targettable}{\datatable}
\pgfplotstablegetelem{0}{\xname}\of{\targettable}
\let\lowerx=\pgfplotsretval
\pgfplotstablegetelem{0}{\yname}\of{\targettable}
\let\lowery=\pgfplotsretval
% Assemble points for arrow
\def\lowerpoint{axis cs:\lowerx,\lowery}
\def\highpoint{axis cs:\lowerx,\maxy}
}
\begin{document}
\assemblearrowpoints{2}
\begin{tikzpicture}
\begin{axis}[
xlabel=Data Values,
ylabel=Count,
]
\addplot table [
x=Data Values,
y=Count
] {sample-data.dat};
\draw[red,->,>=stealth] (\lowerpoint)
node [black,anchor=south west] {\lowery}
-- (\highpoint)
node [pos=0.5,black,anchor=west] {\pgfmathparse{int(\maxy-\lowery)}\pgfmathresult}
node [black,anchor=west] {\maxy};
\end{axis}
\end{tikzpicture}
\end{document}