Skip to content

Commit

Permalink
Merge pull request matplotlib#7639 from tacaswell/enh_color_names
Browse files Browse the repository at this point in the history
Enh color names
  • Loading branch information
efiring authored Jan 16, 2017
2 parents 3b9a92d + 317eaff commit 7555a7a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
4 changes: 4 additions & 0 deletions doc/users/colors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ it can be provided as:
* a name from the `xkcd color survey <https://xkcd.com/color/rgb/>`__
prefixed with ``'xkcd:'`` (e.g., ``'xkcd:sky blue'``)
* one of ``{'C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9'}``
* one of ``{'tab:blue', 'tab:orange', 'tab:green',
'tab:red', 'tab:purple', 'tab:brown', 'tab:pink',
'tab:gray', 'tab:olive', 'tab:cyan'}`` which are the Tableau Colors from the
'T10' categorical palette (which is the default color cycle).

All string specifications of color are case-insensitive.

Expand Down
9 changes: 7 additions & 2 deletions doc/users/dflt_style_changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ Colors in default property cycle
--------------------------------

The colors in the default property cycle have been changed from
``['b', 'g', 'r', 'c', 'm', 'y', 'k']`` to the `Vega category10 palette
<https://github.com/vega/vega/wiki/Scales#scale-range-literals>`__
``['b', 'g', 'r', 'c', 'm', 'y', 'k']`` to the category10
color palette used by `Vega
<https://github.com/vega/vega/wiki/Scales#scale-range-literals>`__ and
`d3
<https://github.com/d3/d3-3.x-api-reference/blob/master/Ordinal-Scales.md#category10>`__
originally developed at Tableau.


.. plot::

Expand Down
21 changes: 19 additions & 2 deletions lib/matplotlib/_color_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)

from collections import OrderedDict
import six


Expand All @@ -15,6 +15,24 @@
'w': (1, 1, 1)}


# These colors are from Tableau
TABLEAU_COLORS = OrderedDict((
('blue', '#1f77b4'),
('orange', '#ff7f0e'),
('green', '#2ca02c'),
('red', '#d62728'),
('purple', '#9467bd'),
('brown', '#8c564b'),
('pink', '#e377c2'),
('gray', '#7f7f7f'),
('olive', '#bcbd22'),
('cyan', '#17becf'))
)

# Normalize name to "tab:<name>" to avoid name collisions.
TABLEAU_COLORS = OrderedDict(
('tab:' + name, value) for name, value in TABLEAU_COLORS.items())

# This mapping of color names -> hex values is taken from
# a survey run by Randel Monroe see:
# http://blog.xkcd.com/2010/05/03/color-survey-results/
Expand Down Expand Up @@ -973,7 +991,6 @@
'green': '#15b01a',
'purple': '#7e1e9c'}


# Normalize name to "xkcd:<name>" to avoid name collisions.
XKCD_COLORS = {'xkcd:' + name: value for name, value in XKCD_COLORS.items()}

Expand Down
13 changes: 10 additions & 3 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

import numpy as np
import matplotlib.cbook as cbook
from ._color_data import BASE_COLORS, CSS4_COLORS, XKCD_COLORS
from ._color_data import BASE_COLORS, TABLEAU_COLORS, CSS4_COLORS, XKCD_COLORS


class _ColorMapping(dict):
Expand All @@ -85,7 +85,14 @@ def __delitem__(self, key, value):
_colors_full_map = {}
# Set by reverse priority order.
_colors_full_map.update(XKCD_COLORS)
_colors_full_map.update({k.replace('grey', 'gray'): v
for k, v in XKCD_COLORS.items()
if 'grey' in k})
_colors_full_map.update(CSS4_COLORS)
_colors_full_map.update(TABLEAU_COLORS)
_colors_full_map.update({k.replace('gray', 'grey'): v
for k, v in TABLEAU_COLORS.items()
if 'gray' in k})
_colors_full_map.update(BASE_COLORS)
_colors_full_map = _ColorMapping(_colors_full_map)

Expand Down Expand Up @@ -252,7 +259,7 @@ def to_hex(c, keep_alpha=False):
### Backwards-compatible color-conversion API

cnames = CSS4_COLORS
COLOR_NAMES = {'xkcd': XKCD_COLORS, 'css4': CSS4_COLORS}
COLOR_NAMES = {'xkcd': XKCD_COLORS, 'css4': CSS4_COLORS, 'tc': TABLEAU_COLORS}
hexColorPattern = re.compile("\A#[a-fA-F0-9]{6}\Z")


Expand Down Expand Up @@ -402,7 +409,7 @@ class Colormap(object):
"""
def __init__(self, name, N=256):
r"""
"""
Parameters
----------
name : str
Expand Down
20 changes: 19 additions & 1 deletion lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,9 +584,10 @@ def angled_plane(azimuth, elevation, angle, x, y):
assert_array_almost_equal(h, np.cos(np.radians(angle)))


def test_xkcd():
def test_color_names():
assert mcolors.to_hex("blue") == "#0000ff"
assert mcolors.to_hex("xkcd:blue") == "#0343df"
assert mcolors.to_hex("tab:blue") == "#1f77b4"


def _sph2cart(theta, phi):
Expand Down Expand Up @@ -668,6 +669,23 @@ def test_conversions():
hex_color)


def test_grey_gray():
color_mapping = mcolors._colors_full_map
for k in color_mapping.keys():
if 'grey' in k:
assert color_mapping[k] == color_mapping[k.replace('grey', 'gray')]
if 'gray' in k:
assert color_mapping[k] == color_mapping[k.replace('gray', 'grey')]


def test_tableau_order():
dflt_cycle = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728',
'#9467bd', '#8c564b', '#e377c2', '#7f7f7f',
'#bcbd22', '#17becf']

assert list(mcolors.TABLEAU_COLORS.values()) == dflt_cycle


if __name__ == '__main__':
import nose
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 comments on commit 7555a7a

Please sign in to comment.