pgfplots: xtick, xticklabels and extra x ticks

1,746

Ignore my comment

In pgfplots.code.tex line 6888-90

  \expandafter\ifx\csname pgfplots@extra@#1ticklabel\endcsname\pgfutil@empty 
      \pgfutil@namelet{pgfplots@extra@#1ticklabel}{pgfplots@#1ticklabel}% 
  \fi

This shows that this is intentionally designed to use xticklables as fallback of extra x tick labels.

The workaround is simple, assign extra x tick label or extra x tick labels.

The expected behavior

@Dr. Manuel Kuehner suggests that the expected behavior is to simply print the x-coordinate. This makes more sense than I thought. In that case, we should replace the three lines by the following

\expandafter\ifx\csname pgfplots@extra@#1ticklabel\endcsname\pgfutil@empty
    \expandafter\def\csname pgfplots@extra@#1ticklabel\endcsname{\axisdefaultticklabel}%
\fi

By replace, I mean to edit pgfplots.code.tex or to redefine \pgfplots@init@ticks@for#1.


By the way, in the same file, if we look at line 6881-90, we see

\expandafter\ifx\csname pgfplots@#1ticklabel\endcsname\pgfutil@empty
  \pgfplots@if{pgfplots@#1islinear}{%
      \expandafter\def\csname pgfplots@#1ticklabel\endcsname{\axisdefaultticklabel}%
  }{%
      \expandafter\def\csname pgfplots@#1ticklabel\endcsname{\axisdefaultticklabellog}%
  }% 
\fi
\expandafter\ifx\csname pgfplots@extra@#1ticklabel\endcsname\pgfutil@empty
  \pgfutil@namelet{pgfplots@extra@#1ticklabel}{pgfplots@#1ticklabel}%
\fi

translate to English

  • if the user does not assign how to print normal labels, simply print the x-coordinate
  • if the user does assign how to print normal labels, obey the user.
  • if the user does not assign how to print extra labels, treat extra labels as they are normal labels.

The logic behind is that, sometimes the user prefers 0cm, 1cm, and 5cm rather than 0, 1, and 5. So the user may assign

xticklabel={$\pgfmathprintnumber{\tick}$cm}

In this case, the user probably want -4cm and -2cm instead of -4 and -2, so PGFPlots will inherit the formatting and add cm to extra labels.


So the problem is, how to implement the following logic?

  • if the user does not assign how to print normal labels, simply print the x-coordinate
  • if the user does assign how to print normal labels, obey the user.
  • if the user does not assign how to print extra labels:
    • if the user assign xticklabel, treat extra labels as they are normal labels.
    • if the user assign xticklabels, simply print the x-coordinate.

You probably want to check if \pgfplots@xticklabel is empty. (Do you?)

Share:
1,746

Related videos on Youtube

Dr. Manuel Kuehner
Author by

Dr. Manuel Kuehner

Updated on March 18, 2020

Comments

  • Dr. Manuel Kuehner
    Dr. Manuel Kuehner over 3 years
    \documentclass[tikz]{standalone}
    
    \usepackage{pgfplots}
    
    \begin{document}
    
    \begin{tikzpicture}
        \begin{axis}[
            xtick = {0,1,5},
            xticklabels = {{zero},$\alpha$,$\varphi$},
            extra x ticks = {-4,-2},
            extra x tick style = {
                                red,
                                font=\bfseries
                                },
        ]
        \addplot {x^2 - x +4};
        \end{axis}
    \end{tikzpicture}
    
    \end{document}
    

    enter image description here

    • I use xtick with custom labels (xticklabels).
    • In addition, I want some extra x ticks.
    • I expect that the extra x ticks are printed automatically but instead, the xticklabels are used.
    • I did not define extra x tick labels.
    • Stefan Pinnow
      Stefan Pinnow over 6 years
      I consider this being a bug which I added to the PGFPlots Tracker. Good catch!
    • Dr. Manuel Kuehner
      Dr. Manuel Kuehner over 6 years
      @StefanPinnow Thanks for the reply. Your code additions are helpful.
    • Symbol 1
      Symbol 1 over 6 years
      I am a little bit confused. Can you assign xtick={0,1,5,-4,-2}?
    • Dr. Manuel Kuehner
      Dr. Manuel Kuehner over 6 years
      @Symbol1 I am not sure that I understand your comment. Surely there is a work around, if that's what you mean. I am more concerned about the unexpected behavior.
    • Symbol 1
      Symbol 1 over 6 years
      IMHO, there is no expected behavior. If I were the package author, I would expect the user to assign xtick in just one place and assign xticklabels in just one place. The only scenario that the extra part is useful is when xtick=something automatic and extra x ticks=something manual. Anyway, this is quite unexpected because I looked into the source code and I cannot find out where is this logic implemented.
    • Dr. Manuel Kuehner
      Dr. Manuel Kuehner over 6 years
      @Symbol1 Ok. Maybe we have different expectations (no problem). Instead of the red text I would expect $-4$ and $-2$. But let's just see whet the developer has to say about the "bug" report.
    • Stefan Pinnow
      Stefan Pinnow over 6 years
      I'm voting to close this question because the bug was solved in PGFPlots v1.15
    • Dr. Manuel Kuehner
      Dr. Manuel Kuehner over 6 years
      @StefanPinnow Alright :)
  • Dr. Manuel Kuehner
    Dr. Manuel Kuehner over 6 years
    Thanks for the workaround! I still think that this is not (at least my) expected behavior. But your code provides a solution.
  • Symbol 1
    Symbol 1 over 6 years
    @Dr.ManuelKuehner: I updated the answer, I hope I can give as much information as you need.
  • Dr. Manuel Kuehner
    Dr. Manuel Kuehner over 6 years
    Wow -- a very elaborate answer. Thanks for the deep dive into the pgfplots code. The xticklabel=$\pgfmathprintnumber{\tick}$cm} might be useful in the future :).
  • Christian Feuersänger
    Christian Feuersänger over 6 years
    Thanks for the good pre-analysis, @Symbol1. I have fixed the issue in the codebase along your suggested definition (which solves the issue at hand and is the best default that I can think of).