在另一个轴中使用一个轴的限值

在另一个轴中使用一个轴的限值

我正在研究一个 pgfplots 源,它绘制一个具有不同平行轴比例的(动态)数据文件。当您提前知道轴/数据范围时,这很容易,但如果事先不知道轴/数据的范围怎么办?假​​设我不能使用包装器来分析数据并生成 tex 源。

以下代码存储前几个轴范围:

[ \xminA :\xmaxA ], [ \yminA :\ymaxA ]

内部变量,但该变量的范围不允许我使用获取的第二个轴对的值来设置它们的适当范围:

[ \xminB = \xminA * \ratioX :\xmaxB = \xmaxA * \ratioX ], [ \yminB = \yminA * \ratioY :\ymaxB = \ymaxA * \ratioY ]

\documentclass[tikz, border={50pt 10pt}]{standalone}
\usepackage{tikz, pgfplots}
\usepgfplotslibrary{units}
\usetikzlibrary{calc}
\newcommand{\ratioX}{2}
\newcommand{\ratioY}{10}
\pgfplotsset{
  compat = newest,
  change x base, change y base,
  scale only axis,
  }
\newcommand*{\datafileLike}{(-3, -3) (2, 2)}
%
\begin{document}
  \begin{tikzpicture} [baseline, trim axis left, trim axis right]
    \def\xminA{\pgfkeysvalueof{/pgfplots/xmin}}
    \def\xmaxA{\pgfkeysvalueof{/pgfplots/xmax}}
    \def\yminA{\pgfkeysvalueof{/pgfplots/ymin}}
    \def\ymaxA{\pgfkeysvalueof{/pgfplots/ymax}}
    %
    \begin{axis} [
      xlabel = {\pgfmathprintnumber{\xminA} : \pgfmathprintnumber{\xmaxA}},
      ylabel = {\pgfmathprintnumber{\yminA} : \pgfmathprintnumber{\ymaxA}},
      axis x line* = bottom,
      axis y line* = left,
      width  = 6cm,
      height = 4cm,
      ] \addplot [black] coordinates {\datafileLike};
    \end{axis}
    %
    \begin{axis} [
      axis x line* = top,
      axis y line* = right,
      width  = 6cm,
      height = 4cm,
      % xmin = \xminA * \ratioX,
      % xmax = \xmaxA * \ratioX,
      % ymin = \yminA * \ratioY,
      % ymax = \ymaxA * \ratioY,
      ]
    \end{axis}
  \end{tikzpicture}
\end{document}

图表由上述来源制作

答案1

在第一个轴完成之前,您可以通过键全局记录下一个轴的值after end axis/.code​​。

\documentclass[border={50pt 10pt}]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{units}
\usetikzlibrary{calc}
\newcommand{\ratioX}{2}
\newcommand{\ratioY}{10}
\pgfplotsset{
  compat = newest,
  change x base, change y base,
  scale only axis,
  }
\newcommand*{\datafileLike}{(-3, -3) (2, 2)}

\begin{document}
  \begin{tikzpicture} [baseline, trim axis left, trim axis right]
    %
    \begin{axis} [
      xlabel = {\pgfmathprintnumber{\xminA} : \pgfmathprintnumber{\xmaxA}},
      ylabel = {\pgfmathprintnumber{\yminA} : \pgfmathprintnumber{\ymaxA}},
      axis x line* = bottom,
      axis y line* = left,
      width  = 6cm,
      height = 4cm,
     after end axis/.code={%
     \xdef\xminA{\pgfkeysvalueof{/pgfplots/xmin}}% e(X)panded global (DEF)inition
     \xdef\xmaxA{\pgfkeysvalueof{/pgfplots/xmax}}%
     \xdef\yminA{\pgfkeysvalueof{/pgfplots/ymin}}%
     \xdef\ymaxA{\pgfkeysvalueof{/pgfplots/ymax}}%
     }
      ] \addplot [black] coordinates {\datafileLike};
    \end{axis}
    %
    \begin{axis} [
      axis x line* = top,
      axis y line* = right,
      width  = 6cm,
      height = 4cm,
       xmin = \xminA*\ratioX,
       xmax = \xmaxA*\ratioX,
       ymin = \yminA*\ratioY,
       ymax = \ymaxA*\ratioY,
      ]
    \end{axis}
  \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容