0

I want to change my x axis tick labels to read 0K 20K 40K 60K 80K similar to my y axis tick labels in this chart:

enter image description here

My LaTeX MVP generating the image:

\begin{filecontents*}{data.csv}
iter;prod
0;-10589.143027099373
2080;-10137.34268961796
5948;-9823.312236854963
8461;-9531.740064852165
11565;-9374.010299137673
14779;-9174.160659364205
18133;-8812.173184097246
21281;-8524.717622361832
24346;-8334.931265113803
28693;-8260.530715627245
33657;-8144.811407051079
37692;-8026.3359694899145
41347;-7927.802055824076
45664;-7854.28445759018
50737;-7837.35420135146
54924;-7697.667649897329
59341;-7681.064772064068
64499;-7460.053337376205
71204;-7385.997574969703
76470;-7330.933540294809
78661;-7315.709786834038
81232;-7232.586421626356
\end{filecontents*}

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{csvsimple}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}
\begin{axis} [
        ymax=0,
        xticklabel = {
            \pgfmathparse{\tick/1000}
            \pgfmathprintnumber{\pgfmathresult}\,K
        },
%        scaled x ticks=false,
        yticklabel = {
            \pgfmathparse{\tick/1000}
            \pgfmathprintnumber{\pgfmathresult}\,K
        },
        scaled y ticks=false,
    ]
    \addplot +[blue,mark=none] table [x=iter, y=prod, col sep=semicolon] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}

Now when I uncomment the line scaled x ticks=false, I receive this error:

! Dimension too large.
<to be read again> 
                   \relax 
l.50     \end{axis}

? 
! Emergency stop.
<to be read again> 
                   \relax 
l.50     \end{axis}

!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on build/mvp.log.

This error message seems to be associated with divisions by zero. Due to my set denominator of 1000 I don't see how that could the cause. Therefore I have no idea why this method works for the y axis but not for the x axis. Could someone please point out what I'm doing wrong?

Update: axes with scaled ticks=base 10:-3,

Based on BambOo's suggestion: enter image description here

5
  • 1
    You error is more likely related to the fact that you handle relatively large numbers for the default TeX math engine. IMHO it is unnecessary to ad a K to each ticklabel, why do you not use it in the label ? e.g. xlabel={money [\si{\kilo\euro}] using the siunitx package
    – BambOo
    Commented May 5, 2020 at 12:04
  • By default the x tick labels are 0 2 4 6 8 and at the end there is a * 10^4 which is added in a new line. I want to avoid this type of visualization. Changing it to 0 20,000 40,000 60,000 80,000 is also not an option because it uses too much space.
    – oschlueter
    Commented May 5, 2020 at 12:55
  • So I'd still have to divide the tick labels by 1000 even if I move the K from the tick labels to the axis label. I tried that by removing the K from my code and lualatex still exists with the same error.
    – oschlueter
    Commented May 5, 2020 at 13:01
  • 1
    If tikc labels 0 20 40 60 80 is what you look for, simply use axis option scaled ticks=base 10:-3 instead of your custom xticklabel/yticklabel
    – BambOo
    Commented May 5, 2020 at 13:24
  • Thank you for the suggestion. I added scaled ticks=base 10:-3, to my axis and rendered the chart. I added a screenshot to the question. Now it shows * 10^3 instead of * 10^4. Is there another way I can hide this? I'd rather have a K appended to each axis label because to me they feel less "out of place".
    – oschlueter
    Commented May 5, 2020 at 15:36

1 Answer 1

2

The documentation of \pgfmathparse held the answer:

The math engine in pgf typically uses TEX’s internal arithmetics. That means: it is well suited for numbers in the range [−16384, 16384] and has a precision of 5 digits

The number range is typically too small for plotting applications. pgfplots improves the number range by means of \pgfkeys{/pgf/fpu}\pgfmathparse{1+41} to activate the “floating point unit” (fpu) and to apply all following operations in floating point.

Adding \pgfkeys{/pgf/fpu} into my blocks for xticklabel and yticklabel allows for the calculations required to render my axis as I want it to.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .