为什么主 X 轴和次 X 轴的位置和比例不同步?

为什么主 X 轴和次 X 轴的位置和比例不同步?

在主 X 轴上绘制一些给定的数据,并在次 X 轴上计算值,我注意到次轴发生了移动,并且与主轴相比具有不同的比例。

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{filecontents,pgfplotstable,pgfplots,booktabs}
\pgfplotsset{compat=newest}

\usepackage{filecontents}
\usepackage{pgfplotstable}

\begin{document}

\begin{filecontents*}{healthcare.txt}
x   Ontario Quebec
2010    597.5 392.3
2011    615.1 400.9
2012    628.7 410.2
2013    634.3 417.2
2014    644.4 422.6
\end{filecontents*}

\pgfplotstableread{healthcare.txt}\datatable
\pgfplotstablecreatecol[expr={(\thisrow{Ontario}-\prevrow{Ontario})*100/\prevrow{Ontario}}]{OntarioP}{\datatable}
\pgfplotstablecreatecol[expr={(\thisrow{Quebec}-\prevrow{Quebec})*100/\prevrow{Quebec}}]{QuebecP}{\datatable}

\pgfkeys{/pgf/number format/.cd,1000 sep={}} % removing thousand separator
\begin{tikzpicture}[font=\tiny,scale=1.2]
\pgfplotsset{
every axis plot/.append style={thick}
,scale only axis
,legend style={at={(1.15,0.5)},anchor=west,draw=none,mark=none}% or north west, ... ,outer north east
}
\begin{axis}[
axis y line*=left,
ytick={0,50,100,150,200,250,300,350,400,450,500,550,600,650},
xtick={2010,2011,2012,2013,2014},
%ymajorgrids=true,
xmajorgrids=true,
ymin=0,
ymax=700,
no markers,
]
\addplot table[mark=none,y={Ontario},x={x}]{\datatable};
\addlegendentry{Ontario}; \label{plot_one}
\addplot table[mark=none,y={Quebec}, x={x}]{\datatable};
\addlegendentry{Quebec}; \label{plot_two}

\end{axis};


\begin{axis}[
axis x line*={top,red}, % only added for visualization, do not need in final
axis y line*=right,
xtick={2010,2011,2012,2013,2014},
ymin=0,
xmin=2010,
]
\addlegendimage{/pgfplots/refstyle=plot_one}\addlegendentry{Ontario}
\addlegendimage{/pgfplots/refstyle=plot_two}\addlegendentry{Quebec}

\addplot [blue,dashed,mark=*,mark options=solid] table[y={OntarioP},x={x}]{\datatable};
\addlegendentry{Ontario \%};
\addplot [red,dashed,mark=square*,mark options=solid] table[y={QuebecP}, x={x}]{\datatable};
\addlegendentry{Quebec \%};

\end{axis};

\end{tikzpicture}
\end{document}

为了使给定数据的实线图和计算数据的虚线图保持一致,我希望使两个 X 轴的缩放比例相同,并且辅助图从 2011 年开始。2010 年的计算值显然不存在,因此我无法也不应该绘制它。这是可行的。但下面以红色显示的轴被移动和缩放了。尝试使用xtick, xmin,但无法弄清楚如何实现所需的结果。

移位次要 X 轴

答案1

\documentclass[]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{filecontents,pgfplotstable,pgfplots,booktabs}
\pgfplotsset{compat=newest}

\usepackage{filecontents}
\usepackage{pgfplotstable}

\begin{document}

\begin{filecontents*}{healthcare.txt}
  x   Ontario Quebec
  2010    597.5 392.3
  2011    615.1 400.9
  2012    628.7 410.2
  2013    634.3 417.2
  2014    644.4 422.6
\end{filecontents*}

\pgfplotstableread{healthcare.txt}\datatable
\pgfplotstablecreatecol[expr={(\thisrow{Ontario}-\prevrow{Ontario})*100/\prevrow{Ontario}}]{OntarioP}{\datatable}
\pgfplotstablecreatecol[expr={(\thisrow{Quebec}-\prevrow{Quebec})*100/\prevrow{Quebec}}]{QuebecP}{\datatable}

\pgfkeys{/pgf/number format/.cd,1000 sep={}} % removing thousand separator
\begin{tikzpicture}[font=\tiny,scale=1.2]
  \pgfplotsset{
    every axis plot/.append style={thick}
    ,scale only axis
    ,legend style={at={(1.15,0.5)},anchor=west,draw=none,mark=none}% or north west, ... ,outer north east
    ,xmin = 2009, xmax = 2015, % << added this
  }
  \begin{axis}[
    axis y line*=left,
    ytick={0,50,100,150,200,250,300,350,400,450,500,550,600,650},
    xtick={2010,2011,2012,2013,2014},
    % ymajorgrids=true,
    xmajorgrids=true,
    ymin=0,
    ymax=700,
    no markers,
    ]
    \addplot table[mark=none,y={Ontario},x={x}]{\datatable};
    \addlegendentry{Ontario}; \label{plot_one}
    \addplot table[mark=none,y={Quebec}, x={x}]{\datatable};
    \addlegendentry{Quebec}; \label{plot_two}

  \end{axis};


  \begin{axis}[
    axis x line*={top,red}, % only added for visualization, do not need in final
    axis y line*=right,
    xtick={2010,2011,2012,2013,2014},
    ymin=0,
    % xmin=2010, % << removed this
    ]
    \addlegendimage{/pgfplots/refstyle=plot_one}\addlegendentry{Ontario}
    \addlegendimage{/pgfplots/refstyle=plot_two}\addlegendentry{Quebec}

    \addplot [blue,dashed,mark=*,mark options=solid] table[y={OntarioP},x={x}]{\datatable};
    \addlegendentry{Ontario \%};
    \addplot [red,dashed,mark=square*,mark options=solid] table[y={QuebecP}, x={x}]{\datatable};
    \addlegendentry{Quebec \%};

  \end{axis};

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容