-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected behavior when using colorbar as legend
#465
Comments
With the help of \documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\pgfplotscolorbardrawstandalone[
colorbar as legend,
colorbar style={
ticklabel style={font=\tiny},
},
colormap access=const,
colormap={CM}{
of colormap={
viridis,
target pos={
0,200,300,350,375,
400,700,800,850,1000
},
sample for=const,
},
},
]
\end{document} |
This is caused by interactions between the If \pgfplotsforeachungrouped \x in {0,...,\pgfplots@colorcount} {%
\pgfplotsarrayselect{\x}\of\positions\to\pgfplotsretval
% ...
} in colorbar drawing, since pgfplots/tex/generic/pgfplots/pgfplots.code.tex Lines 1367 to 1376 in a1fe88f
pgfplots/tex/generic/pgfplots/liststructure/pgfplotsarray.code.tex Lines 207 to 225 in a1fe88f
A quick and very specific workaround diff --git a/tex/generic/pgfplots/pgfplots.code.tex b/tex/generic/pgfplots/pgfplots.code.tex
index 0b4486b..b72bfa6 100644
--- a/tex/generic/pgfplots/pgfplots.code.tex
+++ b/tex/generic/pgfplots/pgfplots.code.tex
@@ -1368,6 +1368,9 @@
% variable:
\def\pgfplots@loc@TMPc{}%
\pgfplotsforeachungrouped \x in {0,...,\pgfplots@colorcount} {%
+ % ensure \x is assignable to a TeX count,
+ % because \pgfplotsarrayselect will assign \x to a TeX count)
+ \pgfmathfloattoint{\x}\let\x=\pgfmathresult
\pgfplotsarrayselect{\x}\of\positions\to\pgfplotsretval
\edef\pgfplots@loc@TMPa{\noexpand\pgfmathprintnumber{\pgfplotsretval}}
\t@pgfplots@toka=\expandafter{\pgfplots@loc@TMPc}% I believe a thorough solution is to adapt \documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage{xpatch}
\makeatletter
% patch /pgfplots/colorbar as legend/.style={...}
\expandafter\xpatchcmd\csname pgfk@/pgfplots/colorbar as legend/.@cmd\endcsname
{%
\pgfplotsarrayselect{\x}\of\positions\to\pgfplotsretval
}{%
\pgfmathfloattoint{\x}\let\x=\pgfmathresult
\pgfplotsarrayselect{\x}\of\positions\to\pgfplotsretval
}{}{\PatchFailed}
\expandafter\xpatchcmd\csname pgfk@/pgfplots/colorbar as legend/.@body\endcsname
{%
\pgfplotsarrayselect{\x}\of\positions\to\pgfplotsretval
}{%
\pgfmathfloattoint{\x}\let\x=\pgfmathresult
\pgfplotsarrayselect{\x}\of\positions\to\pgfplotsretval
}{}{\PatchFailed}
\makeatother
\begin{document}
\pgfplotscolorbardrawstandalone[
colorbar as legend,
colorbar style={
ticklabel style={font=\tiny},
},
colormap access=const,
colormap={CM}{
of colormap={
viridis,
target pos={
0,200,300,350,375,
400,700,800,850,1000
},
sample for=const,
},
},
]
{
\pgfset{fpu=false}
\foreach \x in {1,...,5} {\x, }\par
\pgfplotsforeachungrouped \x in {1,2,...,5} {\x, }\par
\pgfplotsforeachungrouped \x in {1,...,5} {\x, }
\pgfset{fpu=true}
\foreach \x in {1,...,5} {\x, }\par
\pgfplotsforeachungrouped \x in {1,2,...,5} {\x, }\par % \x in fpu forms
\pgfplotsforeachungrouped \x in {1,...,5} {\x, } % \x in fpu forms
}
\end{document} |
It seems this behavior (for Alternatively, add a guard at the beginning of pgfplots/tex/generic/pgfplots/pgfplotsmeshplothandler.code.tex Lines 2516 to 2521 in a1fe88f
|
Another option: The diff --git a/tex/generic/pgfplots/pgfplots.code.tex b/tex/generic/pgfplots/pgfplots.code.tex
index 0b4486b..ba213d0 100644
--- a/tex/generic/pgfplots/pgfplots.code.tex
+++ b/tex/generic/pgfplots/pgfplots.code.tex
@@ -1367,13 +1367,16 @@
% accumulate a list of formatted positions into this
% variable:
\def\pgfplots@loc@TMPc{}%
- \pgfplotsforeachungrouped \x in {0,...,\pgfplots@colorcount} {%
+ \def\x{0}% or, is \c@pgf@counta safe here?
+ \pgfutil@loop
+ \unless\ifnum\x>\pgfplots@colorcount
\pgfplotsarrayselect{\x}\of\positions\to\pgfplotsretval
- \edef\pgfplots@loc@TMPa{\noexpand\pgfmathprintnumber{\pgfplotsretval}}
+ \edef\pgfplots@loc@TMPa{\noexpand\pgfmathprintnumber{\pgfplotsretval}}%
\t@pgfplots@toka=\expandafter{\pgfplots@loc@TMPc}%
\t@pgfplots@tokb=\expandafter{\pgfplots@loc@TMPa}%
\edef\pgfplots@loc@TMPc{\the\t@pgfplots@toka \the\t@pgfplots@tokb,}%
- }%
+ \edef\x{\the\numexpr\x+1}%
+ \pgfutil@repeat
\t@pgfplots@tokb=\expandafter{\pgfplots@loc@TMPc}%
\xdef\pgfplots@glob@TMPa{%
\noexpand\pgfplotsset{% |
When using the
colorbar as legend
key, the ticks on colorbar seem to be incorrect: from the third onwards, they are all the same as the second.The error is already visible in the official manual (1.18.1, page 293).
To make sure that I understood correctly what the key does, I checked version 1.16, which shows the expected result in the manual (1.16, page 290).
The text was updated successfully, but these errors were encountered: