15
15
A similar approach can be used to create a gradient background for an Axes.
16
16
In that case, it is helpful to use Axes coordinates (``extent=(0, 1, 0, 1),
17
17
transform=ax.transAxes``) to be independent of the data coordinates.
18
-
19
18
"""
19
+
20
20
import matplotlib .pyplot as plt
21
21
import numpy as np
22
22
23
23
np .random .seed (19680801 )
24
24
25
25
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 ):
27
27
"""
28
28
Draw a gradient image based on a colormap.
29
29
30
30
Parameters
31
31
----------
32
32
ax : Axes
33
33
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.
38
34
direction : float
39
35
The direction of the gradient. This is a number in
40
36
range 0 (=vertical) to 1 (=horizontal).
@@ -43,16 +39,16 @@ def gradient_image(ax, extent, direction=0.3, cmap_range=(0, 1), **kwargs):
43
39
used for the gradient, where the complete colormap is (0, 1).
44
40
**kwargs
45
41
Other parameters are passed on to `.Axes.imshow()`.
46
- In particular useful is *cmap* .
42
+ In particular, *cmap*, *extent*, and *transform* may be useful .
47
43
"""
48
44
phi = direction * np .pi / 2
49
45
v = np .array ([np .cos (phi ), np .sin (phi )])
50
46
X = np .array ([[v @ [1 , 0 ], v @ [1 , 1 ]],
51
47
[v @ [0 , 0 ], v @ [0 , 1 ]]])
52
48
a , b = cmap_range
53
49
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 )
56
52
return im
57
53
58
54
@@ -63,11 +59,8 @@ def gradient_bar(ax, x, y, width=0.5, bottom=0):
63
59
cmap = plt .cm .Blues_r , cmap_range = (0 , 0.8 ))
64
60
65
61
66
- xmin , xmax = xlim = 0 , 10
67
- ymin , ymax = ylim = 0 , 1
68
-
69
62
fig , ax = plt .subplots ()
70
- ax .set (xlim = xlim , ylim = ylim , autoscale_on = False )
63
+ ax .set (xlim = ( 0 , 10 ), ylim = ( 0 , 1 ) )
71
64
72
65
# background image
73
66
gradient_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):
77
70
x = np .arange (N ) + 0.15
78
71
y = np .random .rand (N )
79
72
gradient_bar (ax , x , y , width = 0.7 )
80
- ax .set_aspect ('auto' )
81
73
plt .show ()
0 commit comments