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

Skip to content

Commit 6fdf978

Browse files
authored
Merge pull request #100 from lukelbd/cycles-improvements
Cycle show improvements
2 parents fa51170 + b7afd01 commit 6fdf978

File tree

10 files changed

+97
-41
lines changed

10 files changed

+97
-41
lines changed

‎proplot/cycles/538.hex‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
# From the 538 matplotlib stylesheet
12
'#008fd5', '#fc4f30', '#e5ae38', '#6d904f', '#8b8b8b', '#810f7c',

‎proplot/cycles/Qual1.rgb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Unknown source
12
91,190,148
23
145,190,100
34
229,207,108

‎proplot/cycles/Qual2.rgb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Unknown source
12
91,190,148
23
182,227,209
34
145,190,100

‎proplot/cycles/bmh.hex‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# From bmh stylesheet
2+
'#348ABD', '#A60628', '#7A68A6', '#467821', '#D55E00', '#CC79A7', '#56B4E9', '#009E73', '#F0E442', '#0072B2'

‎proplot/cycles/classic.hex‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Matplotlib classic 'bgrcmyk' style
2+
'#0000ff', '#008000', '#ff0000', '#00bfbf', '#bf00bf', '#bfbf00', '#000000'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
# Seaborn and proplot default style
12
'#0072B2', '#D55E00', '#009E73', '#CC79A7', '#F0E442', '#56B4E9',
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
# Expanded seaborn default style
12
"#0173B2", "#DE8F05", "#029E73", "#D55E00", "#CC78BC", "#CA9161", "#FBAFE4", "#949494", "#ECE133", "#56B4E9"

‎proplot/cycles/default.hex‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
# Matplotlib latest default style
12
'#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf',
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# From solarized stylesheet
2+
'#268BD2', '#2AA198', '#859900', '#B58900', '#CB4B16', '#DC322F', '#D33682', '#6C71C4'

‎proplot/styletools.py‎

Lines changed: 85 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,26 @@
3939
]
4040

