2

I am plotting some data where I am comparing the N1 and N2 cases to the N0 case. I want to show how much the N1 and N2 case have reduced compared to the N0 case as a percentage, and also show the raw data.

I have a minimum worked example below

\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}

This produces the image: MyAttempt

The percentage version and the raw data version should completely overlap. The only addition should be the axis on the right hand side. I've found what the percentages are by precomputing them in excel (as N1/N0*100 and N2/N0*100) and including them in the .csv, as N0, N1 and N2 percentages, however, ideally I would like to be able to programmatically produce the right hand side Y axis and have the plots perfectly aligned automatically.

Here is the source image I'm trying to duplicate (was made in matplotlib). Orig

The data is the same, but in python I was able to plot the percentage reduction lines as a horizontal dashed line (here shown as "-10 %"), which gave a good indication.

This will ultimately be part of a groupplot, looking something like this (this plot is the upper left corner):

OrigFull

  • As an option to Cat´s answer, if you have already working plot from matplotlib, have a look at pythonTeX package. You can easily do stuff with it and then use it to also generate TeX-aware plots from matplotlib (and include them in TeX doc). – Tomáš Kruliš Dec 13 '19 at 08:11

1 Answers1

1

This finds the minimum and maximum of all values, and adds the second axis accordingly. There are tiniest discrepancies, probably rounding errors. For instance, your computed percentage for N0 is always 100, but the values vary between 13.85 and 13.87. This is why the blue lines do not coincide precisely. Similarly for N1 and 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}

enter image description here