~ubuntu-branches/ubuntu/karmic/matplotlib/karmic

« back to all changes in this revision

Viewing changes to examples/pylab_examples/custom_cmap.py

  • Committer: Bazaar Package Importer
  • Author(s): Benjamin Drung
  • Date: 2009-09-18 10:13:37 UTC
  • mfrom: (2.2.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090918101337-c7wovuqfdi1x1afk
Tags: 0.99.0-1ubuntu1
* Merge from Debian unstable (LP: #419885), remaining changes:
  - Explicitly depend on python-tk as workaround for LP #301007; drop these
    changes if an auto backend discovery is implemented by upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
103
103
                   (1.0, 0.0, 0.0))
104
104
        }
105
105
 
 
106
# Now we will use this example to illustrate 3 ways of
 
107
# handling custom colormaps.
 
108
# First, the most direct and explicit:
106
109
 
107
110
blue_red1 = LinearSegmentedColormap('BlueRed1', cdict1)
 
111
 
 
112
# Second, create the map explicitly and register it.
 
113
# Like the first method, this method works with any kind
 
114
# of Colormap, not just
 
115
# a LinearSegmentedColormap:
 
116
 
108
117
blue_red2 = LinearSegmentedColormap('BlueRed2', cdict2)
109
 
blue_red3 = LinearSegmentedColormap('BlueRed3', cdict3)
 
118
plt.register_cmap(cmap=blue_red2)
 
119
 
 
120
# Third, for LinearSegmentedColormap only,
 
121
# leave everything to register_cmap:
 
122
 
 
123
plt.register_cmap(name='BlueRed3', data=cdict3) # optional lut kwarg
110
124
 
111
125
x = np.arange(0, np.pi, 0.1)
112
126
y = np.arange(0, 2*np.pi, 0.1)
121
135
plt.colorbar()
122
136
 
123
137
plt.subplot(1,3,2)
124
 
plt.imshow(Z, interpolation='nearest', cmap=blue_red2)
 
138
cmap = plt.get_cmap('BlueRed2')
 
139
plt.imshow(Z, interpolation='nearest', cmap=cmap)
125
140
plt.colorbar()
126
141
 
 
142
# Now we will set the third cmap as the default.  One would
 
143
# not normally do this in the middle of a script like this;
 
144
# it is done here just to illustrate the method.
 
145
 
 
146
plt.rcParams['image.cmap'] = 'BlueRed3'
 
147
 
 
148
# Also see below for an alternative, particularly for
 
149
# interactive use.
 
150
 
127
151
plt.subplot(1,3,3)
128
 
plt.imshow(Z, interpolation='nearest', cmap=blue_red3)
 
152
plt.imshow(Z, interpolation='nearest')
129
153
plt.colorbar()
130
154
 
 
155
# Or as yet another variation, we could replace the rcParams
 
156
# specification *before* the imshow with the following *after*
 
157
# imshow:
 
158
#
 
159
# plt.set_cmap('BlueRed3')
 
160
#
 
161
# This sets the new default *and* sets the colormap of the last
 
162
# image-like item plotted via pyplot, if any.
 
163
 
 
164
 
131
165
plt.suptitle('Custom Blue-Red colormaps')
132
166
 
133
167
plt.show()