1

I am trying to create a boxplot of my data in pgfplots with square-root scaling of Y axis, due to the nature of data.

This is my code:

\documentclass{article} 
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.statistics}
\pgfplotsset{compat=1.17}
\usepackage{filecontents}

\begin{filecontents*}{file.csv}
a,b,c,d,e
0.0,2.842170943040401e-14,0.0,0.02581787109375,1.8994026184082031
0.0,0.3329588710026492,0.75,0.02837371826171875,0.4414405822753906
0.0,0.15510184685969364,0.375,0.1663665771484375,0.199951171875
0.0,0.000375662574413127,0.0,0.2701606750488281,0.8085689544677734
0.0,1.4210854715202004e-14,0.0,0.03917694091796875,0.25116539001464844
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    boxplot/draw direction=y,
    ymin=0., ymax=64.,
    y coord trafo/.code={\pgfmathparse{sqrt{#1}}},  % Y-axis forward transform
    y coord inv trafo/.code={\pgfmathparse{#1^2}},  % Y-axis backward transform
    table/row sep=newline,
    table/col sep=comma
]
\addplot+[boxplot={draw position=1}, fill, draw=black, solid, mark=none] table [y=a] {file.csv};
\end{axis}
\end{tikzpicture} 
\end{document}

I am getting an ambiguous error message

This is pdfTeX, Version 3.141592653-2.6-1.40.22 (TeX Live 2021) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./sqrt_plot.tex
LaTeX2e <2020-10-01> patch level 4
L3 programming layer <2021-02-18>
(/usr/local/texlive/2021/texmf-dist/tex/latex/base/article.cls
Document Class: article 2020/04/10 v1.4m Standard LaTeX document class
(/usr/local/texlive/2021/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2021/texmf-dist/tex/latex/pgfplots/pgfplots.sty
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgfplots/pgfplots.revision.tex)
(/usr/local/texlive/2021/texmf-dist/tex/latex/graphics/graphicx.sty
(/usr/local/texlive/2021/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/local/texlive/2021/texmf-dist/tex/latex/graphics/graphics.sty
(/usr/local/texlive/2021/texmf-dist/tex/latex/graphics/trig.sty)
(/usr/local/texlive/2021/texmf-dist/tex/latex/graphics-cfg/graphics.cfg)
(/usr/local/texlive/2021/texmf-dist/tex/latex/graphics-def/pdftex.def)))
(/usr/local/texlive/2021/texmf-dist/tex/latex/pgf/frontendlayer/tikz.sty
(/usr/local/texlive/2021/texmf-dist/tex/latex/pgf/basiclayer/pgf.sty
(/usr/local/texlive/2021/texmf-dist/tex/latex/pgf/utilities/pgfrcs.sty
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/utilities/pgfutil-common.te
x
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/utilities/pgfutil-common-li
sts.tex))
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/utilities/pgfutil-latex.def
) (/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/utilities/pgfrcs.code.tex
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/pgf.revision.tex)))
(/usr/local/texlive/2021/texmf-dist/tex/latex/pgf/basiclayer/pgfcore.sty
(/usr/local/texlive/2021/texmf-dist/tex/latex/pgf/systemlayer/pgfsys.sty
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/systemlayer/pgfsys.code.tex
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/utilities/pgfkeys.code.tex
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/utilities/pgfkeysfiltered.c
ode.tex))
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/systemlayer/pgf.cfg)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/systemlayer/pgfsys-pdftex.d
ef
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/systemlayer/pgfsys-common-p
df.def)))
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/systemlayer/pgfsyssoftpath.
code.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/systemlayer/pgfsysprotocol.
code.tex)) (/usr/local/texlive/2021/texmf-dist/tex/latex/xcolor/xcolor.sty
(/usr/local/texlive/2021/texmf-dist/tex/latex/graphics-cfg/color.cfg))
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/basiclayer/pgfcore.code.tex
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/math/pgfmath.code.tex
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/math/pgfmathcalc.code.tex
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/math/pgfmathutil.code.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/math/pgfmathparser.code.tex
)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/math/pgfmathfunctions.code.
tex
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/math/pgfmathfunctions.basic
.code.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/math/pgfmathfunctions.trigo
nometric.code.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/math/pgfmathfunctions.rando
m.code.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/math/pgfmathfunctions.compa
rison.code.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/math/pgfmathfunctions.base.
code.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/math/pgfmathfunctions.round
.code.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/math/pgfmathfunctions.misc.
code.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/math/pgfmathfunctions.integ
erarithmetics.code.tex)))
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/math/pgfmathfloat.code.tex)
) (/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/math/pgfint.code.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/basiclayer/pgfcorepoints.co
de.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/basiclayer/pgfcorepathconst
ruct.code.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/basiclayer/pgfcorepathusage
.code.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/basiclayer/pgfcorescopes.co
de.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/basiclayer/pgfcoregraphicst
ate.code.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/basiclayer/pgfcoretransform
ations.code.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/basiclayer/pgfcorequick.cod
e.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/basiclayer/pgfcoreobjects.c
ode.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/basiclayer/pgfcorepathproce
ssing.code.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/basiclayer/pgfcorearrows.co
de.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/basiclayer/pgfcoreshade.cod
e.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/basiclayer/pgfcoreimage.cod
e.tex
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/basiclayer/pgfcoreexternal.
code.tex))
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/basiclayer/pgfcorelayers.co
de.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/basiclayer/pgfcoretranspare
ncy.code.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/basiclayer/pgfcorepatterns.
code.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/basiclayer/pgfcorerdf.code.
tex)))
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/modules/pgfmoduleshapes.cod
e.tex)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/modules/pgfmoduleplot.code.
tex)
(/usr/local/texlive/2021/texmf-dist/tex/latex/pgf/compatibility/pgfcomp-version
-0-65.sty)
(/usr/local/texlive/2021/texmf-dist/tex/latex/pgf/compatibility/pgfcomp-version
-1-18.sty))
(/usr/local/texlive/2021/texmf-dist/tex/latex/pgf/utilities/pgffor.sty
(/usr/local/texlive/2021/texmf-dist/tex/latex/pgf/utilities/pgfkeys.sty
(/usr/local/texlive/2021/texmf-dist/tex/generic/pgf/utilities/pgfkeys.code.tex)
./sqrt_plot.tex:26: Missing \endcsname inserted.
<to be read again> 
                   \relax 
l.26 ... solid, mark=none] table [y=b] {file.csv};
                                                  
?                                      

It seems to be the problem with the coordinate scaling. However, when I replace the \addplot with following, it works.

\addplot { x^2 };  % this works

The problem must be related to, when coord trafo is combined with plotting data from CSV.

Can anybody help me, please?

7
  • As an aside, you can visualize sqrt functionality also on a logarithmic scale …
    – MS-SPO
    Commented Feb 21 at 15:44
  • I wonder if you could do tis as a parametric plot? t=x*x and y=sqrt(t). You can use a list of tick marks matching the t values instead of the y vallues. Commented Feb 21 at 15:46
  • @MS-SPO, it is not the same, log is steeper. Also, my data contains zeros. Commented Feb 21 at 15:52
  • @JohnKormylo You mean convert the data to square-roots and then "fake" the Y-axis tick labels? Understood, a bit clumsy though. Commented Feb 21 at 15:53
  • Yes. To a certain extent, the pgfplots tick marks are just as fake. Commented Feb 21 at 15:57

1 Answer 1

2

At some point the boxplot uses y coord trafo with something that is not a number. -otherwise it seems to work. I have copied isPGFNumber from here: https://tex.stackexchange.com/a/463819/8650 and use it to test if #1 is a number before doing the conversion.

A working boxplot on a square-root scale:

\documentclass[tikz, border=1cm]{standalone} 
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.statistics}
\newcommand{\isPGFNumber}[3]{% \isPGFNumber{<Number>}{<True>}{<False>}
    \begingroup
    \pgfkeys{/pgf/fpu/handlers/invalid number/.code={}}%
    \pgfmathfloatparsenumber{#1}%
    \global\pgfmathfloatgetflagstomacro\pgfmathresult\pgfFloatParseFlag%
    \endgroup
    \ifnum\pgfFloatParseFlag=3%
        #3%
    \else%
        #2%
    \fi%
}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymin=0., ymax=64.,
boxplot/draw direction=y,
y coord trafo/.code={\isPGFNumber{#1}{\pgfmathparse{sqrt(#1)}}{}},
y coord inv trafo/.code={\pgfmathparse{#1^2}},
]
\addplot+[boxplot]  table[row sep=\\,y index=0] {4\\ 4\\ 4\\ 36\\ 36\\ 36\\};
\addplot {x^2};
\end{axis}
\end{tikzpicture} 
\end{document}

A graph with a square-root scale and two plots

You must log in to answer this question.

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