3

I am plotting the distance between the Earth and the Moon, and the lunation cycle for three years, 2018–2020 inclusive, and these distances are on the order of 10^5 km. Ideally, I'd like the exponent in the axis description, and the y-axis to have the mantissa alone, without the exponent. Is this possible, without changing all my data?

A (broken) MWE below, and the resulting plot:

\documentclass[preview,11pt]{standalone}

\usepackage[dvipsnames]{xcolor}
\usepackage{pgfplots}
\usepackage{siunitx}
\pgfplotsset{compat=1.16}
\usetikzlibrary{
    arrows,
    arrows.meta,
    calc,
    positioning,
    decorations.pathreplacing,
    decorations.markings,
    calligraphy,
    pgfplots.dateplot
}

\pgfplotsset{tick style={black, thin}}

\begin{document}
\begin{tikzpicture}
    \pgfkeys{
        /pgf/number format/.cd,sci,sci generic=
        {mantissa sep={\times},exponent={10^{#1}}}
    }
    \begin{axis}[
        width=17.5cm,
        height=5cm,
        scaled y ticks = false,
        axis lines=middle,
        axis line style=->,
        date coordinates in=x,
        xmin=2018-01-01 00:00, xmax = 2021-01-03 00:00,
        xtick distance=366,
        minor x tick num = 12,
        xticklabel={\year},
        xlabel={Date},
        ylabel={Earth-Moon distance ($ \times $ \SI{e5}{\kilo\metre})},
        ymin=350000, ymax = 415000,
        ticklabel style = {font=\small},    
        x label style={at={(axis description cs:0.5,-0.15)},anchor=north},
        y label style={at={(axis description cs:-0.05,0.5)},rotate=90,anchor=south},
        legend columns=-1,
        legend cell align={left},
        legend entries={Distance, New moon, Full moon},
        legend style={
            at={(axis description cs:1,1.2)},
            anchor=east,
            /tikz/every even column/.append style={column sep=6pt},
        },
    ]
        \addplot[color=red,smooth,thick] table[x=Date,y=Range,col sep=comma] {moons.csv};
        \addplot[only marks,mark=*,mark options={fill=white},color=blue,mark size=1.5] table[x=Date,y=Range,col sep=comma] {newmoons.csv};
        \addplot[only marks,color=blue,mark size=1.5] table[x=Date,y=Range,col sep=comma] {fullmoons.csv};
        %\legend{Distance,New moon,Full moon}
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here As is evident, the y-axis labels have the exponent and mantissa as defined in the first few lines, \pgfkeys{/pgf/number format/.cd,sci,sci generic={mantissa sep={\times},exponent={10^{#1}}}}. I'd like to get rid of the × 10^5 bit, and keep the 3.6, 3.8 and 4.0, so that my y-axis label remains at the same place.

How may I go about doing this?

  • 1
    Yes, sure it is possible. The problem is that I cannot play with your file since the data file moons.csv is missing. You only need to change yticklabel. Without testing : yticklabel={$\pgfmathfloatparsenumber{\tick} \pgfmathfloattomacro{\pgfmathresult}{\F}{\M}{\E} \pgfmathprintnumber{\M}$}. – user121799 May 30 at 3:29
  • @marmot, you may find the data here. I tried that, but the exponents are now × 10^0 instead. EDIT: I realised I had left the \pgfkeys... line in. Removing that works perfectly. Could you explain your solution, so I could mark it as an answer? Thank you very much! – SRSR333 May 30 at 3:39
3

Rather than providing a solution that works with your data which not everyone has, I base this answer on a simple plot. First of all the ticks are controlled by yticklabel (and xticklabel as well as zticklabel), which the pgfplots manual (v 1.16) discusses extensively on pp. 339. Basically you can wrap any macro around \tick. The second piece of information can be found in section 56 Floating Point Unit Library of the pgfmanual v 3.1.3. It teaches you how to extract the mantissa from a floating point number. Combining these one can come up with something like

     yticklabel={$\pgfmathfloatparsenumber{\tick}
        \pgfmathfloattomacro{\pgfmathresult}{\F}{\M}{\E}
        \pgfmathprintnumber{\M}$}  

which you can massage further if needed.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[scaled y ticks=false,
    yticklabel={$\pgfmathfloatparsenumber{\tick}
        \pgfmathfloattomacro{\pgfmathresult}{\F}{\M}{\E}
        \pgfmathprintnumber{\M}$}
        ]
 \addplot[domain=0:50,samples=501]{1.2*10^5*sin(deg(x))};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Wonderful. Props for generalising the answer for any data, too. – SRSR333 May 30 at 4:08
2

If you really only want to keep the mantissa, marmot's answer is perfectly fine. But as you can see in his resulting image, this may generally not be what the user wants. Normally I guess one would like to keep the sign (for the negative region) and also the relative scaling of the numbers (0, 0.5, 1 instead of 0, 5, 1).

So here I provide a solution that just modifies the scaled ticks key and friends to achieve the (most likely really) desired result. The easiest way would just to use the default scaling and then "through away" the tick scale label. By providing a proper scaled ticks value you could also adjust the numbers as you wish.

(Besides that there are plenty of other ways to achieve what you want by either manipulating the table data using e.g. y expr (and friends) or using y filter (and friends).)

% used PGFPlots v1.16
% (Used code of marmot's answer as basis. Thank you for that!)
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        scaled y ticks={base 10:-3},
        ytick scale label code/.code={},
    ]
        \addplot [domain=0:50,samples=501] {1.2e5 * sin(deg(x))};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

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.