PGFPLOTS 如何在正确的位置添加额外的 xtick 或垂直线?

PGFPLOTS 如何在正确的位置添加额外的 xtick 或垂直线?

我试图在坐标 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 处的额外刻度出现在\coordindex3.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}

在此处输入图片描述

相关内容