4

I am trying to normalize the axis of a pgfplots graph by certain values, something like 2000 for the y-axis and 19099 for the x-axis. The y-axis looks nice as expected, but the x-axis being scaled by a not nice number, produces tick labels that are something like 0.52, 1.05, 1.57, etc. rather than 0.5, 1, 1.5 etc.

Is there a way to scale the axis but still place the ticks at nice locations automatically?

Minimal working example:

\documentclass{standalone}
  \usepackage{pgfplots}
  \begin{document}
    \begin{tikzpicture}
      \begin{axis}[
        xmin = 0,xmax = 70000,
        ymin = 0,ymax = 5000,
        scaled x ticks=manual:{}{\pgfmathparse{(#1)/(19099)}},
        scaled y ticks=manual:{}{\pgfmathparse{(#1)/(2000)}},
      ]
      \end{axis}
    \end{tikzpicture}
  \end{document}

output of code

UPDATE

Trying @Jake approach, gives me errors when I plot a function within my axis, see following:

\documentclass{standalone}
  \usepackage{pgfplots}
  \begin{document}
    \begin{tikzpicture}
      \begin{axis}[
        xmin = 0,xmax = 70000,
        ymin = 0,ymax = 5000,
        domain=0:70000,
        x coord trafo/.code={
          \pgfkeys{/pgf/fpu=true}
          \pgfmathparse{(#1)/(19099)}
          \pgfkeys{/pgf/fpu=false}
        },
        y coord trafo/.code={
          \pgfkeys{/pgf/fpu=true}
          \pgfmathparse{(#1)/(2000)}
          \pgfkeys{/pgf/fpu=false}
        },
%        scaled x ticks=manual:{}{\pgfmathparse{(#1)/(19099)}},
%        scaled y ticks=manual:{}{\pgfmathparse{(#1)/(2000)}},
        ]
        \addplot {2000+0.001*(x/60)^2};
      \end{axis}
    \end{tikzpicture}
  \end{document}

This produces errors, such as: ``! Illegal unit of measure (pt inserted).`

3

The scaled x ticks key only acts after the tick positions have already been determined, so they're not the way to go here. Instead, you should transform your coordinate system using x coord trafo/.code and y coord trafo/.code.

Note that for values as large as yours, you'll need to switch on the fpu library in the .code. When using xmin, xmax, etc., the fpu library needs to be deactivated after the calculation, but when plotting mathematical expressions, the library may not be deactivated. This is a bit cumbersome: You'll have to \pgflibraryfpuifactive to decide whether we're in a context where the libary should be switched on and off, or left as it is:

\documentclass{standalone}
  \usepackage{pgfplots}
  \begin{document}
    \begin{tikzpicture}
      \begin{axis}[
      xmin=0, xmax=70000,
      ymin=0, ymax=5000,
        domain=0:70000,
        x coord trafo/.code={
            \pgflibraryfpuifactive{
                \pgfmathparse{(#1)/(19099)}
            }{
                \pgfkeys{/pgf/fpu=true}
                \pgfmathparse{(#1)/(19099)}
                \pgfkeys{/pgf/fpu=false}
            }
        },
        y coord trafo/.code={
            \pgflibraryfpuifactive{
                \pgfmathparse{(#1)/(2000)}
            }{
                \pgfkeys{/pgf/fpu=true}
                \pgfmathparse{(#1)/(2000)}
                \pgfkeys{/pgf/fpu=false}
            }
        },
        ]
        \addplot {2000+0.001*(x/60)^2};
      \end{axis}
    \end{tikzpicture}
  \end{document}
  • this works quite nicely for coordinate plots. If I add a function to plot, it gives me errors, see update to original post. – Markus Oct 26 '12 at 15:01
  • @Markus: I've edited my answer. – Jake Oct 26 '12 at 15:11
  • @Jake should x filter/.code={\pgfmathparse{#1/19099}} be faster? Shouldn't it? (the same for y axis). You have to adapt the plot range. – Luigi Oct 26 '12 at 18:07
  • @Luigi: I'm not sure I understand what you mean. Just using that x filter will not work if you want to set xmin and xmax manually. – Jake Oct 26 '12 at 18:14
  • @Jake, set xmin=0, xmax=3.67 and that's all. – Luigi Oct 26 '12 at 18:22
1

Easier but dirtier than Jake's solution:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  xmin=0, xmax=3.67,
  ymin=0, ymax=5000,
  domain=0:70000,
  x filter/.code={\pgfmathparse{#1/19099}},
  scaled y ticks=manual:{}{\pgfmathparse{#1/2000}}
  ]
\addplot {2000+0.001*(x/60)^2};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Maybe dirtier, but still works! I will check if performance here is any different. Especially for lots of curves with lots of samples this might get cumbersome I guess. Thanks! – Markus Oct 30 '12 at 14:06

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.