Graphics, Figures & Tablespgfplots | Two y Axes in one Plot

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Heresy
Posts: 12
Joined: Mon May 21, 2012 7:24 pm

pgfplots | Two y Axes in one Plot

Post by Heresy »

Dear All,

I would to have two plots on the same plot (hence two different y axes). While each plot can be easily obtained separately, I am struggling to plot them all together (actually none of them work). Another issue is that the label for the right y axis appears on the left of the left y axis.

Please find attached a MWE, based on an example found in the pgfplots Gallery. If this is not feasible, what other method can I use (Gnuplot should work)?

Thank you.

Sincerely.
Attachments
Paper.tex
MWE that doesn't work.
(2.6 KiB) Downloaded 718 times
TeX Live 2012 and Texmaker 4.0.2

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

pgfplots | Two y Axes in one Plot

Post by cgnieder »

It seems you need to specify the domain. The position of the label of the right y axis is a bit hacked in the example below but it seems to work:

Code: Select all

\documentclass{article}
\usepackage{pgfplots}
\usepackage{siunitx}
\DeclareSIUnit\fahrenheit{\degree F}
\DeclareSIUnit\pound{lbm}
\sisetup{per-mode=symbol}
\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    xmin = 700, xmax = 2000,
    ymin = 0, ymax = 50,
    axis y line* = left, % the '*' avoids arrow heads
    xlabel = $p_0 \, \mathrm{(psi)}$,
    ylabel = $\dot{m}_{choked} \, (\si{\pound\per\second})$]
    \addplot[red,domain=700:2000]{0.019*x};
  \end{axis}
  \begin{axis}[
    xmin = 750, xmax = 2000,
    ymin = 1400, ymax = 1800,
    hide x axis,
    hide y axis]
    \addplot[blue,domain=750:1800]{4800*(4.5/x)^(0.2)};
  \end{axis}
  \pgfplotsset{every axis y label/.append style={rotate=180,yshift=9.5cm}}
  \begin{axis}[
        xmin=0, xmax=1,
        ymin=1400, ymax=1800,
        hide x axis,
        axis y line*=right,
        ylabel={$T_e \, (\si{\fahrenheit})$}
    ]
  \end{axis}
\end{tikzpicture}

\end{document}
twoplots.png
twoplots.png (16.94 KiB) Viewed 43542 times
Regards
site moderator & package author
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

pgfplots | Two y Axes in one Plot

Post by localghost »

cgnieder wrote:[…] The position of the label of the right y axis is a bit hacked in the example below but it seems to work […]
This can be done a bit easier by the ylabel near ticks option for the axis environment.

Code: Select all

\documentclass{standalone}
\usepackage{mathtools}
\usepackage{pgfplots}
\usepackage{siunitx}

\sisetup{per-mode=symbol}

\DeclareSIUnit{\fahrenheit}{\degree F}
\DeclareSIUnit{\pound}{lbm}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
      xmin=700,xmax=2000,
      ymin=0,ymax=50,
      axis y line*=left,
      xlabel={$p_0$ (psi)},
      xlabel near ticks,
      ylabel={$\dot{m}_\text{choked}$ (\si{\pound\per\second})},
      ylabel near ticks
    ]
      \addplot[red,domain=700:2000]{0.019*x};
    \end{axis}
    \begin{axis}[
      xmin = 750, xmax = 2000,
      ymin = 1400, ymax = 1800,
      hide x axis,
      axis y line*=right,
      ylabel={$T_e$ (\si{\fahrenheit})},
      ylabel near ticks
    ]
      \addplot[blue,domain=750:1800]{4800*(4.5/x)^(0.2)};
    \end{axis}
  \end{tikzpicture}
\end{document}
Attachments
The resulting output.
The resulting output.
pgfplots-two-y-axis.png (12.55 KiB) Viewed 43540 times
Heresy
Posts: 12
Joined: Mon May 21, 2012 7:24 pm

Re: pgfplots | Two y Axes in one Plot

Post by Heresy »

Thanks a lot for your help, cgnieder and localghost. This is awesome!

I only found afterwards three websites dealing with plotting with pgfplots (could be useful for someone else):
http://tex.stackexchange.com/questions/ ... ng-pgfplot
http://tex.stackexchange.com/questions/ ... is-scaling
http://www.latex-community.org/forum/vi ... 45&t=12560

Regards,
Heresy
TeX Live 2012 and Texmaker 4.0.2
Post Reply