11

I want to plot ONE dataset in a pgfplots and use TWO axis with different units on each axis. For example I have one dataset with temperature and want to print a °C scale on the left and a °F on the right.

quick paint example :-)

Thank you :-)

I didn't find the answer yet with google :-(

PS: This is such a great platform and it helps me so much - thanks for all the other answers on this side :-)

This is my code so far with the left axis:

\documentclass[a4paper,10pt]{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    xmin=0, xmax=100, %- show range
    ymin=345.95, ymax=346.17, %- show range
    domain=0:100, %- calc range
    ytick={346.16,346.11,...,340}, %- labels on the left side
    y tick label style={
      /pgf/number format/.cd,
        fixed,
        fixed zerofill,
        precision=2,
      /tikz/.cd
    },      
    samples=200,
    no markers,
    enlargelimits=false,
    xlabel={x-axis},
    ylabel={Temperature T[K]},  
]
\addplot gnuplot { (346.16 - 0.002 * x)} ; 
\addplot gnuplot [style=densely dotted] { 346.16 }
node[below left] at (axis cs:100,346.16) {$73^\circ$C};   


\end{axis}\end{tikzpicture}

\end{document}
3
  • You can start from this tex.stackexchange.com/questions/53029/… – percusse Jul 9 '13 at 19:49
  • thank you "percusse" but I do not understand your hint because I do not want to add two datasets! I only want two axis for one dataset??!!... – Thomas Jul 9 '13 at 19:54
  • You don't need to have two data sets. The axes should overlap to give the two ordinate look. You can modify one of the axis without drawing anything with no x axis ticks etc. If you add a simple example with the data set people can react much faster. – percusse Jul 9 '13 at 19:56
12

Here is a simple example though I've used the Fahrenheit instead of Kelvin.

\documentclass{article}
\usepackage{pgfplots,siunitx}
\pgfplotsset{compat=1.8}

\pgfplotstableread{
x celc
0 346.16
20 346.11
40 346.06
60 346.01
80 345.96
}\mytable


\begin{document}
\begin{tikzpicture}
    \begin{axis}[ylabel={\si{\degreeCelsius}}]
    \addplot[draw=none,no marks] table \mytable;
    \end{axis}
    \begin{axis}[
    axis y line*=right,
    ylabel={\si{\degree F}},
    yticklabel={\pgfmathparse{9*\tick/5+32}\pgfmathprintnumber[fixed,precision=2]\pgfmathresult},
    axis x line=none,
    ]
    \addplot table \mytable;
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

2
  • Thank you "percusse" for this great and simple solution :-) – Thomas Jul 18 '13 at 18:17
  • @Thomas My pleasure :) – percusse Jul 18 '13 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.