如何在 y 方向上绘制一条垂直线精确到坐标系的末端?
·请注意,(自动设置的)“ymax=10”可以改变(我不知道ymax=<value>
,因为表中包含随机数。)
·请注意,x 值是“6”(通常从表中读出作为特殊值)。
\documentclass[margin=5mm, tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\pgfplotstableread[col sep=comma]{
0, 0
1, 0
2, 0
3, 1
4, 3
5, 2
6, 5
7, 0
8, 1
12,10
13,5
}\mytable
\begin{tikzpicture}[]
\begin{axis}[
xmin=0, ymin=0,
]
\addplot[mark=*] table[]{\mytable};
\addplot[densely dashed, red] coordinates{(6,0) (6,10)};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\pgfplotstableread[col sep=comma]{
0, 0
1, 0
2, 0
3, 1
4, 3
5, 2
6, 5
7, 0
8, 1
12,10
13,5
}\mytable
\begin{tikzpicture}
\begin{axis}[
xmin=0, ymin=0,
]
\addplot[mark=*] table[]{\mytable};
\draw[red, ultra thick, densely dashed] (6,0) -- (6,0|-current axis.north);
\draw[green, densely dashed] (6,0) -- (6,\pgfkeysvalueof{/pgfplots/ymax});
\end{axis}
\end{tikzpicture}
\end{document}
答案2
两个想法(compat
无论如何你都应该将选项设置为最近的外翻):
- 利用 来
update limits=false
防止线的坐标延长 y 轴限制:
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\pgfplotstableread[col sep=comma]{
0, 0
1, 0
2, 0
3, 1
4, 3
5, 2
6, 5
7, 0
8, 1
12,10
13,5
}\mytable
\begin{tikzpicture}[]
\begin{axis}[
xmin=0, ymin=0,
]
\addplot[mark=*] table[]{\mytable};
\addplot[densely dashed, red, update limits=false] coordinates{(6,0) (6,11)};
\end{axis}
\end{tikzpicture}
\end{document}
- 使用命令绘制线条
\draw
:
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\pgfplotstableread[col sep=comma]{
0, 0
1, 0
2, 0
3, 1
4, 3
5, 2
6, 5
7, 0
8, 1
12,10
13,5
}\mytable
\begin{tikzpicture}[]
\begin{axis}[
xmin=0, ymin=0
]
\addplot[mark=*] table[]{\mytable};
\draw[densely dashed, red]
(6,0 |- {yticklabel cs:0}) -- (6,0 |- {yticklabel cs:1});
% alternatively:
% \draw[densely dashed, red]
% (6,\pgfkeysvalueof{/pgfplots/ymin}) -- (6,\pgfkeysvalueof{/pgfplots/ymax});
\end{axis}
\end{tikzpicture}
\end{document}
两个代码示例都产生相同的输出: