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

Commit bca3321

Browse filesBrowse files
committed
contourf_demo: add a figure to illustrate extend options with cmap
Also removed "from pylab import *".
1 parent 44d7556 commit bca3321
Copy full SHA for bca3321

File tree

Expand file treeCollapse file tree

1 file changed

+45
-25
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+45
-25
lines changed
+45-25Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,70 @@
11
#!/usr/bin/env python
2-
from pylab import *
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
35
origin = 'lower'
46
#origin = 'upper'
57

68
delta = 0.025
79

8-
x = y = arange(-3.0, 3.01, delta)
9-
X, Y = meshgrid(x, y)
10-
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
11-
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
10+
x = y = np.arange(-3.0, 3.01, delta)
11+
X, Y = np.meshgrid(x, y)
12+
Z1 = plt.mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
13+
Z2 = plt.mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
1214
Z = 10 * (Z1 - Z2)
1315

1416
nr, nc = Z.shape
1517

1618
# put NaNs in one corner:
17-
Z[-nr//6:, -nc//6:] = nan
19+
Z[-nr//6:, -nc//6:] = np.nan
1820
# contourf will convert these to masked
1921

2022

21-
Z = ma.array(Z)
23+
Z = np.ma.array(Z)
2224
# mask another corner:
23-
Z[:nr//6, :nc//6] = ma.masked
25+
Z[:nr//6, :nc//6] = np.ma.masked
2426

2527
# mask a circle in the middle:
26-
interior = sqrt((X**2) + (Y**2)) < 0.5
27-
Z[interior] = ma.masked
28+
interior = np.sqrt((X**2) + (Y**2)) < 0.5
29+
Z[interior] = np.ma.masked
2830

2931

3032
# We are using automatic selection of contour levels;
3133
# this is usually not such a good idea, because they don't
3234
# occur on nice boundaries, but we do it here for purposes
3335
# of illustration.
34-
CS = contourf(X, Y, Z, 10, # [-1, -0.1, 0, 0.1],
36+
CS = plt.contourf(X, Y, Z, 10, # [-1, -0.1, 0, 0.1],
3537
#alpha=0.5,
36-
cmap=cm.bone,
38+
cmap=plt.cm.bone,
3739
origin=origin)
3840

3941
# Note that in the following, we explicitly pass in a subset of
4042
# the contour levels used for the filled contours. Alternatively,
4143
# We could pass in additional levels to provide extra resolution,
4244
# or leave out the levels kwarg to use all of the original levels.
4345

44-
CS2 = contour(CS, levels=CS.levels[::2],
46+
CS2 = plt.contour(CS, levels=CS.levels[::2],
4547
colors = 'r',
4648
origin=origin,
4749
hold='on')
4850

49-
title('Nonsense (3 masked regions)')
50-
xlabel('word length anomaly')
51-
ylabel('sentence length anomaly')
51+
plt.title('Nonsense (3 masked regions)')
52+
plt.xlabel('word length anomaly')
53+
plt.ylabel('sentence length anomaly')
5254

5355
# Make a colorbar for the ContourSet returned by the contourf call.
54-
cbar = colorbar(CS)
56+
cbar = plt.colorbar(CS)
5557
cbar.ax.set_ylabel('verbosity coefficient')
5658
# Add the contour line levels to the colorbar
5759
cbar.add_lines(CS2)
5860

59-
figure()
61+
plt.figure()
6062

6163
# Now make a contour plot with the levels specified,
6264
# and with the colormap generated automatically from a list
6365
# of colors.
6466
levels = [-1.5, -1, -0.5, 0, 0.5, 1]
65-
CS3 = contourf(X, Y, Z, levels,
67+
CS3 = plt.contourf(X, Y, Z, levels,
6668
colors = ('r', 'g', 'b'),
6769
origin=origin,
6870
extend='both')
@@ -72,16 +74,34 @@
7274
CS3.cmap.set_under('yellow')
7375
CS3.cmap.set_over('cyan')
7476

75-
CS4 = contour(X, Y, Z, levels,
77+
CS4 = plt.contour(X, Y, Z, levels,
7678
colors = ('k',),
7779
linewidths = (3,),
7880
origin = origin)
79-
title('Listed colors (3 masked regions)')
80-
clabel(CS4, fmt = '%2.1f', colors = 'w', fontsize=14)
81+
plt.title('Listed colors (3 masked regions)')
82+
plt.clabel(CS4, fmt = '%2.1f', colors = 'w', fontsize=14)
8183

8284
# Notice that the colorbar command gets all the information it
8385
# needs from the ContourSet object, CS3.
84-
colorbar(CS3)
85-
86-
show()
86+
plt.colorbar(CS3)
87+
88+
# Illustrate all 4 possible "extend" settings:
89+
extends = ["neither", "both", "min", "max"]
90+
cmap = plt.cm.get_cmap("winter")
91+
cmap.set_under("magenta")
92+
cmap.set_over("yellow")
93+
# Note: contouring simply excludes masked or nan regions, so
94+
# instead of using the "bad" colormap value for them, it draws
95+
# nothing at all in them. Therefore the following would have
96+
# no effect:
97+
#cmap.set_bad("red")
98+
99+
fig, axs = plt.subplots(2,2)
100+
for ax, extend in zip(axs.ravel(), extends):
101+
cs = ax.contourf(X, Y, Z, levels, cmap=cmap, extend=extend, origin=origin)
102+
fig.colorbar(cs, ax=ax, shrink=0.9)
103+
ax.set_title("extend = %s" % extend)
104+
ax.locator_params(nbins=4)
105+
106+
plt.show()
87107

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.