4141
# Colormap stuff
42+
CYCLES_TABLE = {
43+
'Matplotlib originals': (
44+
'default', 'classic',
45+
),
46+
'Matplotlib stylesheets': (
47+
'colorblind', 'colorblind10', 'ggplot', 'bmh', 'solarized', '538',
48+
),
49+
'ColorBrewer2.0 qualitative': (
50+
'Accent', 'Dark2',
51+
'Paired', 'Pastel1', 'Pastel2',
52+
'Set1', 'Set2', 'Set3',
53+
),
54+
'Other qualitative': (
55+
'FlatUI', 'Qual1', 'Qual2', 'Viz',
56+
),
57+
'ProPlot originals': (
58+
'Cool', 'Warm', 'Hot',
59+
'Floral', 'Contrast', 'Sharp',
60+
),
61+
}
4262
CMAPS_TABLE = {
4363
# Assorted origin, but these belong together
4464
'Grayscale': (
@@ -126,7 +146,7 @@
126146
),
127147
'Other': (
128148
'binary', 'bwr', 'brg', # appear to be custom matplotlib
129-
'cubehelix', 'wistia', 'CMRmap', # individually released
149+
'cubehelix', 'Wistia', 'CMRmap', # individually released
130150
'seismic', 'terrain', 'nipy_spectral', # origin ambiguous
131151
'tab10', 'tab20', 'tab20b', 'tab20c', # merged colormap cycles
132152
)
@@ -1835,17 +1855,23 @@ def __init__(self, kwargs):
18351855
if not isinstance(key, str):
18361856
raise KeyError(f'Invalid key {key}. Must be string.')
18371857
self.__setitem__(key, value, sort=False)
1838-
for record in (cmaps, cycles):
1839-
record[:] = sorted(record)
1858+
try:
1859+
for record in (cmaps, cycles):
1860+
record[:] = sorted(record)
1861+
except NameError:
1862+
pass
18401863

18411864
def __delitem__(self, key):
18421865
"""Delete the item from the list records."""
18431866
super().__delitem__(self, key)
1844-
for record in (cmaps, cycles):
1845-
try:
1846-
record.remove(key)
1847-
except ValueError:
1848-
pass
1867+
try:
1868+
for record in (cmaps, cycles):
1869+
try:
1870+
record.remove(key)
1871+
except ValueError:
1872+
pass
1873+
except NameError:
1874+
pass
18491875

18501876
def __getitem__(self, key):
18511877
"""Retrieve the colormap associated with the sanitized key name. The
@@ -1904,10 +1930,13 @@ def __setitem__(self, key, item, sort=True):
19041930
'matplotlib.colors.LinearSegmentedColormap.'
19051931
)
19061932
key = self._sanitize_key(key, mirror=False)
1907-
record = cycles if isinstance(item, ListedColormap) else cmaps
1908-
record.append(key)
1909-
if sort:
1910-
record[:] = sorted(record)
1933+
try:
1934+
record = cycles if isinstance(item, ListedColormap) else cmaps
1935+
record.append(key)
1936+
if sort:
1937+
record[:] = sorted(record)
1938+
except NameError:
1939+
pass
19111940
return super().__setitem__(key, item)
19121941

19131942
def __contains__(self, item):
@@ -1950,11 +1979,14 @@ def get(self, key, *args):
19501979
def pop(self, key, *args):
19511980
"""Pop the sanitized colormap name."""
19521981
key = self._sanitize_key(key, mirror=True)
1953-
for record in (cmaps, cycles):
1954-
try:
1955-
record.remove(key)
1956-
except ValueError:
1957-
pass
1982+
try:
1983+
for record in (cmaps, cycles):
1984+
try:
1985+
record.remove(key)
1986+
except ValueError:
1987+
pass
1988+
except NameError:
1989+
pass
19581990
return super().pop(key, *args)
19591991

19601992
def update(self, *args, **kwargs):
@@ -2836,8 +2868,11 @@ def _warn_or_raise(msg):
28362868
# NOTE: This appears to be biggest import time bottleneck! Increases
28372869
# time from 0.05s to 0.2s, with numpy loadtxt or with this regex thing.
28382870
delim = re.compile(r'[,\s]+')
2839-
data = [delim.split(line.strip())
2840-
for line in open(filename).readlines() if line.strip()]
2871+
data = [
2872+
delim.split(line.strip())
2873+
for line in open(filename).readlines()
2874+
if line.strip() and line.strip()[0] != '#'
2875+
]
28412876
try:
28422877
data = [[float(num) for num in line] for line in data]
28432878
except ValueError:
@@ -3149,12 +3184,24 @@ def register_fonts():
31493184
fonts[:] = [*fonts_proplot, *fonts_system]
31503185

31513186

3152-
def _draw_bars(cmapdict, length=4.0, width=0.2):
3187+
def _draw_bars(names, *, source, unknown='User', length=4.0, width=0.2):
31533188
"""
31543189
Draw colorbars for "colormaps" and "color cycles". This is called by
31553190
`show_cycles` and `show_cmaps`.
31563191
"""
3157-
# Figure
3192+
# Categorize the input names
3193+
cmapdict = {}
3194+
names_all = list(map(str.lower, names))
3195+
names_known = list(map(str.lower, sum(map(list, source.values()), [])))
3196+
names_unknown = [name for name in names if name not in names_known]
3197+
if names_unknown:
3198+
cmapdict[unknown] = names_unknown
3199+
for cat, names in source.items():
3200+
names_cat = [name for name in names if name.lower() in names_all]
3201+
if names_cat:
3202+
cmapdict[cat] = names_cat
3203+
3204+
# Draw figure
31583205
from . import subplots
31593206
naxs = len(cmapdict) + sum(map(len, cmapdict.values()))
31603207
fig, axs = subplots(
@@ -3166,8 +3213,6 @@ def _draw_bars(cmapdict, length=4.0, width=0.2):
31663213
a = np.linspace(0, 1, 257).reshape(1, -1)
31673214
a = np.vstack((a, a))
31683215
for cat, names in cmapdict.items():
3169-
if not names:
3170-
continue
31713216
nheads += 1
31723217
for imap, name in enumerate(names):
31733218
iax += 1
@@ -3511,10 +3556,10 @@ def _color_filter(i, hcl): # noqa: E306
35113556
return figs
35123557

35133558

3514-
def show_cmaps(*args, N=None, unknown='User', **kwargs):
3559+
def show_cmaps(*args, N=None, **kwargs):
35153560
"""
3516-
Generate a table of the registered colormaps or the input colormaps.
3517-
Adapted from `this example \
3561+
Generate a table of the registered colormaps or the input colormaps
3562+
categorized by source. Adapted from `this example \
35183563
<http://matplotlib.org/examples/color/colormaps_reference.html>`__.
35193564
35203565
Parameters
@@ -3549,26 +3594,24 @@ def show_cmaps(*args, N=None, unknown='User', **kwargs):
35493594
isinstance(mcm.cmap_d[name], LinearSegmentedColormap)
35503595
]
35513596

