PGFPlots:显示一个 Y 轴在另一个 Y 轴上的百分比

PGFPlots:显示一个 Y 轴在另一个 Y 轴上的百分比

我正在绘制一些数据,将 N1 和 N2 情况与 N0 情况进行比较。我想以百分比形式显示 N1 和 N2 情况与 N0 情况相比减少了多少,并显示原始数据。

下面我有一个最低限度的工作示例

\documentclass{article}
%\usepackage[utf8]{inputenc}
%\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

% N0 Percentage = (N0 / N0) * 100
% N1 Percentage = (N1 / N0) * 100
% N2 Percentage = (N2 / N0) * 100
\begin{filecontents*}{data.csv}
Depth,N0   ,N1     ,N2   ,N0 Percentage,N1 Percentage,N2 Percentage
20   ,13.87,13.47  ,12.93,100          ,97.11607787  ,93.22278298
30   ,13.87,13.31  ,12.61,100          ,95.96250901  ,90.91564528
40   ,13.87,13.19  ,12.38,100          ,95.09733237  ,89.25739005
50   ,13.86,13.1   ,12.21,100          ,94.51659452  ,88.0952381
60   ,13.86,13.03  ,12.11,100          ,94.01154401  ,87.37373737
70   ,13.86,12.98  ,12.05,100          ,93.65079365  ,86.94083694
80   ,13.86,12.95  ,12.03,100          ,93.43434343  ,86.7965368
90   ,13.86,12.9399,12.06,100          ,93.36147186  ,87.01298701
100  ,13.86,12.94  ,12.11,100          ,93.36219336  ,87.37373737
110  ,13.86,12.96  ,12.19,100          ,93.50649351  ,87.95093795
120  ,13.86,12.99  ,12.3 ,100          ,93.72294372  ,88.74458874
130  ,13.86,13.03  ,12.42,100          ,94.01154401  ,89.61038961
140  ,13.86,13.09  ,12.56,100          ,94.44444444  ,90.62049062
150  ,13.85,13.14  ,12.71,100          ,94.87364621  ,91.76895307
160  ,13.86,13.21  ,12.88,100          ,95.31024531  ,92.92929293
170  ,13.86,13.28  ,13.05,100          ,95.81529582  ,94.15584416
180  ,13.86,13.36  ,13.22,100          ,96.39249639  ,95.38239538
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
    \pgfplotstableset{
      col sep=comma,
      trim cells,
    }
    \pgfplotstableread{data.csv}{\Data}
    %\pgfplotstabletypeset{\Data}

    \begin{axis}[
        ylabel = B(X) (uT),
        xlabel = Depth (mm),
    ]
        \addplot table [x = Depth, y = N0] {\Data}; \label{N0}; \addlegendentry{N0}
        \addplot table [x = Depth, y = N1] {\Data}; \label{N1}; \addlegendentry{N1}
        \addplot table [x = Depth, y = N2] {\Data}; \label{N2}; \addlegendentry{N2}
    \end{axis}


    \begin{axis}[
        axis y line=right,
        xticklabels={}, 
        xlabel={},
        yticklabel={\pgfmathprintnumber\tick\,\%},
        ymax = 100,
        ymin = 87,
    ]
        \addplot table [x = Depth, y = N0 Percentage] {\Data};
        \addplot table [x = Depth, y = N1 Percentage] {\Data};
        \addplot table [x = Depth, y = N2 Percentage] {\Data};
    \end{axis}

\end{tikzpicture}
\end{document}

这会生成如下图像: 我的尝试

百分比版本和原始数据版本应该完全重叠。唯一需要添加的是右侧的轴。我通过在 excel 中预先计算它们(作为 N1/N0*100 和 N2/N0*100)并将它们包含在 .csv 中(作为 N0、N1 和 N2 百分比)来找到百分比,但是,理想情况下,我希望能够以编程方式生成右侧 Y 轴并自动完美对齐图表。

这是我尝试复制的源图像(在 matplotlib 中制作)。 原件

数据相同,但在 python 中,我能够将百分比减少线绘制为水平虚线(此处显示为“-10%”),这给出了很好的指示。

这最终将成为组图的一部分,看起来像这样(该图位于左上角):

原始全文

答案1

