Skip to content

Commit

Permalink
Merge pull request #20546 from QuLogic/fixup-tutorials
Browse files Browse the repository at this point in the history
Improve tutorial figures in the new theme
  • Loading branch information
jklymak committed Jul 19, 2021
2 parents 2bc1243 + 0e5f9eb commit d686d73
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 236 deletions.
35 changes: 19 additions & 16 deletions examples/axes_grid1/simple_axes_divider1.py
Expand Up @@ -18,48 +18,51 @@ def label_axes(ax, text):
left=False, labelleft=False)


fig = plt.figure(figsize=(12, 6))
sfs = fig.subfigures(1, 2)
##############################################################################
# Fixed axes sizes; fixed paddings.

fig = plt.figure(figsize=(6, 6))
fig.suptitle("Fixed axes sizes, fixed paddings")

sfs[0].suptitle("Fixed axes sizes, fixed paddings")
# Sizes are in inches.
horiz = [Size.Fixed(1.), Size.Fixed(.5), Size.Fixed(1.5), Size.Fixed(.5)]
vert = [Size.Fixed(1.5), Size.Fixed(.5), Size.Fixed(1.)]

rect = (0.1, 0.1, 0.8, 0.8)
# Divide the axes rectangle into a grid with sizes specified by horiz * vert.
div = Divider(sfs[0], rect, horiz, vert, aspect=False)
div = Divider(fig, rect, horiz, vert, aspect=False)

# The rect parameter will actually be ignored and overridden by axes_locator.
ax1 = sfs[0].add_axes(rect, axes_locator=div.new_locator(nx=0, ny=0))
ax1 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=0))
label_axes(ax1, "nx=0, ny=0")
ax2 = sfs[0].add_axes(rect, axes_locator=div.new_locator(nx=0, ny=2))
ax2 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=2))
label_axes(ax2, "nx=0, ny=2")
ax3 = sfs[0].add_axes(rect, axes_locator=div.new_locator(nx=2, ny=2))
ax3 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, ny=2))
label_axes(ax3, "nx=2, ny=2")
ax4 = sfs[0].add_axes(rect, axes_locator=div.new_locator(nx=2, nx1=4, ny=0))
ax4 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, nx1=4, ny=0))
label_axes(ax4, "nx=2, nx1=4, ny=0")

##############################################################################
# Axes sizes that scale with the figure size; fixed paddings.

fig = plt.figure(figsize=(6, 6))
fig.suptitle("Scalable axes sizes, fixed paddings")

sfs[1].suptitle("Scalable axes sizes, fixed paddings")
# Fixed sizes are in inches, scaled sizes are relative.
horiz = [Size.Scaled(1.5), Size.Fixed(.5), Size.Scaled(1.), Size.Scaled(.5)]
vert = [Size.Scaled(1.), Size.Fixed(.5), Size.Scaled(1.5)]

rect = (0.1, 0.1, 0.8, 0.8)
# Divide the axes rectangle into a grid with sizes specified by horiz * vert.
div = Divider(sfs[1], rect, horiz, vert, aspect=False)
div = Divider(fig, rect, horiz, vert, aspect=False)

# The rect parameter will actually be ignored and overridden by axes_locator.
ax1 = sfs[1].add_axes(rect, axes_locator=div.new_locator(nx=0, ny=0))
ax1 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=0))
label_axes(ax1, "nx=0, ny=0")
ax2 = sfs[1].add_axes(rect, axes_locator=div.new_locator(nx=0, ny=2))
ax2 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=2))
label_axes(ax2, "nx=0, ny=2")
ax3 = sfs[1].add_axes(rect, axes_locator=div.new_locator(nx=2, ny=2))
ax3 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, ny=2))
label_axes(ax3, "nx=2, ny=2")
ax4 = sfs[1].add_axes(rect, axes_locator=div.new_locator(nx=2, nx1=4, ny=0))
ax4 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, nx1=4, ny=0))
label_axes(ax4, "nx=2, nx1=4, ny=0")


plt.show()
6 changes: 3 additions & 3 deletions examples/userdemo/connectionstyle_demo.py
Expand Up @@ -30,7 +30,7 @@ def demo_con_style(ax, connectionstyle):
transform=ax.transAxes, ha="left", va="top")


fig, axs = plt.subplots(3, 5, figsize=(8, 4.8))
fig, axs = plt.subplots(3, 5, figsize=(7, 6.3), constrained_layout=True)
demo_con_style(axs[0, 0], "angle3,angleA=90,angleB=0")
demo_con_style(axs[1, 0], "angle3,angleA=0,angleB=90")
demo_con_style(axs[0, 1], "arc3,rad=0.")
Expand All @@ -47,8 +47,8 @@ def demo_con_style(ax, connectionstyle):
demo_con_style(axs[2, 4], "bar,angle=180,fraction=-0.2")

