我遇到了一个问题,我想绘制一系列间距均匀的箭头,其长度取决于数据文件中的值,但我不太清楚该怎么做。如果可能的话,我希望能够使用循环,\foreach
而不是手动输入箭头\draw
命令。
我对代码工作方式的想法与此类似,尽管进行了简化:
\begin{tikzpicture}
\begin{axis}
[
ymin=0,
ymax=.04,
]
\addplot [mark=none,black,very thick] table[x=T500,y=Y] {Test.dat};
\foreach \y in {0,0.25e-02,...,0.04}
\draw [->] (axis cs:0,\y) -- (axis cs: [Value From File],\y);
\end{axis}
\end{tikzpicture}
我想要实现的图像(简单地将箭头硬编码)将类似于以下内容,但扩展到整个数据范围(在本例中为 0 到 .04)。有没有办法用类似于我上面提到的方法来完成此操作?我很感激任何帮助。谢谢!
我作为示例绘制的文本文件粘贴在下面。
Y T500
0 40
0.001 36.7099
0.002 33.5354
0.003 30.4769
0.004 27.535
0.005 24.71
0.006 22.0023
0.007 19.4121
0.008 16.9399
0.009 14.5859
0.01 12.3502
0.011 10.2331
0.012 8.23476
0.013 6.35515
0.014 4.59435
0.015 2.95234
0.016 1.42906
0.017 0.0244028
0.018 -1.26176
0.019 -2.42961
0.02 -3.47933
0.021 -4.41114
0.022 -5.22529
0.023 -5.92201
0.024 -6.50156
0.025 -6.9642
0.026 -7.31015
0.027 -7.53968
0.028 -7.65298
0.029 -7.65028
0.03 -7.53175
0.031 -7.29756
0.032 -6.94784
0.033 -6.48271
0.034 -5.90225
0.035 -5.20653
0.036 -4.39559
0.037 -3.46947
0.038 -2.42816
0.039 -1.27168
0.04 0
答案1
这是一个可能的解决方案,改编自杰克的回答这里。
代码注释得很好,但基本思想是修改comb
具有正确一般形式的 -type 图的样式。我们需要更改标记,并移动和旋转它,以便标记与曲线的正确部分对齐。
可以使用键调整采样each nth point=<value>
,如示例所示。
局限性
如该例所示,对于零值,箭头方向的计算并不良好。
代码
\documentclass{standalone}
% include the data file all in one place
\usepackage{filecontents}
\begin{filecontents}{Test.dat}
Y T500
0 40
0.001 36.7099
0.002 33.5354
0.003 30.4769
0.004 27.535
0.005 24.71
0.006 22.0023
0.007 19.4121
0.008 16.9399
0.009 14.5859
0.01 12.3502
0.011 10.2331
0.012 8.23476
0.013 6.35515
0.014 4.59435
0.015 2.95234
0.016 1.42906
0.017 0.0244028
0.018 -1.26176
0.019 -2.42961
0.02 -3.47933
0.021 -4.41114
0.022 -5.22529
0.023 -5.92201
0.024 -6.50156
0.025 -6.9642
0.026 -7.31015
0.027 -7.53968
0.028 -7.65298
0.029 -7.65028
0.03 -7.53175
0.031 -7.29756
0.032 -6.94784
0.033 -6.48271
0.034 -5.90225
0.035 -5.20653
0.036 -4.39559
0.037 -3.46947
0.038 -2.42816
0.039 -1.27168
0.04 0
\end{filecontents}
\usepackage{pgfplots}
\pgfplotsset{%
compat=1.10,
mycomb/.style={% adapted from : https://tex.stackexchange.com/a/20350/21344
mark=triangle*, % can be adjusted as needed
mark options={scale=1,rotate=-90}, % same here; adjust to suit
xcomb, % make it an xcomb plot
scatter, % allow per-point mark adjustments
visualization depends on={x/abs(x)-1 \as \sign}, % compute arrow direction---not well-behaved at zero!
scatter/@pre marker code/.code={%
\scope[rotate=90*\sign,yshift=-2pt]
}, % adjust each marker (shift fine-tunes placement)
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
ymin=0,
ymax=.04,
]
\addplot [black,very thick] table[x=T500,y=Y] {Test.dat}; % plot the function
\addplot [mycomb,each nth point=2] table[x=T500,y=Y] {Test.dat}; % plot the comb (adjust sampling as needed)
\end{axis}
\end{tikzpicture}
\end{document}
输出
答案2
您可以使用该intersections
库来计算箭头的位置:
笔记:
- 在
\edef
中\foreach
解释为\foreach 在轴环境中不起作用。
代码:
\documentclass[12pt]{article}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\usepackage{filecontents}
\begin{filecontents*}{Test.dat}
Y T500
0 40
0.001 36.7099
0.002 33.5354
0.003 30.4769
0.004 27.535
0.005 24.71
0.006 22.0023
0.007 19.4121
0.008 16.9399
0.009 14.5859
0.01 12.3502
0.011 10.2331
0.012 8.23476
0.013 6.35515
0.014 4.59435
0.015 2.95234
0.016 1.42906
0.017 0.0244028
0.018 -1.26176
0.019 -2.42961
0.02 -3.47933
0.021 -4.41114
0.022 -5.22529
0.023 -5.92201
0.024 -6.50156
0.025 -6.9642
0.026 -7.31015
0.027 -7.53968
0.028 -7.65298
0.029 -7.65028
0.03 -7.53175
0.031 -7.29756
0.032 -6.94784
0.033 -6.48271
0.034 -5.90225
0.035 -5.20653
0.036 -4.39559
0.037 -3.46947
0.038 -2.42816
0.039 -1.27168
0.04 0
\end{filecontents*}
\newcommand{\yValue}{0.005}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
ymin=0,
ymax=.04,
xmin=-20,
xmax=50,
]
\addplot [mark=none,blue,very thick,name path global=My Graph] table[x=T500,y=Y] {Test.dat};
\foreach \yValue in {0.0000,0.0025,...,0.04} {
\edef\tempX{\noexpand\draw [draw=none, name path global=Horizontal Line] (axis cs:-20,\yValue) -- (axis cs:50,\yValue);}
\edef\tempY{\noexpand\draw [red,-latex,thick, name intersections={of=My Graph and Horizontal Line}]
{(axis cs:0,\yValue) -- (intersection-1)};
}
\tempX
\tempY
}
\draw [gray] (axis cs:0,0) -- (axis cs:0,0.04);
\end{axis}
\end{tikzpicture}
\end{document}