![PGFPLOTS 如何在正确的位置添加额外的 xtick 或垂直线?](https://linux22.com/image/306085/PGFPLOTS%20%E5%A6%82%E4%BD%95%E5%9C%A8%E6%AD%A3%E7%A1%AE%E7%9A%84%E4%BD%8D%E7%BD%AE%E6%B7%BB%E5%8A%A0%E9%A2%9D%E5%A4%96%E7%9A%84%20xtick%20%E6%88%96%E5%9E%82%E7%9B%B4%E7%BA%BF%EF%BC%9F.png)
我试图在坐标 x= 3.5 处添加一条垂直线,以说明这是实际的厚度配置。但是下面的尝试没有将刻度放在轴上的正确位置。理想情况下,除了垂直线之外,我还想添加一个注释,例如“实际”。
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{siunitx}
\usepackage{filecontents}
\begin{document}
\begin{filecontents}{data.csv}
{Thickness (mm)};{Trise ambient temp};{T-rise at max.temp};{T-rise ambient (narrow)};{T-rise max temp. (narrow)}
3;30;51;46;79
4;21;37;33;56
5;16;28;25;43
6;13;23;20;34
7;11;19;16;28
\end{filecontents}
\pgfplotstableread[col sep = semicolon]{data.csv}{\datatable}
\begin{tikzpicture}
\begin{axis}[height=.6\textwidth, width=\textwidth,
thick,
smooth,
grid=both,
xtick=data,
xticklabels from table={\datatable}{[index]0},
extra x ticks={3.5}, % does not work, puts a tick between 6 and 7, with label "3"
xlabel={Thickness (\si{\milli\meter})},
ylabel={T-rise (\si{\celsius)}},
]
\addplot [gray,] table [
x expr=\coordindex, x index =0,
y index=1] {\datatable}
[sloped, font=\small]
node[below, pos=0.25] {T-rise at \SI {0}{\celsius}};
\addplot [blue,] table [
x expr=\coordindex, x index =0,
y index=2] {\datatable}
[sloped, font=\small]
node[above, pos=0.25] {T-rise at \SI {180}{\celsius}};
%\draw[ultra thin] (axis cs:3.5,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:3.5,\pgfkeysvalueof{/pgfplots/ymax}); draws a vertical line, but at x=4
\end{axis}
\end{tikzpicture}
\end{document}
答案1
您应该使用x expr=\coordindex,
来绘制坐标索引与某些 y 数据的关系。但在这里,您要绘制的是第 0 列(作为 x)与第 1 列和第 2 列(作为 y)的关系。因此,您不应该使用x expr
。由于您这样做了,3.5 处的额外刻度出现在\coordindex
3.5 处,其中您有来自表格的 x 标签。(即 6.5)。
底线:删除 x expr=\coordindex,
\documentclass[border=5mm]{standalone}
%\usepackage{pgfplots} %% loaded by pgfplotstable.
\usepackage{pgfplotstable}
\usepackage{siunitx}
\usepackage{filecontents}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{filecontents}{data.csv}
{Thickness (mm)};{Trise ambient temp};{T-rise at max.temp};{T-rise ambient (narrow)};{T-rise max temp. (narrow)}
3;30;51;46;79
4;21;37;33;56
5;16;28;25;43
6;13;23;20;34
7;11;19;16;28
\end{filecontents}
\pgfplotstableread[col sep = semicolon]{data.csv}{\datatable}
\begin{tikzpicture}
\begin{axis}[height=.6\textwidth, width=\textwidth,
thick,
smooth,
grid=both,
xtick=data,
%xticklabels from table={\datatable}{[index]0},
xlabel={Thickness (\si{\milli\meter})},
ylabel={T-rise (\si{\celsius)}},
extra x ticks={3.5},
extra tick style={grid=major,major grid style={red,thick},
tick label style={
rotate=90,anchor=east}},
extra x tick labels={Actual},
xticklabels from table={\datatable}{[index]0},
]
\addplot [gray,] table [
x index =0,
y index=1] {\datatable}
[sloped, font=\small]
node[below, pos=0.25] {T-rise at \SI {0}{\celsius}};
\addplot [blue,] table [
x index =0,
y index=2] {\datatable}
[sloped, font=\small]
node[above, pos=0.25] {T-rise at \SI {180}{\celsius}};
%\draw[ultra thin] (axis cs:3.5,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:3.5,\pgfkeysvalueof{/pgfplots/ymax}); draws a vertical line, but at x=4
\end{axis}
\end{tikzpicture}
\end{document}