for ax in axs.flat:
ax.set(xlim=(0, 1), ylim=(0, 1), xticks=[], yticks=[], aspect=1)
fig.tight_layout(pad=0.2)
ax.set(xlim=(0, 1), ylim=(0, 1.25), xticks=[], yticks=[], aspect=1.25)
fig.set_constrained_layout_pads(wspace=0, hspace=0, w_pad=0, h_pad=0)

plt.show()

Expand Down
23 changes: 12 additions & 11 deletions tutorials/colors/colormap-manipulation.py
Expand Up @@ -48,7 +48,7 @@
# ListedColormap
# --------------
#
# `.ListedColormap` s store their color values in a ``.colors`` attribute.
# `.ListedColormap`\s store their color values in a ``.colors`` attribute.
# The list of colors that comprise the colormap can be directly accessed using
# the ``colors`` property,
# or it can be accessed indirectly by calling ``viridis`` with an array
Expand All @@ -68,7 +68,7 @@
##############################################################################
# LinearSegmentedColormap
# -----------------------
# `.LinearSegmentedColormap` s do not have a ``.colors`` attribute.
# `.LinearSegmentedColormap`\s do not have a ``.colors`` attribute.
# However, one may still call the colormap with an integer array, or with a
# float array between 0 and 1.

Expand Down Expand Up @@ -114,7 +114,7 @@ def plot_examples(colormaps):

##############################################################################
# In fact, that list may contain any valid
# :doc:`matplotlib color specification </tutorials/colors/colors>`.
# :doc:`Matplotlib color specification </tutorials/colors/colors>`.
# Particularly useful for creating custom colormaps are Nx4 numpy arrays.
# Because with the variety of numpy operations that we can do on a such an
# array, carpentry of new colormaps from existing colormaps become quite
Expand Down Expand Up @@ -168,7 +168,7 @@ def plot_examples(colormaps):
# Creating linear segmented colormaps
# ===================================
#
# `.LinearSegmentedColormap` class specifies colormaps using anchor points
# The `.LinearSegmentedColormap` class specifies colormaps using anchor points
# between which RGB(A) values are interpolated.
#
# The format to specify these colormaps allows discontinuities at the anchor
Expand All @@ -177,7 +177,7 @@ def plot_examples(colormaps):
# ``yleft[i]`` and ``yright[i]`` are the values of the color on either
# side of the anchor point.
#
# If there are no discontinuities, then ``yleft[i]=yright[i]``:
# If there are no discontinuities, then ``yleft[i] == yright[i]``:

cdict = {'red': [[0.0, 0.0, 0.0],
[0.5, 1.0, 1.0],
Expand Down Expand Up @@ -221,9 +221,10 @@ def plot_linearmap(cdict):
#
# In the example below there is a discontinuity in red at 0.5. The
# interpolation between 0 and 0.5 goes from 0.3 to 1, and between 0.5 and 1
# it goes from 0.9 to 1. Note that red[0, 1], and red[2, 2] are both
# superfluous to the interpolation because red[0, 1] is the value to the
# left of 0, and red[2, 2] is the value to the right of 1.0.
# it goes from 0.9 to 1. Note that ``red[0, 1]``, and ``red[2, 2]`` are both
# superfluous to the interpolation because ``red[0, 1]`` (i.e., ``yleft[0]``)
# is the value to the left of 0, and ``red[2, 2]`` (i.e., ``yright[2]``) is the
# value to the right of 1, which are outside the color mapping domain.

cdict['red'] = [[0.0, 0.0, 0.3],
[0.5, 1.0, 0.9],
Expand All @@ -234,7 +235,7 @@ def plot_linearmap(cdict):
# Directly creating a segmented colormap from a list
# --------------------------------------------------
#
# The above described is a very versatile approach, but admittedly a bit
# The approach described above is very versatile, but admittedly a bit
# cumbersome to implement. For some basic cases, the use of
# `.LinearSegmentedColormap.from_list` may be easier. This creates a segmented
# colormap with equal spacings from a supplied list of colors.
Expand All @@ -243,8 +244,8 @@ def plot_linearmap(cdict):
cmap1 = LinearSegmentedColormap.from_list("mycmap", colors)

#############################################################################
# If desired, the nodes of the colormap can be given as numbers
# between 0 and 1. E.g. one could have the reddish part take more space in the
# If desired, the nodes of the colormap can be given as numbers between 0 and
# 1. For example, one could have the reddish part take more space in the
# colormap.

nodes = [0.0, 0.4, 0.8, 1.0]
Expand Down
111 changes: 58 additions & 53 deletions tutorials/colors/colormaps.py
Expand Up @@ -47,6 +47,8 @@
is from [IBM]_.
.. _color-colormaps_reference:
Classes of colormaps
====================
Expand Down Expand Up @@ -82,8 +84,39 @@
from colorspacious import cspace_converter


###############################################################################
#
# First, we'll show the range of each colormap. Note that some seem
# to change more "quickly" than others.

cmaps = {}

gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))


def plot_color_gradients(category, cmap_list):
# Create figure and adjust figure height to number of colormaps
nrows = len(cmap_list)
figh = 0.35 + 0.15 + (nrows + (nrows - 1) * 0.1) * 0.22
fig, axs = plt.subplots(nrows=nrows + 1, figsize=(6.4, figh))
fig.subplots_adjust(top=1 - 0.35 / figh, bottom=0.15 / figh,
left=0.2, right=0.99)
axs[0].set_title(f'{category} colormaps', fontsize=14)

for ax, name in zip(axs, cmap_list):
ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
ax.text(-0.01, 0.5, name, va='center', ha='right', fontsize=10,
transform=ax.transAxes)

# Turn off *all* ticks & spines, not just the ones with colormaps.
for ax in axs:
ax.set_axis_off()

# Save colormap list for later.
cmaps[category] = cmap_list


###############################################################################
# Sequential
# ----------
Expand All @@ -96,13 +129,15 @@
# amongst the colormaps: some are approximately linear in :math:`L^*` and others
# are more curved.

cmaps['Perceptually Uniform Sequential'] = [
'viridis', 'plasma', 'inferno', 'magma', 'cividis']
plot_color_gradients('Perceptually Uniform Sequential',
['viridis', 'plasma', 'inferno', 'magma', 'cividis'])

###############################################################################

cmaps['Sequential'] = [
'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn']
plot_color_gradients('Sequential',
['Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn'])

###############################################################################
# Sequential2
Expand All @@ -116,10 +151,10 @@
# banding of the data in those values in the colormap (see [mycarta-banding]_ for
# an excellent example of this).

cmaps['Sequential (2)'] = [
'binary', 'gist_yarg', 'gist_gray', 'gray', 'bone', 'pink',
'spring', 'summer', 'autumn', 'winter', 'cool', 'Wistia',
'hot', 'afmhot', 'gist_heat', 'copper']
plot_color_gradients('Sequential (2)',
['binary', 'gist_yarg', 'gist_gray', 'gray', 'bone',
'pink', 'spring', 'summer', 'autumn', 'winter', 'cool',
'Wistia', 'hot', 'afmhot', 'gist_heat', 'copper'])

###############################################################################
# Diverging
Expand All @@ -132,9 +167,9 @@
# measures, BrBG and RdBu are good options. coolwarm is a good option, but it
# doesn't span a wide range of :math:`L^*` values (see grayscale section below).

cmaps['Diverging'] = [
'PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu',
'RdYlBu', 'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic']
plot_color_gradients('Diverging',
['PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu', 'RdYlBu',
'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic'])

###############################################################################
# Cyclic
Expand All @@ -154,7 +189,7 @@
# for viewers to see perceptually. See an extension on this idea at
# [mycarta-jet]_.

cmaps['Cyclic'] = ['twilight', 'twilight_shifted', 'hsv']
plot_color_gradients('Cyclic', ['twilight', 'twilight_shifted', 'hsv'])

###############################################################################
# Qualitative
Expand All @@ -165,9 +200,10 @@
# the place throughout the colormap, and are clearly not monotonically increasing.
# These would not be good options for use as perceptual colormaps.

cmaps['Qualitative'] = ['Pastel1', 'Pastel2', 'Paired', 'Accent',
'Dark2', 'Set1', 'Set2', 'Set3',
'tab10', 'tab20', 'tab20b', 'tab20c']
plot_color_gradients('Qualitative',
['Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2',
'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b',
'tab20c'])

###############################################################################
# Miscellaneous
Expand All @@ -189,43 +225,12 @@
# poor choice for representing data for viewers to see perceptually. See an
# extension on this idea at [mycarta-jet]_ and [turbo]_.

cmaps['Miscellaneous'] = [
'flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern',
'gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg',
'gist_rainbow', 'rainbow', 'jet', 'turbo', 'nipy_spectral',
'gist_ncar']

###############################################################################
# .. _color-colormaps_reference:
#
# First, we'll show the range of each colormap. Note that some seem
# to change more "quickly" than others.

gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))


