|
30 | 30 | # over the line segments of the contour, removing the lines beneath |
31 | 31 | # the label |
32 | 32 |
|
33 | | -plt.figure() |
34 | | -CS = plt.contour(X, Y, Z) |
35 | | -plt.clabel(CS, inline=1, fontsize=10) |
36 | | -plt.title('Simplest default with labels') |
| 33 | +fig, ax = plt.subplots() |
| 34 | +CS = ax.contour(X, Y, Z) |
| 35 | +ax.clabel(CS, inline=1, fontsize=10) |
| 36 | +ax.set_title('Simplest default with labels') |
37 | 37 |
|
38 | 38 |
|
39 | 39 | ############################################################################### |
40 | 40 | # contour labels can be placed manually by providing list of positions |
41 | 41 | # (in data coordinate). See ginput_manual_clabel.py for interactive |
42 | 42 | # placement. |
43 | 43 |
|
44 | | -plt.figure() |
45 | | -CS = plt.contour(X, Y, Z) |
| 44 | +fig, ax = plt.subplots() |
| 45 | +CS = ax.contour(X, Y, Z) |
46 | 46 | manual_locations = [(-1, -1.4), (-0.62, -0.7), (-2, 0.5), (1.7, 1.2), (2.0, 1.4), (2.4, 1.7)] |
47 | | -plt.clabel(CS, inline=1, fontsize=10, manual=manual_locations) |
48 | | -plt.title('labels at selected locations') |
| 47 | +ax.clabel(CS, inline=1, fontsize=10, manual=manual_locations) |
| 48 | +ax.set_title('labels at selected locations') |
49 | 49 |
|
50 | 50 |
|
51 | 51 | ############################################################################### |
52 | 52 | # You can force all the contours to be the same color. |
53 | 53 |
|
54 | | -plt.figure() |
55 | | -CS = plt.contour(X, Y, Z, 6, |
| 54 | +fig, ax = plt.subplots() |
| 55 | +CS = ax.contour(X, Y, Z, 6, |
56 | 56 | colors='k', # negative contours will be dashed by default |
57 | 57 | ) |
58 | | -plt.clabel(CS, fontsize=9, inline=1) |
59 | | -plt.title('Single color - negative contours dashed') |
| 58 | +ax.clabel(CS, fontsize=9, inline=1) |
| 59 | +ax.set_title('Single color - negative contours dashed') |
60 | 60 |
|
61 | 61 | ############################################################################### |
62 | 62 | # You can set negative contours to be solid instead of dashed: |
63 | 63 |
|
64 | 64 | matplotlib.rcParams['contour.negative_linestyle'] = 'solid' |
65 | | -plt.figure() |
66 | | -CS = plt.contour(X, Y, Z, 6, |
| 65 | +fig, ax = plt.subplots() |
| 66 | +CS = ax.contour(X, Y, Z, 6, |
67 | 67 | colors='k', # negative contours will be dashed by default |
68 | 68 | ) |
69 | | -plt.clabel(CS, fontsize=9, inline=1) |
70 | | -plt.title('Single color - negative contours solid') |
| 69 | +ax.clabel(CS, fontsize=9, inline=1) |
| 70 | +ax.set_title('Single color - negative contours solid') |
71 | 71 |
|
72 | 72 |
|
73 | 73 | ############################################################################### |
74 | 74 | # And you can manually specify the colors of the contour |
75 | 75 |
|
76 | | -plt.figure() |
77 | | -CS = plt.contour(X, Y, Z, 6, |
| 76 | +fig, ax = plt.subplots() |
| 77 | +CS = ax.contour(X, Y, Z, 6, |
78 | 78 | linewidths=np.arange(.5, 4, .5), |
79 | 79 | colors=('r', 'green', 'blue', (1, 1, 0), '#afeeee', '0.5') |
80 | 80 | ) |
81 | | -plt.clabel(CS, fontsize=9, inline=1) |
82 | | -plt.title('Crazy lines') |
| 81 | +ax.clabel(CS, fontsize=9, inline=1) |
| 82 | +ax.set_title('Crazy lines') |
83 | 83 |
|
84 | 84 |
|
85 | 85 | ############################################################################### |
86 | 86 | # Or you can use a colormap to specify the colors; the default |
87 | 87 | # colormap will be used for the contour lines |
88 | 88 |
|
89 | | -plt.figure() |
90 | | -im = plt.imshow(Z, interpolation='bilinear', origin='lower', |
| 89 | +fig, ax = plt.subplots() |
| 90 | +im = ax.imshow(Z, interpolation='bilinear', origin='lower', |
91 | 91 | cmap=cm.gray, extent=(-3, 3, -2, 2)) |
92 | 92 | levels = np.arange(-1.2, 1.6, 0.2) |
93 | | -CS = plt.contour(Z, levels, |
94 | | - origin='lower', |
95 | | - linewidths=2, |
96 | | - extent=(-3, 3, -2, 2)) |
| 93 | +CS = ax.contour(Z, levels, origin='lower', cmap='flag', |
| 94 | + linewidths=2, extent=(-3, 3, -2, 2)) |
97 | 95 |
|
98 | 96 | # Thicken the zero contour. |
99 | 97 | zc = CS.collections[6] |
100 | 98 | plt.setp(zc, linewidth=4) |
101 | 99 |
|
102 | | -plt.clabel(CS, levels[1::2], # label every second level |
103 | | - inline=1, |
104 | | - fmt='%1.1f', |
105 | | - fontsize=14) |
| 100 | +ax.clabel(CS, levels[1::2], # label every second level |
| 101 | + inline=1, fmt='%1.1f', |
| 102 | + cmap='flag', fontsize=14) |
106 | 103 |
|
107 | 104 | # make a colorbar for the contour lines |
108 | | -CB = plt.colorbar(CS, shrink=0.8, extend='both') |
| 105 | +CB = fig.colorbar(CS, shrink=0.8, extend='both') |
109 | 106 |
|
110 | | -plt.title('Lines with colorbar') |
111 | | -#plt.hot() # Now change the colormap for the contour lines and colorbar |
112 | | -plt.flag() |
| 107 | +ax.set_title('Lines with colorbar') |
113 | 108 |
|
114 | 109 | # We can still add a colorbar for the image, too. |
115 | | -CBI = plt.colorbar(im, orientation='horizontal', shrink=0.8) |
| 110 | +CBI = fig.colorbar(im, orientation='horizontal', shrink=0.8) |
116 | 111 |
|
117 | 112 | # This makes the original colorbar look a bit out of place, |
118 | 113 | # so let's improve its position. |
119 | 114 |
|
120 | | -l, b, w, h = plt.gca().get_position().bounds |
| 115 | +l, b, w, h = ax.get_position().bounds |
121 | 116 | ll, bb, ww, hh = CB.ax.get_position().bounds |
122 | 117 | CB.ax.set_position([ll, b + 0.1*h, ww, h*0.8]) |
123 | 118 |
|
124 | | - |
125 | 119 | plt.show() |
0 commit comments