3552-
# Get dictionary of registered colormaps and their categories
3553-
cmapdict = {}
3554-
names_all = list(map(str.lower, names))
3555-
names_known = sum(map(list, CMAPS_TABLE.values()), [])
3556-
cmapdict[unknown] = [name for name in names if name not in names_known]
3557-
for cat, names in CMAPS_TABLE.items():
3558-
cmapdict[cat] = [name for name in names if name.lower() in names_all]
3559-
35603597
# Return figure of colorbars
3561-
return _draw_bars(cmapdict, **kwargs)
3598+
kwargs.setdefault('source', CMAPS_TABLE)
3599+
return _draw_bars(names, **kwargs)
35623600

35633601

35643602
def show_cycles(*args, **kwargs):
35653603
"""
3566-
Generate a table of registered color cycles or the input color cycles.
3604+
Generate a table of registered color cycles or the input color cycles
3605+
categorized by source. Adapted from `this example \
3606+
<http://matplotlib.org/examples/color/colormaps_reference.html>`__.
35673607
35683608
Parameters
35693609
----------
35703610
*args : colormap-spec, optional
35713611
Cycle names or objects.
3612+
unknown : str, optional
3613+
Category name for cycles that are unknown to ProPlot. The
3614+
default is ``'User'``.
35723615
length : float or str, optional
35733616
The length of the colorbars. Units are interpreted by
35743617
`~proplot.utils.units`.
@@ -3591,8 +3634,8 @@ def show_cycles(*args, **kwargs):
35913634
]
35923635

35933636
# Return figure of colorbars
3594-
cmapdict = {'Color cycles': names}
3595-
return _draw_bars(cmapdict, **kwargs)
3637+
kwargs.setdefault('source', CYCLES_TABLE)
3638+
return _draw_bars(names, **kwargs)
35963639

35973640

35983641
def show_fonts(*args, size=12, text=None):
@@ -3654,9 +3697,10 @@ def show_fonts(*args, size=12, text=None):
36543697

36553698

36563699
# Apply custom changes
3657-
mcm.cmap_d['Grays'] = mcm.cmap_d.pop('Greys', None) # 'Murica (and consistency with registered colors) # noqa
3658-
mcm.cmap_d['Spectral'] = mcm.cmap_d['Spectral'].reversed(
3659-
name='Spectral') # make spectral go from 'cold' to 'hot'
3700+
if 'Greys' in mcm.cmap_d: # 'Murica (and consistency with registered colors)
3701+
mcm.cmap_d['Grays'] = mcm.cmap_d.pop('Greys')
3702+
if 'Spectral' in mcm.cmap_d: # make spectral go from 'cold' to 'hot'
3703+
mcm.cmap_d['Spectral'] = mcm.cmap_d['Spectral'].reversed(name='Spectral')
36603704
for _name in CMAPS_TABLE['Matplotlib originals']: # initialize as empty lists
36613705
if _name == 'twilight_shifted':
36623706
mcm.cmap_d.pop(_name, None)

0 commit comments

Comments
 (0)