def plot_color_gradients(cmap_category, cmap_list):
# Create figure and adjust figure height to number of colormaps
nrows = len(cmap_list)
figh = 0.35 + 0.15 + (nrows + (nrows - 1) * 0.1) * 0.22
fig, axs = plt.subplots(nrows=nrows + 1, figsize=(6.4, figh))
fig.subplots_adjust(top=1 - 0.35 / figh, bottom=0.15 / figh,
left=0.2, right=0.99)
axs[0].set_title(cmap_category + ' colormaps', fontsize=14)

for ax, name in zip(axs, cmap_list):
ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
ax.text(-0.01, 0.5, name, va='center', ha='right', fontsize=10,
transform=ax.transAxes)

# Turn off *all* ticks & spines, not just the ones with colormaps.
for ax in axs:
ax.set_axis_off()


for cmap_category, cmap_list in cmaps.items():
plot_color_gradients(cmap_category, cmap_list)
plot_color_gradients('Miscellaneous',
['flag', 'prism', 'ocean', 'gist_earth', 'terrain',
'gist_stern', 'gnuplot', 'gnuplot2', 'CMRmap',
'cubehelix', 'brg', 'gist_rainbow', 'rainbow', 'jet',
'turbo', 'nipy_spectral', 'gist_ncar'])

plt.show()

Expand Down
31 changes: 19 additions & 12 deletions tutorials/colors/colors.py
Expand Up @@ -160,11 +160,11 @@ def demo(sty):
# The visual below shows name collisions. Color names where color values agree
# are in bold.

import matplotlib._color_data as mcd
import matplotlib.colors as mcolors
import matplotlib.patches as mpatch

overlap = {name for name in mcd.CSS4_COLORS
if "xkcd:" + name in mcd.XKCD_COLORS}
overlap = {name for name in mcolors.CSS4_COLORS
if f'xkcd:{name}' in mcolors.XKCD_COLORS}

fig = plt.figure(figsize=[9, 5])
ax = fig.add_axes([0, 0, 1, 1])
Expand All @@ -173,23 +173,30 @@ def demo(sty):
n_rows = len(overlap) // n_groups + 1

for j, color_name in enumerate(sorted(overlap)):
css4 = mcd.CSS4_COLORS[color_name]
xkcd = mcd.XKCD_COLORS["xkcd:" + color_name].upper()
css4 = mcolors.CSS4_COLORS[color_name]
xkcd = mcolors.XKCD_COLORS[f'xkcd:{color_name}'].upper()

# Pick text colour based on perceived luminance.
rgba = mcolors.to_rgba_array([css4, xkcd])
luma = 0.299 * rgba[:, 0] + 0.587 * rgba[:, 1] + 0.114 * rgba[:, 2]
css4_text_color = 'k' if luma[0] > 0.5 else 'w'
xkcd_text_color = 'k' if luma[1] > 0.5 else 'w'

col_shift = (j // n_rows) * 3
y_pos = j % n_rows
text_args = dict(va='center', fontsize=10,
weight='bold' if css4 == xkcd else None)
text_args = dict(fontsize=10, weight='bold' if css4 == xkcd else None)
ax.add_patch(mpatch.Rectangle((0 + col_shift, y_pos), 1, 1, color=css4))
ax.add_patch(mpatch.Rectangle((1 + col_shift, y_pos), 1, 1, color=xkcd))
ax.text(0 + col_shift, y_pos + .5, ' ' + css4, alpha=0.5, **text_args)
ax.text(1 + col_shift, y_pos + .5, ' ' + xkcd, alpha=0.5, **text_args)
ax.text(2 + col_shift, y_pos + .5, ' ' + color_name, **text_args)
ax.text(0.5 + col_shift, y_pos + .7, css4,
color=css4_text_color, ha='center', **text_args)
ax.text(1.5 + col_shift, y_pos + .7, xkcd,
color=xkcd_text_color, ha='center', **text_args)
ax.text(2 + col_shift, y_pos + .7, f' {color_name}', **text_args)

for g in range(n_groups):
ax.hlines(range(n_rows), 3*g, 3*g + 2.8, color='0.7', linewidth=1)
ax.text(0.5 + 3*g, -0.5, 'X11', ha='center', va='center')
ax.text(1.5 + 3*g, -0.5, 'xkcd', ha='center', va='center')
ax.text(0.5 + 3*g, -0.3, 'X11/CSS4', ha='center')
ax.text(1.5 + 3*g, -0.3, 'xkcd', ha='center')

ax.set_xlim(0, 3 * n_groups)
ax.set_ylim(n_rows, -1)
Expand Down

0 comments on commit d686d73

Please sign in to comment.