Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

For a publication I am preparing multiple plots with PGFPlots and would like to have them all look alike, i.e. having the same axis and identical ticks. For this I intended to use ymin and ymax, but then PGFPlots doesn't seem to respect scaled ticks=true.

I've tried to boil it down to the fairly minimal example below, showing the effect of setting the different parameters.. I'd like to achieve the plot in the lower right corner, but with scaled ticks. What am I doing wrong?

\documentclass{article}
\usepackage{pgfplots}
        \pgfplotsset{compat=newest,width=6cm}
\usepackage[pdftex,active,tightpage]{preview}
\begin{document}
    \begin{preview}
        \begin{tikzpicture}
            \begin{axis}[ title=Nothing ]
                \addplot [red] coordinates { (0,0.0003) (1,0.0004) };
            \end{axis}
        \end{tikzpicture}
        \begin{tikzpicture}
            \begin{axis}[ title={Scaled Ticks \emph{false}},
                    scaled ticks=false  ]
                \addplot [green] coordinates { (0,0.0003) (1,0.0004) };
            \end{axis}
        \end{tikzpicture}
        \begin{tikzpicture}
            \begin{axis}[ title={Scaled ticks \emph{true} with ymax},
                    scaled ticks=true,
                    ymax=0.0005 ]
                \addplot [blue] coordinates { (0,0.0003) (1,0.0004) };
            \end{axis}
        \end{tikzpicture}
        \begin{tikzpicture}
            \begin{axis}[ title={Scaled ticks \emph{true} with ymax \& ymin},
                    scaled ticks=true,
                    ymax=0.0005,
                    ymin=0 ]
                \addplot [cyan] coordinates { (0,0.0003) (1,0.0004) };
            \end{axis}
        \end{tikzpicture}
    \end{preview}
\end{document}

enter image description here

share|improve this question
    
I've just now seen that when I set ymin=0.0000001 in my last example, that the scaled ticks do work as expected. But is that really the solution? –  Habi Dec 21 '12 at 11:21
add comment

1 Answer

up vote 3 down vote accepted

This seems to be a bug in PGFPlots (update 22 January 2012: This has now been fixed): Whenever the minimum or maximum value of an axis is exactly 0, the scaling isn't applied. You can patch an internal macro to check and account for this.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest,width=6cm}

%
\makeatletter
\def\pgfplots@init@scaled@tick@for#1{%
    \global\def\pgfplots@glob@TMPa{0}%
    \expandafter\pgfplotslistcheckempty\csname pgfplots@prepared@tick@positions@major@#1\endcsname
    \ifpgfplotslistempty
        % we have no tick labels. Omit the tick scale label as well!
    \else
    \begingroup
    \ifcase\csname pgfplots@scaled@ticks@#1@choice\endcsname\relax
    % CASE 0 : scaled #1 ticks=false: do nothing here.
    \or
        % CASE 1 : scaled #1 ticks=true:
        %--------------------------------
        % the \pgfplots@xmin@unscaled@as@float  is set just before the data
        % scale transformation is initialised.
        %
        % The variables are empty if there is no datascale transformation.
        \expandafter\let\expandafter\pgfplots@cur@min@unscaled\csname pgfplots@#1min@unscaled@as@float\endcsname
        \expandafter\let\expandafter\pgfplots@cur@max@unscaled\csname pgfplots@#1max@unscaled@as@float\endcsname
        %
        \ifx\pgfplots@cur@min@unscaled\pgfutil@empty
            \edef\pgfplots@loc@TMPa{\csname pgfplots@#1min\endcsname}%
            \expandafter\pgfmathfloatparsenumber\expandafter{\pgfplots@loc@TMPa}%
            \let\pgfplots@cur@min@unscaled=\pgfmathresult
            \edef\pgfplots@loc@TMPa{\csname pgfplots@#1max\endcsname}%
            \expandafter\pgfmathfloatparsenumber\expandafter{\pgfplots@loc@TMPa}%
            \let\pgfplots@cur@max@unscaled=\pgfmathresult
        \fi
        %
        \expandafter\pgfmathfloat@decompose@E\pgfplots@cur@min@unscaled\relax\pgfmathfloat@a@E
        \expandafter\pgfmathfloat@decompose@E\pgfplots@cur@max@unscaled\relax\pgfmathfloat@b@E
        \ifnum\pgfmathfloat@a@E=0%
            \pgfmathfloat@a@E=\pgfmathfloat@b@E%
        \fi%
        \ifnum\pgfmathfloat@b@E=0%
            \pgfmathfloat@b@E=\pgfmathfloat@a@E%
        \fi%
        \ifnum\pgfmathfloat@b@E<\pgfmathfloat@a@E%
            \pgfmathfloat@b@E=\pgfmathfloat@a@E%
        \fi
        \xdef\pgfplots@glob@TMPa{\pgfplots@scale@ticks@above@exponent}%
        \expandafter\ifnum\pgfplots@glob@TMPa<\pgfmathfloat@b@E
            % ok, scale it:
            \multiply\pgfmathfloat@b@E by-1
            \xdef\pgfplots@glob@TMPa{\the\pgfmathfloat@b@E}%
        \else
            \xdef\pgfplots@glob@TMPa{\pgfplots@scale@ticks@below@exponent}%
            \expandafter\ifnum\pgfplots@glob@TMPa>\pgfmathfloat@b@E
                % ok, scale it:
                \multiply\pgfmathfloat@b@E by-1
                \xdef\pgfplots@glob@TMPa{\the\pgfmathfloat@b@E}%
            \else
                % no scaling necessary:
                \xdef\pgfplots@glob@TMPa{0}%
            \fi
        \fi
    \or
        % CASE 2 : scaled #1 ticks=base 10:
        %--------------------------------
        \c@pgf@counta=\csname pgfplots@scaled@ticks@#1@arg\endcsname\relax
        %\multiply\c@pgf@counta by-1
        \xdef\pgfplots@glob@TMPa{\the\c@pgf@counta}%
    \or
        % CASE 3 : scaled #1 ticks=real:
        %--------------------------------
        \pgfmathfloatparsenumber{\csname pgfplots@scaled@ticks@#1@arg\endcsname}%
        \global\let\pgfplots@glob@TMPa=\pgfmathresult
    \or
        % CASE 4 : scaled #1 ticks=manual:
        \expandafter\global\expandafter\let\expandafter\pgfplots@glob@TMPa\csname pgfplots@scaled@ticks@#1@arg\endcsname
    \fi
    \endgroup
    \fi
    \expandafter\let\csname pgfplots@tick@scale@#1\endcsname=\pgfplots@glob@TMPa%
}
\makeatother
%

\begin{document}
\begin{tikzpicture}
    \begin{axis}[ title={Scaled ticks \emph{true} with ymax \& ymin},
        scaled y ticks=true,
        ymax=0.0008, ymin=0
    ]
        \addplot [cyan, domain=0:10] {0.0001*x};
    \end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
Thanks for your elaborate answer which I'll use in the final version. The quick-and-dirty workaround for sending the manuscript to my co-authors was to use ymin=0.0000001 instead of ymin=0. –  Habi Dec 21 '12 at 14:51
    
Oh and BTW @Jake: Thanks for submitting sourceforge.net/tracker/…, I just wanted to submit a bug-report myself :) –  Habi Dec 21 '12 at 14:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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