3

I try to implement 'parula' Matlab colormap in pgfplots drawings. Here is the MWE for that palette with 10 points for interpolation:

\documentclass{article}
\usepackage{pgfplots}

\pgfplotsset{
colormap={parula}{
rgb255=(53,42,135)
rgb255=(15,92,221)
rgb255=(18,125,216)
rgb255=(7,156,207)
rgb255=(21,177,180)
rgb255=(89,189,140)
rgb255=(165,190,107)
rgb255=(225,185,82)
rgb255=(252,206,46)
rgb255=(249,251,14)}}

\begin{document}
\begin{tikzpicture}

\begin{axis}[
colorbar sampled,
colorbar style={ytick={0,0.1,...,1},samples=11},
point meta min=0,
point meta max=1
]

\end{axis}
\end{tikzpicture}
\end{document}

Strange thing is that produced colormap does not correpsond to the inserted interpolation points. In the following picture, there is comparison of the correct 'parula' colormap (left bar, colors correspond to the interpolation points) with the result produced by pgfplots (right bar, colors are wrong). In fact, two results are not consistent, especially at the edge positions - top and bottom of the palette.

enter image description here

Do you have any ideas what is going wrong here?

  • You cannot be sure unless you have them drawn on the same color model and scale. Even the medium you compare can make a difference (html, pdf and so on). As a colorblind, I couldn't care less :P Jake implemented it here tex.stackexchange.com/questions/228197/… – percusse May 31 '15 at 23:41
  • You're right. So I came up with eyedropper tool to the palette produced by pgfplots, and for instance, first blue color (0-0.1 range) has code (53,62,175) instead of (53,42,135) and last yellow one - (55,88,96) instead of (249,251,14). The wrong RGB coordinates are still on the palette, but aren't at the correct edge locations. I've seen Jake's answer, but still it doesn't solve my issue :( – user54824 Jun 1 '15 at 5:50
  • I just updated the other post with a new colormap but regarding this post is it possible that you are using very few sample points? I would do it myself but I don't trust my eyes ;) – percusse Jul 10 '15 at 9:27
3

The OPs code gives the expected result (from the point of view from PGFPlots), but I think "the user" expects that "the last shown color" in the colorbar should be the last defined color in the colormap.

With OPs code that "last color" is indeed used in the colorbar, but only at 1 (and above), so it is invisible. Luckily with PGFPlots v1.14 there where introduced many new features regarding colormaps and colorbars so it is now very easy to create the colorbar "the user expects".

For more details please have a look at the comments in the code.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        colormap={parula}{
            rgb255=(53,42,135)
            rgb255=(15,92,221)
            rgb255=(18,125,216)
            rgb255=(7,156,207)
            rgb255=(21,177,180)
            rgb255=(89,189,140)
            rgb255=(165,190,107)
            rgb255=(225,185,82)
            rgb255=(252,206,46)
            rgb255=(249,251,14)
        },
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            % use the following key--value to get the desired result
            colormap access=piecewise const,
            % (just to save (vertical) space in the answer)
            colorbar horizontal,
%            % -----
%            % (please note that also the following key isn't needed any more
%            %  in this solution. But then it could be that your PDF viewer
%            %  returns a strange result. For example in SumatraPDF this most
%            %  probably is a bug that is reported already
%            %  <https://github.com/sumatrapdfreader/sumatrapdf/issues/595>)
%            colorbar sampled,
%            % also this key isn't needed when using
%            % `colormap access=piecewise constant' and the number of "samples"
%            % equals the number of given colors in the colormap
%            colorbar style={
%                samples=11,
%            },
%            % -----
%            % if you need more or less colors than the colormap provides than
%            % you can use the new keys provided
%            % (see "Building Colormaps ased on other Colormaps" in
%            %  section 4.7.6 of the PGFPlots manual (v1.14) on page 198ff)
%            colormap={my sampled version of viridis}{
%                samples of colormap=(10 of viridis)
%            },
%            % -----
            point meta min=0,
            point meta max=1,
        ]
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of the colorbar from above code

  • great, it solves the issue! – user54824 Oct 31 '16 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.