这将找到所有值的最小值和最大值,并相应地添加第二个轴。存在最小差异,可能是舍入误差。例如,您计算的百分比N0始终为100,但值在13.85和之间变化13.87。这就是蓝线不精确重合的原因。对于N1和 也类似N2

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\newcommand{\findminmax}[3]{% cf https://tex.stackexchange.com/a/107364
  % Starting values for min and max
  \pgfplotsforeachungrouped\XX in {0,...,\the\numexpr\numrows-1}{
    \pgfplotstablegetelem{\XX}{#1}\of{\Data}
    \pgfmathsetmacro{#3}{max(#3,\pgfplotsretval)}
    \pgfmathsetmacro{#2}{min(#2,\pgfplotsretval)}
  }
}
% N0 Percentage = (N0 / N0) * 100
% N1 Percentage = (N1 / N0) * 100
% N2 Percentage = (N2 / N0) * 100
\begin{filecontents*}{data.csv}
Depth,N0   ,N1     ,N2   ,N0 Percentage,N1 Percentage,N2 Percentage
20   ,13.87,13.47  ,12.93,100          ,97.11607787  ,93.22278298
30   ,13.87,13.31  ,12.61,100          ,95.96250901  ,90.91564528
40   ,13.87,13.19  ,12.38,100          ,95.09733237  ,89.25739005
50   ,13.86,13.1   ,12.21,100          ,94.51659452  ,88.0952381
60   ,13.86,13.03  ,12.11,100          ,94.01154401  ,87.37373737
70   ,13.86,12.98  ,12.05,100          ,93.65079365  ,86.94083694
80   ,13.86,12.95  ,12.03,100          ,93.43434343  ,86.7965368
90   ,13.86,12.9399,12.06,100          ,93.36147186  ,87.01298701
100  ,13.86,12.94  ,12.11,100          ,93.36219336  ,87.37373737
110  ,13.86,12.96  ,12.19,100          ,93.50649351  ,87.95093795
120  ,13.86,12.99  ,12.3 ,100          ,93.72294372  ,88.74458874
130  ,13.86,13.03  ,12.42,100          ,94.01154401  ,89.61038961
140  ,13.86,13.09  ,12.56,100          ,94.44444444  ,90.62049062
150  ,13.85,13.14  ,12.71,100          ,94.87364621  ,91.76895307
160  ,13.86,13.21  ,12.88,100          ,95.31024531  ,92.92929293
170  ,13.86,13.28  ,13.05,100          ,95.81529582  ,94.15584416
180  ,13.86,13.36  ,13.22,100          ,96.39249639  ,95.38239538
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
    \pgfplotstableset{
      col sep=comma,
      trim cells,
    }
    \pgfplotstableread{data.csv}{\Data}
    %\pgfplotstabletypeset{\Data}
    \pgfplotstablegetrowsof{\Data}
    \pgfmathtruncatemacro{\numrows}{\pgfplotsretval}
    \pgfplotstablegetcolsof{\Data}
    \pgfmathtruncatemacro{\numcols}{\pgfplotsretval}
    \pgfmathsetmacro{\mymin}{100}
    \pgfmathsetmacro{\mymax}{0} 
    \findminmax{N0}{\mymin}{\mymax}
    \findminmax{N1}{\mymin}{\mymax}
    \findminmax{N2}{\mymin}{\mymax}
    \typeout{\numrows,\mymin,\mymax}
    \begin{axis}[
        ylabel = B(X) (uT),
        xlabel = Depth (mm),
        ymax=1.01*\mymax,
        ymin=\mymin-0.01*\mymax,
        legend style={at={(0.99,0.01)},anchor=south east}
    ]
        \addplot table [x = Depth, y = N0] {\Data}; \label{N0}; \addlegendentry{N0}
        \addplot table [x = Depth, y = N1] {\Data}; \label{N1}; \addlegendentry{N1}
        \addplot table [x = Depth, y = N2] {\Data}; \label{N2}; \addlegendentry{N2}
    \end{axis}


    \begin{axis}[
        axis y line=right,
        xticklabels={}, 
        xlabel={},
        yticklabel={\pgfmathprintnumber\tick\,\%},
        ymax = 101,
        ymin = 100*\mymin/\mymax-1,
    ]
        \addplot table [x = Depth, y = N0 Percentage] {\Data};
        \addplot table [x = Depth, y = N1 Percentage] {\Data};
        \addplot table [x = Depth, y = N2 Percentage] {\Data};
    \end{axis}

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容