1515A similar approach can be used to create a gradient background for an Axes.
1616In that case, it is helpful to use Axes coordinates (``extent=(0, 1, 0, 1),
1717transform=ax.transAxes``) to be independent of the data coordinates.
18-
1918"""
19+
2020import matplotlib .pyplot as plt
2121import numpy as np
2222
2323np .random .seed (19680801 )
2424
2525
26- def gradient_image (ax , extent , direction = 0.3 , cmap_range = (0 , 1 ), ** kwargs ):
26+ def gradient_image (ax , direction = 0.3 , cmap_range = (0 , 1 ), ** kwargs ):
2727 """
2828 Draw a gradient image based on a colormap.
2929
3030 Parameters
3131 ----------
3232 ax : Axes
3333 The axes to draw on.
34- extent
35- The extent of the image as (xmin, xmax, ymin, ymax).
36- By default, this is in Axes coordinates but may be
37- changed using the *transform* keyword argument.
3834 direction : float
3935 The direction of the gradient. This is a number in
4036 range 0 (=vertical) to 1 (=horizontal).
@@ -43,16 +39,16 @@ def gradient_image(ax, extent, direction=0.3, cmap_range=(0, 1), **kwargs):
4339 used for the gradient, where the complete colormap is (0, 1).
4440 **kwargs
4541 Other parameters are passed on to `.Axes.imshow()`.
46- In particular useful is *cmap* .
42+ In particular, *cmap*, *extent*, and *transform* may be useful .
4743 """
4844 phi = direction * np .pi / 2
4945 v = np .array ([np .cos (phi ), np .sin (phi )])
5046 X = np .array ([[v @ [1 , 0 ], v @ [1 , 1 ]],
5147 [v @ [0 , 0 ], v @ [0 , 1 ]]])
5248 a , b = cmap_range
5349 X = a + (b - a ) / X .max () * X
54- im = ax .imshow (X , extent = extent , interpolation = 'bicubic' ,
55- vmin = 0 , vmax = 1 , ** kwargs )
50+ im = ax .imshow (X , interpolation = 'bicubic' , clim = ( 0 , 1 ) ,
51+ aspect = 'auto' , ** kwargs )
5652 return im
5753
5854
@@ -63,11 +59,8 @@ def gradient_bar(ax, x, y, width=0.5, bottom=0):
6359 cmap = plt .cm .Blues_r , cmap_range = (0 , 0.8 ))
6460
6561
66- xmin , xmax = xlim = 0 , 10
67- ymin , ymax = ylim = 0 , 1
68-
6962fig , ax = plt .subplots ()
70- ax .set (xlim = xlim , ylim = ylim , autoscale_on = False )
63+ ax .set (xlim = ( 0 , 10 ), ylim = ( 0 , 1 ) )
7164
7265# background image
7366gradient_image (ax , direction = 1 , extent = (0 , 1 , 0 , 1 ), transform = ax .transAxes ,
@@ -77,5 +70,4 @@ def gradient_bar(ax, x, y, width=0.5, bottom=0):
7770x = np .arange (N ) + 0.15
7871y = np .random .rand (N )
7972gradient_bar (ax , x , y , width = 0.7 )
80- ax .set_aspect ('auto' )
8173plt .show ()
0 commit comments