如何将额外的 y 刻度样式从十进制转换为分钟:秒格式?
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\newcommand{\plotcoef}[2]{%
\begin{tikzpicture}
\begin{axis}[
width=0.92\textwidth,
height=0.35\textheight, % size of the image
grid = major,
grid style = {dashed, gray!30},
xmin = 10, % start the diagram at this x-coordinate
xmax = 150, % end the diagram at this x-coordinate
ymin = 0, % start the diagram at this y-coordinate
ymax = 25, % end the diagram at this y-coordinate
% filter/.code={\pgfmathparse{#1/60}},
extra x ticks = {#2},
extra y ticks = {#1},
extra tick style={% changes for all extra ticks
tick align=outside,
tick pos=left,
},
extra x tick style={% changes for extra x ticks
major tick length=1.3\baselineskip,
/pgf/number format/precision=1,
/pgf/number format/fixed,
/pgf/number format/fixed zerofill,
/pgf/number format/1000 sep={.},
/pgf/number format/set decimal separator={,}%
},
extra y tick style={% changes for extra y ticks
major tick length=1.5em,
y filter/.code={\pgfmathparse{#1/60}\pgfmathresult},
tick label style={rotate=90}
},
axis background/.style = {fill=white},
ylabel = {%
\begin{minipage}{6cm}
\centering
Infiltration time\\%
(for 1 cm lowering)
\end{minipage}},
xlabel = {Infiltration coefficient($l/m^{2}\cdot\,dia$)},
tick align = outside,]
\draw[blue, dashed, thick](#2,0) -- (#2,#1); %vertical line
\draw[blue, dashed, thick](0,#1) -- (#2,#1); %horizontal line
\addplot[domain=0:150, red, thick] {490/x-2.5};
\end{axis}
\end{tikzpicture}}
\begin{figure}[htb!]\plotcoef{1.46666666666667}{123.529411764706}\caption{Test result - Cx-01}\label{fig:cx1}\end{figure}
\end{document}
答案1
使用特殊的刻度标签格式设置
extra y tick style={% changes for extra y ticks
major tick length=1.5em,
% y filter/.code={\pgfmathparse{#1/60}\pgfmathresult},
tick label style={rotate=90},
yticklabel={%
\begingroup
\pgfmathparse{int(\tick)}%
\ifdim\tick pt>\pgfmathresult pt
\pgfmathresult:%
\pgfmathparse{(\tick-\pgfmathresult)*60}%
\pgfmathprintnumber{\pgfmathresult}%
\else
\pgfmathresult
\fi
\endgroup
},
},
你可以得到
完整示例
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\newcommand{\plotcoef}[2]{%
\begin{tikzpicture}
\begin{axis}[
width=0.92\textwidth,
height=0.35\textheight, % size of the image
grid = major,
grid style = {dashed, gray!30},
xmin = 10, % start the diagram at this x-coordinate
xmax = 150, % end the diagram at this x-coordinate
ymin = 0, % start the diagram at this y-coordinate
ymax = 25, % end the diagram at this y-coordinate
% filter/.code={\pgfmathparse{#1/60}},
extra x ticks = {#2},
extra y ticks = {#1},
extra tick style={% changes for all extra ticks
tick align=outside,
tick pos=left,
},
extra x tick style={% changes for extra x ticks
major tick length=1.3\baselineskip,
/pgf/number format/precision=1,
/pgf/number format/fixed,
/pgf/number format/fixed zerofill,
/pgf/number format/1000 sep={.},
/pgf/number format/set decimal separator={,}
},
extra y tick style={% changes for extra y ticks
major tick length=1.5em,
% y filter/.code={\pgfmathparse{#1/60}\pgfmathresult},
tick label style={rotate=90},
yticklabel={%
\begingroup
\pgfmathparse{int(\tick)}%
\ifdim\tick pt>\pgfmathresult pt
\pgfmathresult:%
\pgfmathparse{(\tick-\pgfmathresult)*60}%
\pgfmathprintnumber{\pgfmathresult}%
\else
\pgfmathresult
\fi
\endgroup
},
},
axis background/.style = {fill=white},
ylabel = {%
\begin{minipage}{6cm}
\centering
Infiltration time\\%
(for 1 cm lowering)
\end{minipage}},
xlabel = {Infiltration coefficient($l/m^{2}\cdot\,dia$)},
tick align = outside,
]
\draw[blue, dashed, thick](#2,0) -- (#2,#1); %vertical line
\draw[blue, dashed, thick](0,#1) -- (#2,#1); %horizontal line
\addplot[domain=0:150, red, thick] {490/x-2.5};
\end{axis}
\end{tikzpicture}}
\begin{figure}[htb!]
\plotcoef{1.46666666666667}{123.529411764706}
\caption{Test result - Cx-01}\label{fig:cx1}
\end{figure}
\end{document}