[Tex/LaTex] pgfplots: x axis in log | prevent scaling (scientific notation) for extra x ticks

pgfplots

  • I am working on a 1/3 Octave Band Filter application.
  • I want to generate a plot with a logarithmic x-axis.
  • Remark: The black/grey grid shows the center frequencies.
  • Remark: The red grid shows the corner frequencies.

Problem 1 (Main Problem)

How can I prevent the scientific notation for extra x ticks (see screenshot)? I added a linear x-axis version where I found a solution:

xticklabel style = {
            /pgf/number format/fixed,
            /pgf/number format/precision = 0,
},
scaled x ticks = false

A workaround is to set the (extra x) labels manually using extra x tick labels.


Problem 2 (Minor Problem) –> Solved

How can I make the tick labels of the extra x ticks bold? (\bfseries) I guess the problem is, that the tick numbers are typeset in math mode.

Solved: How can I change the font family in pgfplots?


\documentclass[tikz]{standalone}

\usepackage{pgfplots}

\begin{document}

%% x axis is log
%
\begin{tikzpicture}
\begin{axis}[
        width = 200mm,
        height = 60mm,
        grid = both,
        xlabel = {Frequency in Hz},
        ylabel = {SPL in dB},
        axis y line = left, 
        axis x line = middle,
        xmin = 150,
        xmax = 20000,
        xticklabel style = {
%           /pgf/number format/fixed,
%           /pgf/number format/precision = 0,
            font = \tiny\sffamily,
        },
        %scaled x ticks = false,        
        xtick = {200, 250, 315, 400, 500, 630, 800, 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300, 8000, 10000, 12500, 16000, 20000},
        extra x ticks = {180, 224, 280, 355, 450, 560, 710, 899, 1120, 1410, 1800, 2240, 2800, 3550, 4500, 5600, 7100, 9000, 11200, 14100, 18000, 22400},
        extra x tick style = {
            major grid style = red,
            tick align = outside,
            tick style = {red, thin},
            major tick length = 5mm,
            },      
        ymin = 0,
        ymax = 10,      
        xmode = log,
        log ticks with fixed point,
    ]        
\end{axis}
\end{tikzpicture}

%% x axis is linear
%
\begin{tikzpicture}
\begin{axis}[
        width = 200mm,
        height = 60mm,
        grid = both,
        xlabel = {Frequency in Hz},
        ylabel = {SPL in dB},
        axis y line = left, 
        axis x line = middle,
        xmin = 150,
        xmax = 20000,
        xticklabel style = {
            /pgf/number format/fixed,
            /pgf/number format/precision = 0,
            font = \tiny\sffamily,
        },
        scaled x ticks = false,     
        xtick = {200, 250, 315, 400, 500, 630, 800, 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300, 8000, 10000, 12500, 16000, 20000},
        extra x ticks = {180, 224, 280, 355, 450, 560, 710, 899, 1120, 1410, 1800, 2240, 2800, 3550, 4500, 5600, 7100, 9000, 11200, 14100, 18000, 22400},
        extra x tick style = {
            major grid style = red,
            tick align = outside,
            tick style = {red, thin},
            major tick length = 5mm,
            },      
        ymin = 0,
        ymax = 10,      
%       xmode = log,
%       log ticks with fixed point,
    ]        
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

enter image description here

Related

Best Answer

For some reason that I cannot recall right now, the default style for extra tick labels activates a special tick formatting feature which is kicking you here.

More precisely, the offending default is

/pgfplots/every extra x tick/.style={
    /pgfplots/log identify minor tick positions=true,
    /pgfplots/hide obscured x ticks=false,
},

consequently, a simple fix is to add

\documentclass[tikz]{standalone}

\usepackage{pgfplots}

\begin{document}

%% x axis is log
%
\begin{tikzpicture}
\begin{axis}[
        width = 200mm,
        height = 60mm,
        grid = both,
        xlabel = {Frequency in Hz},
        ylabel = {SPL in dB},
        axis y line = left, 
        axis x line = middle,
        xmin = 150,
        xmax = 20000,
        xticklabel style = {
           /pgf/number format/fixed,
           /pgf/number format/precision = 0,
            font = \tiny\sffamily,
        },
        %scaled x ticks = false,        
        xtick = {200, 250, 315, 400, 500, 630, 800, 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300, 8000, 10000, 12500, 16000, 20000},
        extra x ticks = {180, 224, 280, 355, 450, 560, 710, 899, 1120, 1410, 1800, 2240, 2800, 3550, 4500, 5600, 7100, 9000, 11200, 14100, 18000, 22400},
        extra x tick style = {
            major grid style = red,
            tick align = outside,
            tick style = {red, thin},
            major tick length = 5mm,
            log identify minor tick positions=false,
            },      
        ymin = 0,
        ymax = 10,      
        xmode = log,
        log ticks with fixed point,
    ]        
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

I am wondering if this is actually more a bug than a feature... not sure if anyone ever used it. The idea of this feature is to identify extra tick labels in a log plot which are actually minor ticks like "1* 10^1, 2*10^1, 3 * 10^1" in a special way.