-
Notifications
You must be signed in to change notification settings - Fork 35
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).
![1.18.1 Manual, page 293](https://private-user-images.githubusercontent.com/85574806/256702811-b8488669-8136-4210-a7c0-248788019f50.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU2NzkxODcsIm5iZiI6MTcyNTY3ODg4NywicGF0aCI6Ii84NTU3NDgwNi8yNTY3MDI4MTEtYjg0ODg2NjktODEzNi00MjEwLWE3YzAtMjQ4Nzg4MDE5ZjUwLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA5MDclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwOTA3VDAzMTQ0N1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTI5YWUyOGQ0YzdiOTkxNjA0NWM5OWI0NjhmY2Q0NGJjMjRlMzU4NjdjMmVkMjRjMzc3NDBkY2QyZmJmZDEwMDMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.lzPecRT4Pcrc1nfB1rUcangZVbK8HeZ6ENibIFcMgD4)
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).
![1.16 Manual, page 290](https://private-user-images.githubusercontent.com/85574806/256703374-4165f4f4-b2b4-45e6-aac2-2e02d3079b10.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU2NzkxODcsIm5iZiI6MTcyNTY3ODg4NywicGF0aCI6Ii84NTU3NDgwNi8yNTY3MDMzNzQtNDE2NWY0ZjQtYjJiNC00NWU2LWFhYzItMmUwMmQzMDc5YjEwLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA5MDclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwOTA3VDAzMTQ0N1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPThmOWM4NzQxNWVkNjYyNGM2YWU2MzA0MDA0ZDViNGU4OTFlNWViMzUyMGYwNjE5NDRhYTM2MjI3ZDNlYjM3YjMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.anI0iOfgP8slMZm-u4z1c94F_ZpXFiWwVdXcoU4kGgQ)
The text was updated successfully, but these errors were encountered: