Skip to content

Add solarized palette as named colors #11927

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

Closed

Conversation

josesho
Copy link

@josesho josesho commented Aug 24, 2018

PR Summary

  • Adds Ethan Schoonover's 16-color Solarized palette as named colors to matplotlib. They can be accessed by appending 'solarized:' to the relevant color name.

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution.

There are a few minor style issues. Otherwise good to go.

@@ -16,7 +16,8 @@
from matplotlib import colors as mcolors


colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS)
colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS,
**mcolors.SOLARIZED_COLORS)
Copy link
Member

Choose a reason for hiding this comment

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

Please align the continued line with the inside I’d the parentheses, I.e. remove two spaces.

# Copyright (c) 2011 Ethan Schoonover
SOLARIZED_COLORS = {
'solarized-base03': '#002b36',
'solarized-base02': '#073642',
Copy link
Member

Choose a reason for hiding this comment

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

Please align the color values with the color values of the CSS colors above.

@@ -50,7 +50,8 @@

import numpy as np
import matplotlib.cbook as cbook
from ._color_data import BASE_COLORS, TABLEAU_COLORS, CSS4_COLORS, XKCD_COLORS
from ._color_data import BASE_COLORS, TABLEAU_COLORS, CSS4_COLORS, \
Copy link
Member

Choose a reason for hiding this comment

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

For multi-line imports we prefer enclosing in brackets over line-continuation characters, i.e.

from ._color_data import (BASE_COLORS, TABLEAU_COLORS, CSS4_COLORS,
                          XKCD_COLORS, SOLARIZED_COLORS)

@timhoffm timhoffm added this to the v3.1 milestone Aug 24, 2018
# License: https://github.com/altercation/solarized/blob/master/LICENSE
# Copyright (c) 2011 Ethan Schoonover
SOLARIZED_COLORS = {
'solarized-base03': '#002b36',
Copy link
Contributor

@anntzer anntzer Aug 24, 2018

Choose a reason for hiding this comment

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

The namespacing method we use (cf. xkcd, tab) is "solarized:base03" (colon), not "solarized-base03" (dash).
Anyone can dismiss this once handled.

@josesho
Copy link
Author

josesho commented Aug 24, 2018

@timhoffm @anntzer Have addressed your comments; thanks for the review.

@anntzer anntzer dismissed their stale review August 24, 2018 09:19

handled

@jklymak
Copy link
Member

jklymak commented Aug 24, 2018

This largely seems good to me, but
https://12757-1385122-gh.circle-artifacts.com/0/home/circleci/project/doc/build/html/gallery/color/named_colors.html#sphx-glr-gallery-color-named-colors-py

doesnt' seem an optimal way to display these. Also, Tableau colors are not displayed. Maybe each color group should get its own subsection?

Display Tableau colors, and now each color group should get its own figure
@josesho
Copy link
Author

josesho commented Aug 25, 2018

@jklymak Have updated the named colors example to address your suggestions. Let me know if it's OK!

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

The idea of adding separate sections for the different color groups is great. However the current implementation is not sufficient IMHO. I expect all texts to be of the same font size. Also the color patches should all be of the same size and at best even be aligned across the groups.

https://12789-1385122-gh.circle-artifacts.com/0/home/circleci/project/doc/build/html/gallery/color/named_colors.html?highlight=visualizing%20named

You may improve this here or split it into another PR, if that's easier.

@josesho
Copy link
Author

josesho commented Aug 25, 2018

Noted, look our for a new PR here!

@timhoffm
Copy link
Member

timhoffm commented Aug 26, 2018

It's really a bit difficult to exactly control sizes and positions in such a plot. Try this for the color table:

import matplotlib.pyplot as plt
from matplotlib.colors import rgb_to_hsv, to_rgba

def plot_colortable(colors, title, sort_colors=True, ncols=4):
    extra_rows = 2  # additional space for title
    cell_width = 225
    cell_height = 30
    swatch_width = 50
    
    # Sort colors by hue, saturation, value and name.
    by_hsv = ((tuple(rgb_to_hsv(to_rgba(color)[:3])), name)
                    for name, color in colors.items())
    if sort_colors is True:
        by_hsv = sorted(by_hsv)
    names = [name for hsv, name in by_hsv]

    n = len(names)
    nrows = (n + 1) // ncols

    width = cell_width * ncols
    height = cell_height * (nrows + extra_rows)
    dpi = 72
    fig, ax = plt.subplots(figsize=(width / dpi, height / dpi), dpi=dpi)
    
    ax.set_xlim(0, width)
    ax.set_ylim(height, 0)
    ax.yaxis.set_visible(False)
    ax.xaxis.set_visible(False)
    ax.set_axis_off()
    ax.text(0, cell_height, title, fontsize=20)
    
    for i, name in enumerate(names):
        row = i % nrows
        col = i // nrows
        y = (row + extra_rows) * cell_height

        swatch_start_x = cell_width * col
        swatch_end_x = cell_width * col + swatch_width
        text_pos_x = cell_width * col + swatch_width + 5

        ax.text(text_pos_x, y, name, fontsize=12,
                horizontalalignment='left',
                verticalalignment='center')

        ax.hlines(y, swatch_start_x, swatch_end_x,
                  color=colors[name], linewidth=20)
    plt.show()

plot_colortable(mcolors.BASE_COLORS, "Base Colors", sort_colors=False, ncols=3)
plot_colortable(mcolors.CSS4_COLORS, "CSS Colors")
plot_colortable(mcolors.SOLARIZED_COLORS, "Solarized Palette", sort_colors=False)
plot_colortable(mcolors.TABLEAU_COLORS, "Tableau Palette")

@josesho
Copy link
Author

josesho commented Aug 27, 2018

OK, added a few tweaks to your update as well.

@josesho
Copy link
Author

josesho commented Aug 27, 2018

Tangent: Adding the Solarized palette led me to discover that Tableau updated their color palette in 2016.

Not sure if it's worth it to update the colors? (Not in this PR, of course.) I myself don't personally use Tableau or its palette, so the benefits are unclear to me.

@ImportanceOfBeingErnest
Copy link
Member

The list of which options there are to specify colors in matplotlib appears in three places:

Ultimately one would need to update all three tables with the new solarized:... colors.
This can also be a new PR of course, I just wanted to meantion it not to be forgotten.

Additional question: Do the "Accent colors" qualify to create a new listed (discrete) colormap from them, similar to how "tableau" colors are in the "tab10" colormap?

@timhoffm timhoffm dismissed their stale review October 13, 2018 15:31

Sorting of the named colors example handled separately.

@timhoffm
Copy link
Member

@josesho still interested in adding solarized colors? The color sorting has been handled separately in #12209. So this PR can be simplified again.

Please note also, that we do not write to whats_new.rst directly because it would create a lot of conflicts. You should instead create a separate file for the entry as described in https://github.com/matplotlib/matplotlib/tree/master/doc/users/next_whats_new.

@ImportanceOfBeingErnest
Copy link
Member

Relevant here: #12009 (comment). Essentially adding new named colors has been discussed in a monday's call and the overall opinion (though not shared by everyone) was not to add too many of them. So I'm not sure in how far this applies here.

@josesho
Copy link
Author

josesho commented Oct 15, 2018

I'll have a look at #12209 and modify this PR accordingly. 👌

@jklymak
Copy link
Member

jklymak commented Feb 26, 2019

ping @josesho: Pushing to 3.2, but feel free to update when you get a chance...

@jklymak jklymak modified the milestones: v3.1.0, v3.2.0 Feb 26, 2019
Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

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

As discussed in #12009, more and more "named colors" has to stop somewhere. The idea behind this PR is great, but we need a way to organize these custom lists better....

@josesho
Copy link
Author

josesho commented Mar 4, 2019

Been snowed under with work but let me get back to this and see what I need to do!

@jklymak
Copy link
Member

jklymak commented Mar 4, 2019

Sorry I should have been clearer; we need some sort of way to manage these lists of named colors. Otherwise we will have hundreds of them.

@tacaswell tacaswell modified the milestones: v3.2.0, v3.3.0 Aug 28, 2019
@anntzer
Copy link
Contributor

anntzer commented Aug 28, 2019

More or less in the same way as for the colormap discussion in some other thread, this could basically be implemented as a third-party package. In fact a quick googling shows that there's already https://pypi.org/project/pygments-solarized/ which exposes the desired colors in #rrggbb format:

from pygments_solarized import dark as sol
plt.plot([1, 2], c=sol.MAGENTA)

which is not much worse than "solarized:magenta"...
Hence I am tempted to just close this.

@josesho josesho closed this Aug 29, 2019
@tacaswell tacaswell modified the milestones: v3.3.0, unassigned Aug 29, 2019
@tacaswell
Copy link
Member

@josesho Could you put a note about this in the color docs?

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

Successfully merging this pull request may close these issues.

6 participants