From: <ef...@us...> - 2008-11-18 23:38:59
|
Revision: 6414 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=6414&view=rev Author: efiring Date: 2008-11-18 23:38:53 +0000 (Tue, 18 Nov 2008) Log Message: ----------- New custom colormap example; and fix typo in Axes.autoscale_view Modified Paths: -------------- trunk/matplotlib/examples/tests/backend_driver.py trunk/matplotlib/lib/matplotlib/axes.py Added Paths: ----------- trunk/matplotlib/examples/pylab_examples/custom_cmap.py Added: trunk/matplotlib/examples/pylab_examples/custom_cmap.py =================================================================== --- trunk/matplotlib/examples/pylab_examples/custom_cmap.py (rev 0) +++ trunk/matplotlib/examples/pylab_examples/custom_cmap.py 2008-11-18 23:38:53 UTC (rev 6414) @@ -0,0 +1,134 @@ +#!/usr/bin/env python + +import numpy as np +import matplotlib.pyplot as plt +from matplotlib.colors import LinearSegmentedColormap + +""" + +Example: suppose you want red to increase from 0 to 1 over the bottom +half, green to do the same over the middle half, and blue over the top +half. Then you would use: + +cdict = {'red': ((0.0, 0.0, 0.0), + (0.5, 1.0, 1.0), + (1.0, 1.0, 1.0)), + + 'green': ((0.0, 0.0, 0.0), + (0.25, 0.0, 0.0), + (0.75, 1.0, 1.0), + (1.0, 1.0, 1.0)), + + 'blue': ((0.0, 0.0, 0.0), + (0.5, 0.0, 0.0), + (1.0, 1.0, 1.0))} + +If, as in this example, there are no discontinuities in the r, g, and b +components, then it is quite simple: the second and third element of +each tuple, above, is the same--call it "y". The first element ("x") +defines interpolation intervals over the full range of 0 to 1, and it +must span that whole range. In other words, the values of x divide the +0-to-1 range into a set of segments, and y gives the end-point color +values for each segment. + +Now consider the green. cdict['green'] is saying that for +0 <= x <= 0.25, y is zero; no green. +0.25 < x <= 0.75, y varies linearly from 0 to 1. +x > 0.75, y remains at 1, full green. + +If there are discontinuities, then it is a little more complicated. +Label the 3 elements in each row in the cdict entry for a given color as +(x, y0, y1). Then for values of x between x[i] and x[i+1] the color +value is interpolated between y1[i] and y0[i+1]. + +Going back to the cookbook example, look at cdict['red']; because y0 != +y1, it is saying that for x from 0 to 0.5, red increases from 0 to 1, +but then it jumps down, so that for x from 0.5 to 1, red increases from +0.7 to 1. Green ramps from 0 to 1 as x goes from 0 to 0.5, then jumps +back to 0, and ramps back to 1 as x goes from 0.5 to 1. + +row i: x y0 y1 + / + / +row i+1: x y0 y1 + +Above is an attempt to show that for x in the range x[i] to x[i+1], the +interpolation is between y1[i] and y0[i+1]. So, y0[0] and y1[-1] are +never used. + +""" + + + +cdict1 = {'red': ((0.0, 0.0, 0.0), + (0.5, 0.0, 0.1), + (1.0, 1.0, 1.0)), + + 'green': ((0.0, 0.0, 0.0), + (1.0, 0.0, 0.0)), + + 'blue': ((0.0, 0.0, 1.0), + (0.5, 0.1, 0.0), + (1.0, 0.0, 0.0)) + } + +cdict2 = {'red': ((0.0, 0.0, 0.0), + (0.5, 0.0, 1.0), + (1.0, 0.1, 1.0)), + + 'green': ((0.0, 0.0, 0.0), + (1.0, 0.0, 0.0)), + + 'blue': ((0.0, 0.0, 0.1), + (0.5, 1.0, 0.0), + (1.0, 0.0, 0.0)) + } + +cdict3 = {'red': ((0.0, 0.0, 0.0), + (0.25,0.0, 0.0), + (0.5, 0.8, 1.0), + (0.75,1.0, 1.0), + (1.0, 0.4, 1.0)), + + 'green': ((0.0, 0.0, 0.0), + (0.25,0.0, 0.0), + (0.5, 0.9, 0.9), + (0.75,0.0, 0.0), + (1.0, 0.0, 0.0)), + + 'blue': ((0.0, 0.0, 0.4), + (0.25,1.0, 1.0), + (0.5, 1.0, 0.8), + (0.75,0.0, 0.0), + (1.0, 0.0, 0.0)) + } + + +blue_red1 = LinearSegmentedColormap('BlueRed1', cdict1) +blue_red2 = LinearSegmentedColormap('BlueRed2', cdict2) +blue_red3 = LinearSegmentedColormap('BlueRed3', cdict3) + +x = np.arange(0, np.pi, 0.1) +y = np.arange(0, 2*np.pi, 0.1) +X, Y = np.meshgrid(x,y) +Z = np.cos(X) * np.sin(Y) + +plt.figure(figsize=(10,4)) +plt.subplots_adjust(wspace=0.3) + +plt.subplot(1,3,1) +plt.imshow(Z, interpolation='nearest', cmap=blue_red1) +plt.colorbar() + +plt.subplot(1,3,2) +plt.imshow(Z, interpolation='nearest', cmap=blue_red2) +plt.colorbar() + +plt.subplot(1,3,3) +plt.imshow(Z, interpolation='nearest', cmap=blue_red3) +plt.colorbar() + +plt.suptitle('Custom Blue-Red colormaps') + +plt.show() + Modified: trunk/matplotlib/examples/tests/backend_driver.py =================================================================== --- trunk/matplotlib/examples/tests/backend_driver.py 2008-11-18 21:37:25 UTC (rev 6413) +++ trunk/matplotlib/examples/tests/backend_driver.py 2008-11-18 23:38:53 UTC (rev 6414) @@ -43,6 +43,7 @@ 'contour_demo.py', 'contour_label_demo.py', 'contourf_demo.py', + 'custom_cmap.py', 'geo_demo.py', 'griddata_demo.py', 'csd_demo.py', Modified: trunk/matplotlib/lib/matplotlib/axes.py =================================================================== --- trunk/matplotlib/lib/matplotlib/axes.py 2008-11-18 21:37:25 UTC (rev 6413) +++ trunk/matplotlib/lib/matplotlib/axes.py 2008-11-18 23:38:53 UTC (rev 6414) @@ -1496,7 +1496,7 @@ if scalex: self.set_xbound(x0, x1) if scaley: - self.set_ybound(y0, 11) + self.set_ybound(y0, y1) return if scalex: This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |