
我試圖在座標 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}