我正在从 matlab 生成的 .dat 文件中导入绘图数据。我还想从文件中设置轴限值.dat
。有没有办法从矩阵中的表格中设置 xmin、xmax、ymin 和 ymax .dat
(如稍后绘制的数据)?我目前设置轴限值的方式如下所示。
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgf,pgffor}
\pgfplotsset{compat=1.15}
\def\names{{Folder1/}{Folder2/}{Folder3/}}
\foreach \x in \names{
\graphicspath{ \x {Icons/}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
enlargelimits = false,
hide axis,
]
\addplot graphics [
xmin=-80.5430, xmax=-80.5150,
ymin=43.4580, ymax=43.4740,
]{ov2.png};
\addplot[
only marks,
mark=text,
text mark={\includegraphics[scale=0.02]{icon.png}}
]
table [search path={\x}]{overnight.dat};
\end{axis}
\end{tikzpicture}
\end{document}
我希望在包含轴限值的文件中定义 xmin xmax,而不是在代码中定义它们(它们不是正在绘制的数据的函数,而是正在叠加的.dat
图像角落的 GPS 坐标)ov2.png
答案1
如前所述,您可以使用\pgfplotstablegetelem
来访问数据文件的单个元素。这需要您知道它的行和列。值以 返回\pgfplotsretval
。列可以通过名称引用,行可以通过其索引引用,从 0 开始。
还可以使用 循环遍历列并对每个值执行某些操作\pgfplotstableforeachcolumnelement
。例如,这可用于查找最小值、最大值或平均值。
为此编写几个宏是个好主意,尤其是在第二种情况下。以下代码显示了一些示例:
\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents}{data.csv}
x y
-1.5 27.3
-1.0 24.8
-0.5 22.1
0.0 30.7
0.5 11.3
1.0 15.9
1.5 20.4
2.0 21.8
2.5 22.8
\end{filecontents}
% read the table
\pgfplotstableread{data.csv}\datatable
% get specific element and store it in a macro
% #1: table
% #2: row
% #3: column (name or [index]<index>)
% #4: macro for value
\newcommand*{\GetElement}[4]{%
\pgfplotstablegetelem{#2}{#3}\of{#1}%
\let#4\pgfplotsretval
}
% get the first element of a column and store it in a macro
% #1: table
% #2: column (name or [index]<index>)
% #3: macro for value
\newcommand*{\GetFirstElement}[3]{%
\GetElement{#1}{0}{#2}{#3}%
}
% get the last element of a column and store it in a macro
% #1: table
% #2: column (name or [index]<index>)
% #3: macro for value
\newcommand*{\GetLastElement}[3]{%
\pgfplotstablegetrowsof{#1}%
\pgfmathsetmacro\lastrowidx{int(\pgfplotsretval - 1)}%
\GetElement{#1}{\lastrowidx}{#2}{#3}%
}
% get maximum and minimum value of a column and store them in macros
% #1: table
% #2: column
% #3: macro for max
% #4: macro for min
\newcommand*{\GetMinMax}[4]{%
\pgfmathsetmacro#3{-16383}%
\pgfmathsetmacro#4{16383}%
\pgfplotstableforeachcolumnelement{#2}\of{#1}\as\cellValue{%
\ifx\cellValue\@empty\else
\pgfmathsetmacro{#3}{max(#3,\cellValue)}%
\pgfmathsetmacro{#4}{min(#4,\cellValue)}%
\fi
}
}
% get average of column and store it in a macro
% #1: table
% #2: column
% #3: macro for average
\newcommand*{\GetAverage}[3]{%
\pgfplotstablegetrowsof{#1}%
\let\rownumber\pgfplotsretval
\def#3{0}%
\pgfplotstableforeachcolumnelement{#2}\of{#1}\as\cellValue{%
\ifx\cellValue\@empty
% don't count emplty cells
\pgfmathsetmacro{\rownumber}{\rownumber - 1}%
\else
\pgfmathsetmacro{#3}{#3 + \cellValue}%
\fi
}
\pgfmathsetmacro{#3}{#3 / \rownumber}%
}
\GetMinMax{\datatable}{x}{\dataxmax}{\dataxmin}
\GetMinMax{\datatable}{y}{\dataymax}{\dataymin}
\GetElement{\datatable}{5}{y}{\value}
\GetFirstElement{\datatable}{y}{\first}
\GetLastElement{\datatable}{y}{\last}
\GetMinMax{\datatable}{x}{\dataxmax}{\dataxmin}
\GetMinMax{\datatable}{y}{\dataymax}{\dataymin}
\GetAverage{\datatable}{y}{\average}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
xlabel=$x$-label,
ylabel=$y$-label,
xmin=\dataxmin, xmax=\dataxmax,
ymin=\dataymin, ymax=\dataymax,
]
\addplot table[x=x,y=y] {\datatable};
\draw[red] (axis cs:\dataxmin,\value) -- node[pos=0.35, above] {\value} (axis cs:\dataxmax,\value);
\draw[green] (axis cs:\dataxmin,\first) -- node[pos=0.07, above] {\first} node[pos=0.93, above] {\last} (axis cs:\dataxmax,\last);
\draw[cyan] (axis cs:\dataxmin,\average) -- node[pos=0.25, below] {average $=$ \average} (axis cs:\dataxmax,\average);
\end{axis}
\end{tikzpicture}
\end{document}