具有两个 y 轴的 Pgfplot 未显示正确的 ytick 值

具有两个 y 轴的 Pgfplot 未显示正确的 ytick 值

我正在尝试绘制一个有两个 y 轴的图表,但值一直显示与我预期不同的内容。有什么方法可以解决这个问题吗?非常感谢。

\begin{tikzpicture}
    \centering
    \pgfplotsset{set layers}
\begin{axis}[
    scale only axis,
    title={Optimum pipe diameter selection},
    axis y line*=left,
    xlabel={Pipe diamter DN (mm)},
    ylabel={Total cost (\si{\euro\per\metre})]},
    xmin=500, xmax=1050,
    ymin=3500, ymax=6500,
    xtick={500,550,600,650,700,750,800,850,900,950,1000,1050},
    ytick={3500,4000,4500,5000,5500,6000,6500},
    ymajorgrids=true,
    grid style={line width=.1pt, draw=gray!10},
    major grid style={line width=.2pt,draw=gray!50},
]

\addplot[
    color=blue,
    mark=*,
    ]
    coordinates {
    (500,8062.65)(550,5941.79)(600,4821.04)(650,4233.05)(700,3944.02)(750, 3830.34)(800,3823.46)(850,3883.82)(900,3987.76)(950,4120.66)(1000,4273.26)
    };
    \label{Total cost}
    
\end{axis}
\begin{axis}[
scale only axis,
axis y line*=right,
axis x line=none,
 ylabel={Pressure drop (mm/m)},
 ymin=0, ymax=45,
 ytick={0,5,10,15,20,25,30,35,40,45}
 ]

\end{axis}
\end{tikzpicture}

在此处输入图片描述

答案1

似乎如果您仅指定 y 限制,并且轴上没有任何图,pgfplots则会恢复到默认限制。它会在日志文件中警告您这一点:

软件包 pgfplots 警告:您的轴有空范围(在 x 方向上)。将其替换为默认范围并清除所有图。在输入行 46。

但是如果你也指定了 x 限制,你就会得到我认为你想要的。

在此处输入图片描述

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usepackage{eurosym}
\usepackage{siunitx}
\DeclareSIUnit{\EUR}{\text{\euro}} % https://tex.stackexchange.com/a/152599
\pgfplotsset{compat=1.18}
\begin{document}

\begin{tikzpicture}
    \pgfplotsset{set layers}
\begin{axis}[
    scale only axis,
    title={Optimum pipe diameter selection},
    axis y line*=left,
    xlabel={Pipe diameter DN (mm)},
    ylabel={Total cost (\si{\EUR\per\metre})]},
    xmin=500, xmax=1050,
    ymin=3500, ymax=6500,
    xtick={500,550,600,650,700,750,800,850,900,950,1000,1050},
    ytick={3500,4000,4500,5000,5500,6000,6500},
    ymajorgrids=true,
    grid style={line width=.1pt, draw=gray!10},
    major grid style={line width=.2pt,draw=gray!50},
]

\addplot[
    color=blue,
    mark=*,
    ]
    coordinates {
    (500,8062.65)(550,5941.79)(600,4821.04)(650,4233.05)(700,3944.02)(750, 3830.34)(800,3823.46)(850,3883.82)(900,3987.76)(950,4120.66)(1000,4273.26)
    };
    \label{Total cost}
    
\end{axis}
\begin{axis}[
scale only axis,
axis y line*=right, 
axis x line=none,
 ylabel={Pressure drop (mm/m)},
 xmin=500, xmax=1050, % <--- added
 ymin=0, ymax=45,
 ytick={0,5,10,15,20,25,30,35,40,45}
 ]

\end{axis}
\end{tikzpicture}

\end{document}

相关内容