Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

DOC: Add missing cmaps to perception doc (fix for #8073) #8156

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

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Overhaul and refactoring of the 'lightness.py' script
  • Loading branch information
afvincent committed Mar 25, 2017
commit 0c9a2805f880837a1d98ec2ca9f2c308ef56cf40
150 changes: 65 additions & 85 deletions 150 doc/users/plotting/colormaps/lightness.py
Original file line number Diff line number Diff line change
@@ -1,120 +1,100 @@
'''
==========================
Lightness of the colormaps
==========================

For each colormap, plot the lightness parameter L* from CIELAB colorspace
along the y axis vs index through the colormap. Colormaps are examined in
categories as in the original matplotlib gallery of colormaps.
'''

from colormaps import cmaps
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import cm
import matplotlib as mpl
from colorspacious import cspace_converter
from colormaps import cmaps # the colormaps, grouped by category

mpl.rcParams.update({'font.size': 12})

# indices to step through colormap
# Number of colormap par subplot for particular cmap categories
Copy link
Member

Choose a reason for hiding this comment

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

"par" -> "per" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep (French went through the cracks of my mind…).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed!

_DSUBS = {'Perceptually Uniform Sequential': 4, 'Sequential': 6,
Copy link
Member

Choose a reason for hiding this comment

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

It'd be nice if we could compute the values in someway. I that would require the colors module to be reorganzed in some way. And that's definitely out of the scope of this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The new order (and the addition of some previously missing colormaps) in 3c29961 allow a more uniform set of values for _DSUBS and _DC. Basically only two different values are sufficient to clean reference plots.

I have kept an explicit dictionary with all values inside, in case one would want to further tweak these values. One could also use these dictionary to just describe the non-standard cases and rely on .get(key, default) for the others.

'Sequential (2)': 5, 'Diverging': 6, 'Qualitative': 4,
'Miscellaneous': 6}

# Spacing between the colormaps of a subplot
Copy link
Member

@QuLogic QuLogic Mar 25, 2017

Choose a reason for hiding this comment

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

I think it makes more sense to change the figure size like it used to be done. Now you get a different sized Axes and different number of ticks for each figure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I do not understand to which situation you are refering to: looking at blame on GH, the dc and dsub strategy is used since at least 3 years. I tried to not modify the spirit of the plots, but rather to refactor what could be, and to make the magic values a bit more uniform across all the different cases.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, this comment probably shouldn't be here but on a subplots line. In any case, I missed the fact that you kept the changing figure size, so it should be the same as before.

But if the figure size changes based on the number of plots, then I don't understand why 00 and 05 have 3 y-ticks, but the others have 5.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did not even notice the different amount of ticks ^^... If I had to guess, I would say that it may be an artifact from plt.tight_layout() that gives more or less place to the subplots depending on length of the x-tick labels.

Copy link
Member

Choose a reason for hiding this comment

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

That might be it. It would be nice if we didn't have to use tight_layout, but I guess then we'd need to know the size of the colormap labels first.

_DC = {'Perceptually Uniform Sequential': 1.15, 'Sequential': 0.6,
'Sequential (2)': 1.15, 'Diverging': 1.3, 'Qualitative': 1.3,
'Miscellaneous': 1.4}

# Indices to step through colormap
x = np.linspace(0.0, 1.0, 100)

# Do plot
for cmap_category, cmap_list in cmaps:

# Do subplots so that colormaps have enough space. 5 per subplot?
dsub = 5 # number of colormaps per subplot
if cmap_category == 'Diverging': # because has 12 colormaps
dsub = 6
elif cmap_category == 'Sequential (2)':
dsub = 6
elif cmap_category == 'Sequential':
dsub = 7
nsubplots = int(np.ceil(len(cmap_list)/float(dsub)))

fig = plt.figure(figsize=(7,2.6*nsubplots))
# Do subplots so that colormaps have enough space.
# Default is 5 colormaps per subplot.
dsub = _DSUBS.get(cmap_category, 5)
nsubplots = int(np.ceil(len(cmap_list) / float(dsub)))

for i, subplot in enumerate(range(nsubplots)):
# squeeze=False to handle similarly the case of a single subplot
fig, axes = plt.subplots(nrows=nsubplots, squeeze=False,
figsize=(7, 2.6*nsubplots))

locs = [] # locations for text labels
for i, ax in enumerate(axes.flat):

ax = fig.add_subplot(nsubplots, 1, i+1)
locs = [] # locations for text labels

for j, cmap in enumerate(cmap_list[i*dsub:(i+1)*dsub]):

# Get rgb values for colormap
rgb = cm.get_cmap(cmap)(x)[np.newaxis,:,:3]

# Get colormap in CAM02-UCS colorspace. We want the lightness.
# Get RGB values for colormap and convert the colormap in
# CAM02-UCS colorspace. lab[0, :, 0] is the lightness.
rgb = cm.get_cmap(cmap)(x)[np.newaxis, :, :3]
lab = cspace_converter("sRGB1", "CAM02-UCS")(rgb)

# Plot colormap L values
# Do separately for each category so each plot can be pretty
# to make scatter markers change color along plot:
# Plot colormap L values. Do separately for each category
# so each plot can be pretty. To make scatter markers change
# color along plot:
# http://stackoverflow.com/questions/8202605/matplotlib-scatterplot-colour-as-a-function-of-a-third-variable
if cmap_category=='Perceptually Uniform Sequential':
dc = 1.15 # spacing between colormaps
ax.scatter(x+j*dc, lab[0,:,0], c=x, cmap=cmap,
s=300, linewidths=0.)
if i==2:
ax.axis([-0.1,4.1,0,100])
else:
ax.axis([-0.1,4.7,0,100])
locs.append(x[-1]+j*dc) # store locations for colormap labels

elif cmap_category=='Sequential':
dc = 0.6 # spacing between colormaps

if cmap_category == 'Sequential':
# These colormaps all start at high lightness but we want them
# reversed to look nice in the plot, so reverse the order.
ax.scatter(x+j*dc, lab[0,::-1,0], c=x[::-1], cmap=cmap,
s=300, linewidths=0.)
if i==2:
ax.axis([-0.1,4.1,0,100])
else:
ax.axis([-0.1,4.7,0,100])
locs.append(x[-1]+j*dc) # store locations for colormap labels

elif cmap_category=='Sequential (2)':
dc = 1.15
ax.scatter(x+j*dc, lab[0,:,0], c=x, cmap=cmap,
s=300, linewidths=0.)
ax.axis([-0.1,7.0,0,100])
# store locations for colormap labels
locs.append(x[-1]+j*dc)

elif cmap_category=='Diverging':
dc = 1.2
ax.scatter(x+j*dc, lab[0,:,0], c=x, cmap=cmap,
s=300, linewidths=0.)
ax.axis([-0.1,7.1,0,100])
# store locations for colormap labels
locs.append(x[int(x.size/2.)]+j*dc)
elif cmap_category=='Qualitative':
dc = 1.3
ax.scatter(x+j*dc, lab[0,:,0], c=x, cmap=cmap,
s=300, linewidths=0.)
ax.axis([-0.1,6.3,0,100])
# store locations for colormap labels
locs.append(x[int(x.size/2.)]+j*dc)

elif cmap_category=='Miscellaneous':
dc = 1.25
ax.scatter(x+j*dc, lab[0,:,0], c=x, cmap=cmap,
s=300, linewidths=0.)
ax.axis([-0.1,6.1,0,100])
# store locations for colormap labels
locs.append(x[int(x.size/2.)]+j*dc)

# Set up labels for colormaps
ax.xaxis.set_ticks_position('top')
ticker = mpl.ticker.FixedLocator(locs)
ax.xaxis.set_major_locator(ticker)
formatter = mpl.ticker.FixedFormatter(cmap_list[i*dsub:(i+1)*dsub])
ax.xaxis.set_major_formatter(formatter)
labels = ax.get_xticklabels()
for label in labels:
label.set_rotation(60)
y_ = lab[0, ::-1, 0]
c_ = x[::-1]
else:
y_ = lab[0, :, 0]
c_ = x

dc = _DC.get(cmap_category, 1.2) # cmaps horizontal spacing
ax.scatter(x + j*dc, y_, c=c_, cmap=cmap, s=300, linewidths=0.0)

# Store locations for colormap labels
if cmap_category in ('Perceptually Uniform Sequential',
'Sequential', 'Sequential (2)'):
locs.append(x[-1] + j*dc)
elif cmap_category in ('Diverging', 'Qualitative',
'Miscellaneous'):
locs.append(x[int(x.size/2.)] + j*dc)

# Set up the axis limits:
# * the 1st subplot is used as a reference for the x-axis limits
# * lightness values goes from 0 to 100 (y-axis limits)
ax.set_xlim(axes[0, 0].get_xlim())
ax.set_ylim(0.0, 100.0)

# Set up labels for colormaps
ax.xaxis.set_ticks_position('top')
ticker = mpl.ticker.FixedLocator(locs)
ax.xaxis.set_major_locator(ticker)
formatter = mpl.ticker.FixedFormatter(cmap_list[i*dsub:(i+1)*dsub])
ax.xaxis.set_major_formatter(formatter)
ax.xaxis.set_tick_params(rotation=50)

ax.set_xlabel(cmap_category + ' colormaps', fontsize=14)
fig.text(0.0, 0.55, 'Lightness $L^*$', fontsize=12,
transform=fig.transFigure, rotation=90)

fig.tight_layout(h_pad=0.05, pad=1.5)
fig.tight_layout(h_pad=0.0, pad=1.5)
plt.show()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.