Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7555a7a

Browse files
authored
Merge pull request #7639 from tacaswell/enh_color_names
Enh color names
2 parents 3b9a92d + 317eaff commit 7555a7a

File tree

5 files changed

+59
-8
lines changed

5 files changed

+59
-8
lines changed

doc/users/colors.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ it can be provided as:
1717
* a name from the `xkcd color survey <https://xkcd.com/color/rgb/>`__
1818
prefixed with ``'xkcd:'`` (e.g., ``'xkcd:sky blue'``)
1919
* one of ``{'C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9'}``
20+
* one of ``{'tab:blue', 'tab:orange', 'tab:green',
21+
'tab:red', 'tab:purple', 'tab:brown', 'tab:pink',
22+
'tab:gray', 'tab:olive', 'tab:cyan'}`` which are the Tableau Colors from the
23+
'T10' categorical palette (which is the default color cycle).
2024

2125
All string specifications of color are case-insensitive.
2226

doc/users/dflt_style_changes.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@ Colors in default property cycle
3232
--------------------------------
3333

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

3843
.. plot::
3944

lib/matplotlib/_color_data.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import (absolute_import, division, print_function,
22
unicode_literals)
3-
3+
from collections import OrderedDict
44
import six
55

66

@@ -15,6 +15,24 @@
1515
'w': (1, 1, 1)}
1616

1717

18+
# These colors are from Tableau
19+
TABLEAU_COLORS = OrderedDict((
20+
('blue', '#1f77b4'),
21+
('orange', '#ff7f0e'),
22+
('green', '#2ca02c'),
23+
('red', '#d62728'),
24+
('purple', '#9467bd'),
25+
('brown', '#8c564b'),
26+
('pink', '#e377c2'),
27+
('gray', '#7f7f7f'),
28+
('olive', '#bcbd22'),
29+
('cyan', '#17becf'))
30+
)
31+
32+
# Normalize name to "tab:<name>" to avoid name collisions.
33+
TABLEAU_COLORS = OrderedDict(
34+
('tab:' + name, value) for name, value in TABLEAU_COLORS.items())
35+
1836
# This mapping of color names -> hex values is taken from
1937
# a survey run by Randel Monroe see:
2038
# http://blog.xkcd.com/2010/05/03/color-survey-results/
@@ -973,7 +991,6 @@
973991
'green': '#15b01a',
974992
'purple': '#7e1e9c'}
975993

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

lib/matplotlib/colors.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565

6666
import numpy as np
6767
import matplotlib.cbook as cbook
68-
from ._color_data import BASE_COLORS, CSS4_COLORS, XKCD_COLORS
68+
from ._color_data import BASE_COLORS, TABLEAU_COLORS, CSS4_COLORS, XKCD_COLORS
6969

7070

7171
class _ColorMapping(dict):
@@ -85,7 +85,14 @@ def __delitem__(self, key, value):
8585
_colors_full_map = {}
8686
# Set by reverse priority order.
8787
_colors_full_map.update(XKCD_COLORS)
88+
_colors_full_map.update({k.replace('grey', 'gray'): v
89+
for k, v in XKCD_COLORS.items()
90+
if 'grey' in k})
8891
_colors_full_map.update(CSS4_COLORS)
92+
_colors_full_map.update(TABLEAU_COLORS)
93+
_colors_full_map.update({k.replace('gray', 'grey'): v
94+
for k, v in TABLEAU_COLORS.items()
95+
if 'gray' in k})
8996
_colors_full_map.update(BASE_COLORS)
9097
_colors_full_map = _ColorMapping(_colors_full_map)
9198

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

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

258265

@@ -402,7 +409,7 @@ class Colormap(object):
402409
403410
"""
404411
def __init__(self, name, N=256):
405-
r"""
412+
"""
406413
Parameters
407414
----------
408415
name : str

lib/matplotlib/tests/test_colors.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,10 @@ def angled_plane(azimuth, elevation, angle, x, y):
584584
assert_array_almost_equal(h, np.cos(np.radians(angle)))
585585

586586

587-
def test_xkcd():
587+
def test_color_names():
588588
assert mcolors.to_hex("blue") == "#0000ff"
589589
assert mcolors.to_hex("xkcd:blue") == "#0343df"
590+
assert mcolors.to_hex("tab:blue") == "#1f77b4"
590591

591592

592593
def _sph2cart(theta, phi):
@@ -668,6 +669,23 @@ def test_conversions():
668669
hex_color)
669670

670671

672+
def test_grey_gray():
673+
color_mapping = mcolors._colors_full_map
674+
for k in color_mapping.keys():
675+
if 'grey' in k:
676+
assert color_mapping[k] == color_mapping[k.replace('grey', 'gray')]
677+
if 'gray' in k:
678+
assert color_mapping[k] == color_mapping[k.replace('gray', 'grey')]
679+
680+
681+
def test_tableau_order():
682+
dflt_cycle = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728',
683+
'#9467bd', '#8c564b', '#e377c2', '#7f7f7f',
684+
'#bcbd22', '#17becf']
685+
686+
assert list(mcolors.TABLEAU_COLORS.values()) == dflt_cycle
687+
688+
671689
if __name__ == '__main__':
672690
import nose
673691
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)