8
  • 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

  • 5
    +1 for those mad MS Paint skills. – Henri Menke Apr 26 '18 at 23:44
6

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.

  • Thanks! Crazy - I did not get (or saw) a notification that you answered until I saw @Marmot's answer. Do you think that this will be fixed in future versions (if you still consider it rather a bug)? – Dr. Manuel Kuehner Apr 28 '18 at 12:26
  • 1
    Generally: yes, I would fix it in a future version... but I am wondering about backwards compatibility. If someone got used to it, he might expect it. In particular, changes to existing fixgures are unexpected. PErhaps I'll add it to the default of next "compat" level. – Christian Feuersänger Apr 28 '18 at 14:05
1

I certainly cannot compete with Christian Feuersänger, but since you pinged me, here is what I got.

\documentclass[tikz]{standalone}

\usepackage{pgfplots}

\begin{document}

%% x axis is log
%
\begin{tikzpicture}
\begin{axis}[
        width = 200mm,
        height = 60mm,
        grid = both,
        xlabel = {Frequency in kHz},
        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,
        },
        xticklabel={\pgfmathsetmacro{\mytick}{exp(\tick-ln(1000))}
        $\mathbf{\pgfmathprintnumber[fixed,fixed
        zerofill,precision=2]{\mytick}}$},
        %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}

%% original
%
\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

As you see, I cheated a bit by switching to kHz since otherwise the numbers get too big.

  • Thanks! I did not see Christian's answer until I saw your answer (not notification, don't know why). I will upvote other answers of you for compensation if I accept Christians answer (after I tested it). Is that a deal you could live with? And your cheat is very good - in my real document, I will probably use a different font or will somehow modify the diagram. – Dr. Manuel Kuehner Apr 28 '18 at 12:29
  • Is \pgfmathprintnumber[fixed,fixed (double fixed) a typo or needed? – Dr. Manuel Kuehner Apr 28 '18 at 12:32
  • @Dr.ManuelKuehner Of course, you should accept Christian's answer. And it is not a typo, see e.g. the example at the bottom of p. 320 of the pgfplots manual. Whether it is needed depends on your preferences. – user121799 Apr 28 '18 at 12:34
  • Ok. Thanks for eth hint. And I also would have assumed the log axes has a base of 10. – Dr. Manuel Kuehner Apr 28 '18 at 13:17

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.