3

I have an image I'm trying to recreate in pgfplots. We have a loglogaxis plot, onto which we would like to add an extra set of axis to show what these values correspond to under a simple transformation. On this site I found a solution on how to add a second axis and I've got the scales ligned up, but I need some suggestion on how the tick marks on the second axis can be transformed.

MWE follows below

  1. On the y-axis on the right I'd like to have the number from the axis on the left divided by some number, say 7.5e15. If I manually use yticks= to mess with it, those numbers no longer align with the numbers on the x-axis

    (I also need it for the horizontal axis with a different factor, not so important for now)

    BTW: I do not mind typing in the tick labels manually, they should just be place at the same position as on the left y-axis

Bonus questions:

  1. In loglogaxis how can I get 0.1, 1, 10, etc instead of 10^-1 etc
  2. What is the best method for specifying common settings? Here I have to set width and height in both loglogaxis envs

MWE:

\documentclass{standalone}
\usepackage[svgnames,dvipsnames]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13} 
\begin{document}

\begin{tikzpicture}
  \begin{loglogaxis}[
    xmin=0.4*1e6,
    xmax=1.2*1e9,
    ymin=1e12,
    ymax=5e18,
    width=12cm,
    height=9cm,
    unit vector ratio*=,
    ]
  \end{loglogaxis}
  \begin{loglogaxis}[
    axis y line*=right,
    axis x line*=top,
    xmin=0.4*1e-1,
    xmax=1.2*1e2, 
    ymin=1e-4, 
    ymax=5*1e2,
    width=12cm,
    height=9cm,
    unit vector ratio*=,
    ]
  \end{loglogaxis}
\end{tikzpicture}
\end{document}

enter image description here


Update: For those interested, I used Symbol 1s excellent solution to make this for one if the axis:

 \begin{loglogaxis}[
    axis x line*=none,xmajorticks=false,
    axis y line*=right,
    log number format basis/.code 2 args={
      % #1 = base, #2=  exponent   
      \pgfmathparse{(1/9.46)*10^(#2-15)}
      \pgfmathprintnumber[/pgf/number format/.cd,fixed,precision=3,fixed zerofill,]\pgfmathresult
    },
    ylabel={Some text},
  ]
  \end{loglogaxis}
3

The loglogaxis environment, in general a log-axis, uses log number format basis/.code 2 args to typeset the ticks. That is the key you can play with.

In the following example

  • right axis and top axis are treat separately so that one can define different log number format basis.
  • for right axis, it is currently $\frac{#1^{\pgfmathprintnumber{#2}}}{7.5\times10^{15}}$. You can insert some necessary calculation here. (for example \pgfmathparse{1/7.5} and \pgfmathparse{#2-16})
  • for top axis, it is currently an if-statement. This shows the possibility to replace 10^{-1}, 10^0, 10^1 by .1, 1, 10 case-by-case. Nevertheless, one might check if \pgfmathprintnumber provides what they want.

  • width and height are given to every axis.

\begin{tikzpicture}[/pgfplots/every axis/.style={
    xmin=0.4*1e6,
    xmax=1.2*1e9,
    ymin=1e12,
    ymax=5e18,
    width=12cm,
    height=9cm}]
  \begin{loglogaxis}
  \end{loglogaxis}
  \begin{loglogaxis}[
    axis x line*=none,xmajorticks=false,
    axis y line*=right,
    log number format basis/.code 2 args={
      $\frac{#1^{\pgfmathprintnumber{#2}}}{7.5\times10^{15}}$
    }
  ]
  \end{loglogaxis}
  \begin{loglogaxis}[
    axis x line*=top,
    axis y line*=none,ymajorticks=false,
    log number format basis/.code 2 args={
      \ifdim #2 pt=7pt
        \parbox{2cm}{\centering ten \\ to \\ the \\ seventh \\ power}
      \else
        $#1^{\pgfmathprintnumber{#2}}$
      \fi
    }
  ]
  \end{loglogaxis}
\end{tikzpicture}

\end{document}
  • Very nice, I'll apply this to the actual image tomorrow – daleif Jan 28 '16 at 18:52

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.