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

Skip to content

Commit d686d73

Browse files
authored
Merge pull request #20546 from QuLogic/fixup-tutorials
Improve tutorial figures in the new theme
2 parents 2bc1243 + 0e5f9eb commit d686d73

File tree

12 files changed

+140
-236
lines changed

12 files changed

+140
-236
lines changed

examples/axes_grid1/simple_axes_divider1.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,51 @@ def label_axes(ax, text):
1818
left=False, labelleft=False)
1919

2020

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

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

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

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

3435
# The rect parameter will actually be ignored and overridden by axes_locator.
35-
ax1 = sfs[0].add_axes(rect, axes_locator=div.new_locator(nx=0, ny=0))
36+
ax1 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=0))
3637
label_axes(ax1, "nx=0, ny=0")
37-
ax2 = sfs[0].add_axes(rect, axes_locator=div.new_locator(nx=0, ny=2))
38+
ax2 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=2))
3839
label_axes(ax2, "nx=0, ny=2")
39-
ax3 = sfs[0].add_axes(rect, axes_locator=div.new_locator(nx=2, ny=2))
40+
ax3 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, ny=2))
4041
label_axes(ax3, "nx=2, ny=2")
41-
ax4 = sfs[0].add_axes(rect, axes_locator=div.new_locator(nx=2, nx1=4, ny=0))
42+
ax4 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, nx1=4, ny=0))
4243
label_axes(ax4, "nx=2, nx1=4, ny=0")
4344

45+
##############################################################################
46+
# Axes sizes that scale with the figure size; fixed paddings.
47+
48+
fig = plt.figure(figsize=(6, 6))
49+
fig.suptitle("Scalable axes sizes, fixed paddings")
4450

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

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

5458
# The rect parameter will actually be ignored and overridden by axes_locator.
55-
ax1 = sfs[1].add_axes(rect, axes_locator=div.new_locator(nx=0, ny=0))
59+
ax1 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=0))
5660
label_axes(ax1, "nx=0, ny=0")
57-
ax2 = sfs[1].add_axes(rect, axes_locator=div.new_locator(nx=0, ny=2))
61+
ax2 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=2))
5862
label_axes(ax2, "nx=0, ny=2")
59-
ax3 = sfs[1].add_axes(rect, axes_locator=div.new_locator(nx=2, ny=2))
63+
ax3 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, ny=2))
6064
label_axes(ax3, "nx=2, ny=2")
61-
ax4 = sfs[1].add_axes(rect, axes_locator=div.new_locator(nx=2, nx1=4, ny=0))
65+
ax4 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, nx1=4, ny=0))
6266
label_axes(ax4, "nx=2, nx1=4, ny=0")
6367

64-
6568
plt.show()

examples/userdemo/connectionstyle_demo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def demo_con_style(ax, connectionstyle):
3030
transform=ax.transAxes, ha="left", va="top")
3131

3232

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

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

5353
plt.show()
5454

tutorials/colors/colormap-manipulation.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
# ListedColormap
4949
# --------------
5050
#
51-
# `.ListedColormap` s store their color values in a ``.colors`` attribute.
51+
# `.ListedColormap`\s store their color values in a ``.colors`` attribute.
5252
# The list of colors that comprise the colormap can be directly accessed using
5353
# the ``colors`` property,
5454
# or it can be accessed indirectly by calling ``viridis`` with an array
@@ -68,7 +68,7 @@
6868
##############################################################################
6969
# LinearSegmentedColormap
7070
# -----------------------
71-
# `.LinearSegmentedColormap` s do not have a ``.colors`` attribute.
71+
# `.LinearSegmentedColormap`\s do not have a ``.colors`` attribute.
7272
# However, one may still call the colormap with an integer array, or with a
7373
# float array between 0 and 1.
7474

@@ -114,7 +114,7 @@ def plot_examples(colormaps):
114114

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

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

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

245246
#############################################################################
246-
# If desired, the nodes of the colormap can be given as numbers
247-
# between 0 and 1. E.g. one could have the reddish part take more space in the
247+
# If desired, the nodes of the colormap can be given as numbers between 0 and
248+
# 1. For example, one could have the reddish part take more space in the
248249
# colormap.
249250

250251
nodes = [0.0, 0.4, 0.8, 1.0]

