Skip to content

Added discrete rainbow styles proposed by Paul Tol#121

Merged
echedey-ls merged 7 commits intogarrettj403:masterfrom
IchBinGROOT:discrete-rainbow
Sep 10, 2024
Merged

Added discrete rainbow styles proposed by Paul Tol#121
echedey-ls merged 7 commits intogarrettj403:masterfrom
IchBinGROOT:discrete-rainbow

Conversation

@IchBinGROOT
Copy link
Copy Markdown
Contributor

On his website Paul Tol proposes which colors to use when more than the regular 7 in the seven in the std-colors are needed. As different rainbow subsets are used depending on the number of colors needed, a simple skipping of colors from the maximum of 23 is not the proposed way, thus all 23 styles need to be included.

Copy link
Copy Markdown
Collaborator

@echedey-ls echedey-ls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @IchBinGROOT , this makes a great contribution to the project!

Two questions:

  • How would you name the styles? Right now, discrete-rainbow-* is a term that we both know from the author naming, have you considered any alternatives?
  • I'd like to test that the cyclers match Paul Tol's implementation. Do you recommend me any way to do so, maybe by sharing your generator script of the files?

I will leave this PR open for a few days so you can apply the changes whenever you want, and other people review as well.

Thanks for the contribution ❣️

# from Paul Tot's website: https://personal.sron.nl/~pault/

# Set color cycle
axes.prop_cycle : cycler('color', ['1965B0']) No newline at end of file
Copy link
Copy Markdown
Collaborator

@echedey-ls echedey-ls Aug 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a new line at the end of the mplstyle files? Just to be style-consistent with the existing codebase.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course I can add a new line, that is easily done in the generating script.

Comment thread scienceplots/styles/color/discrete-rainbow/discrete-rainbow-1.mplstyle Outdated
Comment thread CHANGES.md Outdated
Comment thread CHANGES.md Outdated
Comment on lines +1 to +4
v2.1.3 (05-Aug-2024)
====================
- Now all subdirectories of the ``styles`` folder are parsed for styles
- Added all 23 "discrete rainbow" styles proposed by Paul Tol on his website
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you merge these entries to the v2.1.2 section? That will be the next release.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can certainly do so

@echedey-ls
Copy link
Copy Markdown
Collaborator

Hey @IchBinGROOT , I saw there was no further activity in this PR so I continued your work.
My only issue with this current PR is that the set_bad from the colormaps is not available through stylesheets. It makes sense since stylesheets are not for cmaps, but whatever, I think it's a nice improvement to make people aware of this colormaps and ease the use. Maybe spread the word and make people aware of https://github.com/Descanonge/tol_colors. For multi-plots the user will need to use this repo unless the number of data categories is the same for all plots.

I found a typo in one of the colors btw.

Anyways, thank you very much @IchBinGROOT ❣️ . I've left updating documentation and the example file for you or anybody else. When I get some more time I may be able to do so and release a new version. Until then help is pretty much appreciated 😉

Code - click to expand

__rainbow_discrete comes from Paul Tol's website script. This script should be run in the folder the files want to be created in.

from string import Template
from pathlib import Path

def __rainbow_discrete(lut=None):
        """
        Define colormap 'rainbow_discrete'.
        """
        clrs = ['#E8ECFB', '#D9CCE3', '#D1BBD7', '#CAACCB', '#BA8DB4',
                '#AE76A3', '#AA6F9E', '#994F88', '#882E72', '#1965B0',
                '#437DBF', '#5289C7', '#6195CF', '#7BAFDE', '#4EB265',
                '#90C987', '#CAE0AB', '#F7F056', '#F7CB45', '#F6C141',
                '#F4A736', '#F1932D', '#EE8026', '#E8601C', '#E65518',
                '#DC050C', '#A5170E', '#72190E', '#42150A']
        indexes = [[9], [9, 25], [9, 17, 25], [9, 14, 17, 25], [9, 13, 14, 17,
                25], [9, 13, 14, 16, 17, 25], [8, 9, 13, 14, 16, 17, 25], [8,
                9, 13, 14, 16, 17, 22, 25], [8, 9, 13, 14, 16, 17, 22, 25, 27],
                [8, 9, 13, 14, 16, 17, 20, 23, 25, 27], [8, 9, 11, 13, 14, 16,
                17, 20, 23, 25, 27], [2, 5, 8, 9, 11, 13, 14, 16, 17, 20, 23,
                25], [2, 5, 8, 9, 11, 13, 14, 15, 16, 17, 20, 23, 25], [2, 5,
                8, 9, 11, 13, 14, 15, 16, 17, 19, 21, 23, 25], [2, 5, 8, 9, 11,
                13, 14, 15, 16, 17, 19, 21, 23, 25, 27], [2, 4, 6, 8, 9, 11,
                13, 14, 15, 16, 17, 19, 21, 23, 25, 27], [2, 4, 6, 7, 8, 9, 11,
                13, 14, 15, 16, 17, 19, 21, 23, 25, 27], [2, 4, 6, 7, 8, 9, 11,
                13, 14, 15, 16, 17, 19, 21, 23, 25, 26, 27], [1, 3, 4, 6, 7, 8,
                9, 11, 13, 14, 15, 16, 17, 19, 21, 23, 25, 26, 27], [1, 3, 4,
                6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 19, 21, 23, 25, 26,
                27], [1, 3, 4, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 20,
                22, 24, 25, 26, 27], [1, 3, 4, 6, 7, 8, 9, 10, 12, 13, 14, 15,
                16, 17, 18, 20, 22, 24, 25, 26, 27, 28], [0, 1, 3, 4, 6, 7, 8,
                9, 10, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 25, 26, 27, 28]]
        if lut is None or lut < 1 or lut > 23:
            lut = 22
        # trim the hash symbol from the color start and return a list of hex colors
        return [ clrs[i][1:] for i in indexes[lut-1] ]


def generate_discrete_rainbow(folder):
    """
    Generate 'rainbow_discrete' colormap.
    """
    template_str = """# Discrete rainbow color scheme with $colors_len color$s
# from Paul Tol's website: https://personal.sron.nl/~pault/

# Set color cycle
axes.prop_cycle : cycler('color', $colors_list)
"""
    template = Template(template_str)
    for i in range(1, 24):
        colors_list = __rainbow_discrete(i)
        colors_len = len(colors_list)
        file_path = folder / f'discrete-rainbow-{colors_len}.mplstyle'
        with open(file_path, 'w') as f:
            f.write(template.substitute(colors_list=colors_list, colors_len=colors_len, s='s' if colors_len > 1 else ''))

if __name__ == '__main__':
    output_folder = Path(__file__).parent
    generate_discrete_rainbow(output_folder)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants