|
1 | 1 | #!/usr/bin/env python |
2 | | -''' |
| 2 | +""" |
3 | 3 | Illustrate simple contour plotting, contours on an image with |
4 | 4 | a colorbar for the contours, and labelled contours. |
5 | 5 |
|
6 | 6 | See also contour_image.py. |
7 | | -''' |
8 | | -from pylab import * |
| 7 | +""" |
| 8 | +import matplotlib |
| 9 | +import numpy as np |
| 10 | +import matplotlib.cm as cm |
| 11 | +import matplotlib.mlab as mlab |
| 12 | +import matplotlib.pyplot as plt |
9 | 13 |
|
10 | | -rcParams['xtick.direction'] = 'out' |
11 | | -rcParams['ytick.direction'] = 'out' |
| 14 | +matplotlib.rcParams['xtick.direction'] = 'out' |
| 15 | +matplotlib.rcParams['ytick.direction'] = 'out' |
12 | 16 |
|
13 | 17 | delta = 0.025 |
14 | | -x = arange(-3.0, 3.0, delta) |
15 | | -y = arange(-2.0, 2.0, delta) |
16 | | -X, Y = meshgrid(x, y) |
17 | | -Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) |
18 | | -Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) |
| 18 | +x = np.arange(-3.0, 3.0, delta) |
| 19 | +y = np.arange(-2.0, 2.0, delta) |
| 20 | +X, Y = np.meshgrid(x, y) |
| 21 | +Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) |
| 22 | +Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) |
19 | 23 | # difference of Gaussians |
20 | 24 | Z = 10.0 * (Z2 - Z1) |
21 | 25 |
|
|
25 | 29 | # inline argument to clabel will control whether the labels are draw |
26 | 30 | # over the line segments of the contour, removing the lines beneath |
27 | 31 | # the label |
28 | | -figure() |
29 | | -CS = contour(X, Y, Z) |
30 | | -clabel(CS, inline=1, fontsize=10) |
31 | | -title('Simplest default with labels') |
| 32 | +plt.figure() |
| 33 | +CS = plt.contour(X, Y, Z) |
| 34 | +plt.clabel(CS, inline=1, fontsize=10) |
| 35 | +plt.title('Simplest default with labels') |
32 | 36 |
|
33 | 37 |
|
34 | 38 | # You can force all the contours to be the same color. |
35 | | -figure() |
36 | | -CS = contour(X, Y, Z, 6, |
37 | | - colors='k', # negative contours will be dashed by default |
38 | | - ) |
39 | | -clabel(CS, fontsize=9, inline=1) |
40 | | -title('Single color - negative contours dashed') |
| 39 | +plt.figure() |
| 40 | +CS = plt.contour(X, Y, Z, 6, |
| 41 | + colors='k', # negative contours will be dashed by default |
| 42 | + ) |
| 43 | +plt.clabel(CS, fontsize=9, inline=1) |
| 44 | +plt.title('Single color - negative contours dashed') |
41 | 45 |
|
42 | 46 | # You can set negative contours to be solid instead of dashed: |
43 | | -rcParams['contour.negative_linestyle'] = 'solid' |
44 | | -figure() |
45 | | -CS = contour(X, Y, Z, 6, |
46 | | - colors='k', # negative contours will be dashed by default |
47 | | - ) |
48 | | -clabel(CS, fontsize=9, inline=1) |
49 | | -title('Single color - negative contours solid') |
| 47 | +matplotlib.rcParams['contour.negative_linestyle'] = 'solid' |
| 48 | +plt.figure() |
| 49 | +CS = plt.contour(X, Y, Z, 6, |
| 50 | + colors='k', # negative contours will be dashed by default |
| 51 | + ) |
| 52 | +plt.clabel(CS, fontsize=9, inline=1) |
| 53 | +plt.title('Single color - negative contours solid') |
50 | 54 |
|
51 | 55 |
|
52 | 56 | # And you can manually specify the colors of the contour |
53 | | -figure() |
54 | | -CS = contour(X, Y, Z, 6, |
55 | | - linewidths=arange(.5, 4, .5), |
56 | | - colors=('r', 'green', 'blue', (1,1,0), '#afeeee', '0.5') |
57 | | - ) |
58 | | -clabel(CS, fontsize=9, inline=1) |
59 | | -title('Crazy lines') |
| 57 | +plt.figure() |
| 58 | +CS = plt.contour(X, Y, Z, 6, |
| 59 | + linewidths=np.arange(.5, 4, .5), |
| 60 | + colors=('r', 'green', 'blue', (1,1,0), '#afeeee', '0.5') |
| 61 | + ) |
| 62 | +plt.clabel(CS, fontsize=9, inline=1) |
| 63 | +plt.title('Crazy lines') |
60 | 64 |
|
61 | 65 |
|
62 | 66 | # Or you can use a colormap to specify the colors; the default |
63 | 67 | # colormap will be used for the contour lines |
64 | | -figure() |
65 | | -im = imshow(Z, interpolation='bilinear', origin='lower', |
66 | | - cmap=cm.gray, extent=(-3,3,-2,2)) |
67 | | -levels = arange(-1.2, 1.6, 0.2) |
68 | | -CS = contour(Z, levels, |
69 | | - origin='lower', |
70 | | - linewidths=2, |
71 | | - extent=(-3,3,-2,2)) |
| 68 | +plt.figure() |
| 69 | +im = plt.imshow(Z, interpolation='bilinear', origin='lower', |
| 70 | + cmap=cm.gray, extent=(-3,3,-2,2)) |
| 71 | +levels = np.arange(-1.2, 1.6, 0.2) |
| 72 | +CS = plt.contour(Z, levels, |
| 73 | + origin='lower', |
| 74 | + linewidths=2, |
| 75 | + extent=(-3,3,-2,2)) |
72 | 76 |
|
73 | 77 | #Thicken the zero contour. |
74 | 78 | zc = CS.collections[6] |
75 | | -setp(zc, linewidth=4) |
| 79 | +plt.setp(zc, linewidth=4) |
76 | 80 |
|
77 | | -clabel(CS, levels[1::2], # label every second level |
78 | | - inline=1, |
79 | | - fmt='%1.1f', |
80 | | - fontsize=14) |
| 81 | +plt.clabel(CS, levels[1::2], # label every second level |
| 82 | + inline=1, |
| 83 | + fmt='%1.1f', |
| 84 | + fontsize=14) |
81 | 85 |
|
82 | 86 | # make a colorbar for the contour lines |
83 | | -CB = colorbar(CS, shrink=0.8, extend='both') |
| 87 | +CB = plt.colorbar(CS, shrink=0.8, extend='both') |
84 | 88 |
|
85 | | -title('Lines with colorbar') |
86 | | -hot() # Now change the colormap for the contour lines and colorbar |
87 | | -flag() |
| 89 | +plt.title('Lines with colorbar') |
| 90 | +#plt.hot() # Now change the colormap for the contour lines and colorbar |
| 91 | +plt.flag() |
88 | 92 |
|
89 | 93 | # We can still add a colorbar for the image, too. |
90 | | -CBI = colorbar(im, orientation='horizontal', shrink=0.8) |
| 94 | +CBI = plt.colorbar(im, orientation='horizontal', shrink=0.8) |
91 | 95 |
|
92 | 96 | # This makes the original colorbar look a bit out of place, |
93 | 97 | # so let's improve its position. |
94 | 98 |
|
95 | | -l,b,w,h = gca().get_position().bounds |
| 99 | +l,b,w,h = plt.gca().get_position().bounds |
96 | 100 | ll,bb,ww,hh = CB.ax.get_position().bounds |
97 | 101 | CB.ax.set_position([ll, b+0.1*h, ww, h*0.8]) |
98 | 102 |
|
99 | 103 |
|
100 | 104 | #savefig('contour_demo') |
101 | | -show() |
| 105 | +plt.show() |
0 commit comments