tutorials/colors/colormaps.py

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
is from [IBM]_.
4848
4949
50+
.. _color-colormaps_reference:
51+
5052
Classes of colormaps
5153
====================
5254
@@ -82,8 +84,39 @@
8284
from colorspacious import cspace_converter
8385

8486

87+
###############################################################################
88+
#
89+
# First, we'll show the range of each colormap. Note that some seem
90+
# to change more "quickly" than others.
91+
8592
cmaps = {}
8693

94+
gradient = np.linspace(0, 1, 256)
95+
gradient = np.vstack((gradient, gradient))
96+
97+
98+
def plot_color_gradients(category, cmap_list):
99+
# Create figure and adjust figure height to number of colormaps
100+
nrows = len(cmap_list)
101+
figh = 0.35 + 0.15 + (nrows + (nrows - 1) * 0.1) * 0.22
102+
fig, axs = plt.subplots(nrows=nrows + 1, figsize=(6.4, figh))
103+
fig.subplots_adjust(top=1 - 0.35 / figh, bottom=0.15 / figh,
104+
left=0.2, right=0.99)
105+
axs[0].set_title(f'{category} colormaps', fontsize=14)
106+
107+
for ax, name in zip(axs, cmap_list):
108+
ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
109+
ax.text(-0.01, 0.5, name, va='center', ha='right', fontsize=10,
110+
transform=ax.transAxes)
111+
112+
# Turn off *all* ticks & spines, not just the ones with colormaps.
113+
for ax in axs:
114+
ax.set_axis_off()
115+
116+
# Save colormap list for later.
117+
cmaps[category] = cmap_list
118+
119+
87120
###############################################################################
88121
# Sequential
89122
# ----------
@@ -96,13 +129,15 @@
96129
# amongst the colormaps: some are approximately linear in :math:`L^*` and others
97130
# are more curved.
98131

99-
cmaps['Perceptually Uniform Sequential'] = [
100-
'viridis', 'plasma', 'inferno', 'magma', 'cividis']
132+
plot_color_gradients('Perceptually Uniform Sequential',
133+
['viridis', 'plasma', 'inferno', 'magma', 'cividis'])
134+
135+
###############################################################################
101136

102-
cmaps['Sequential'] = [
103-
'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
104-
'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
105-
'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn']
137+
plot_color_gradients('Sequential',
138+
['Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
139+
'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
140+
'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn'])
106141

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

119-
cmaps['Sequential (2)'] = [
120-
'binary', 'gist_yarg', 'gist_gray', 'gray', 'bone', 'pink',
121-
'spring', 'summer', 'autumn', 'winter', 'cool', 'Wistia',
122-
'hot', 'afmhot', 'gist_heat', 'copper']
154+
plot_color_gradients('Sequential (2)',
155+
['binary', 'gist_yarg', 'gist_gray', 'gray', 'bone',
156+
'pink', 'spring', 'summer', 'autumn', 'winter', 'cool',
157+
'Wistia', 'hot', 'afmhot', 'gist_heat', 'copper'])
123158

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

135-
cmaps['Diverging'] = [
136-
'PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu',
137-
'RdYlBu', 'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic']
170+
plot_color_gradients('Diverging',
171+
['PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu', 'RdYlBu',
172+
'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic'])
138173

139174
###############################################################################
140175
# Cyclic
@@ -154,7 +189,7 @@
154189
# for viewers to see perceptually. See an extension on this idea at
155190
# [mycarta-jet]_.
156191

157-
cmaps['Cyclic'] = ['twilight', 'twilight_shifted', 'hsv']
192+
plot_color_gradients('Cyclic', ['twilight', 'twilight_shifted', 'hsv'])
158193

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

168-
cmaps['Qualitative'] = ['Pastel1', 'Pastel2', 'Paired', 'Accent',
169-
'Dark2', 'Set1', 'Set2', 'Set3',
170-
'tab10', 'tab20', 'tab20b', 'tab20c']
203+
plot_color_gradients('Qualitative',
204+
['Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2',
205+
'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b',
206+
'tab20c'])
171207

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

192-
cmaps['Miscellaneous'] = [
193-
'flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern',
194-
'gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg',
195-
'gist_rainbow', 'rainbow', 'jet', 'turbo', 'nipy_spectral',
196-
'gist_ncar']
197228

198-
###############################################################################
199-
# .. _color-colormaps_reference:
200-
#
201-
# First, we'll show the range of each colormap. Note that some seem
202-
# to change more "quickly" than others.
203-
204-
gradient = np.linspace(0, 1, 256)
205-
gradient = np.vstack((gradient, gradient))
206-
207-
208-
def plot_color_gradients(cmap_category, cmap_list):
209-
# Create figure and adjust figure height to number of colormaps
210-
nrows = len(cmap_list)
211-
figh = 0.35 + 0.15 + (nrows + (nrows - 1) * 0.1) * 0.22
212-
fig, axs = plt.subplots(nrows=nrows + 1, figsize=(6.4, figh))
213-
fig.subplots_adjust(top=1 - 0.35 / figh, bottom=0.15 / figh,
214-
left=0.2, right=0.99)
215-
axs[0].set_title(cmap_category + ' colormaps', fontsize=14)
216-
217-
for ax, name in zip(axs, cmap_list):
218-
ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
219-
ax.text(-0.01, 0.5, name, va='center', ha='right', fontsize=10,
220-
transform=ax.transAxes)
221-
222-
# Turn off *all* ticks & spines, not just the ones with colormaps.
223-
for ax in axs:
224-
ax.set_axis_off()
225-
226-
227-
for cmap_category, cmap_list in cmaps.items():
228-
plot_color_gradients(cmap_category, cmap_list)
229+
plot_color_gradients('Miscellaneous',
230+
['flag', 'prism', 'ocean', 'gist_earth', 'terrain',
231+
'gist_stern', 'gnuplot', 'gnuplot2', 'CMRmap',
232+
'cubehelix', 'brg', 'gist_rainbow', 'rainbow', 'jet',
233+
'turbo', 'nipy_spectral', 'gist_ncar'])
229234

230235
plt.show()
231236

tutorials/colors/colors.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ def demo(sty):
160160
# The visual below shows name collisions. Color names where color values agree
161161
# are in bold.
162162

163-
import matplotlib._color_data as mcd
163+
import matplotlib.colors as mcolors
164164
import matplotlib.patches as mpatch
165165

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

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

175175
for j, color_name in enumerate(sorted(overlap)):
176-
css4 = mcd.CSS4_COLORS[color_name]
177-
xkcd = mcd.XKCD_COLORS["xkcd:" + color_name].upper()
176+
css4 = mcolors.CSS4_COLORS[color_name]
177+
xkcd = mcolors.XKCD_COLORS[f'xkcd:{color_name}'].upper()
178+
179+
# Pick text colour based on perceived luminance.
180+
rgba = mcolors.to_rgba_array([css4, xkcd])
181+
luma = 0.299 * rgba[:, 0] + 0.587 * rgba[:, 1] + 0.114 * rgba[:, 2]
182+
css4_text_color = 'k' if luma[0] > 0.5 else 'w'
183+
xkcd_text_color = 'k' if luma[1] > 0.5 else 'w'
178184

179185
col_shift = (j // n_rows) * 3
180186
y_pos = j % n_rows
181-
text_args = dict(va='center', fontsize=10,
182-
weight='bold' if css4 == xkcd else None)
187+
text_args = dict(fontsize=10, weight='bold' if css4 == xkcd else None)
183188
ax.add_patch(mpatch.Rectangle((0 + col_shift, y_pos), 1, 1, color=css4))
184189
ax.add_patch(mpatch.Rectangle((1 + col_shift, y_pos), 1, 1, color=xkcd))
185-
ax.text(0 + col_shift, y_pos + .5, ' ' + css4, alpha=0.5, **text_args)
186-
ax.text(1 + col_shift, y_pos + .5, ' ' + xkcd, alpha=0.5, **text_args)
187-
ax.text(2 + col_shift, y_pos + .5, ' ' + color_name, **text_args)
190+
ax.text(0.5 + col_shift, y_pos + .7, css4,
191+
color=css4_text_color, ha='center', **text_args)
192+
ax.text(1.5 + col_shift, y_pos + .7, xkcd,
193+
color=xkcd_text_color, ha='center', **text_args)
194+
ax.text(2 + col_shift, y_pos + .7, f' {color_name}', **text_args)
188195

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

194201
ax.set_xlim(0, 3 * n_groups)
195202
ax.set_ylim(n_rows, -1)

0 commit comments

Comments
 (0)