6
6
Blend transparency with color to highlight parts of data with imshow.
7
7
8
8
A common use for :func:`matplotlib.pyplot.imshow` is to plot a 2-D statistical
9
- map. While ``imshow`` makes it easy to visualize a 2-D matrix as an image,
10
- it doesn't easily let you add transparency to the output. For example, one can
11
- plot a statistic (such as a t-statistic) and color the transparency of
12
- each pixel according to its p-value. This example demonstrates how you can
13
- achieve this effect using :class:`matplotlib.colors.Normalize`. Note that it is
14
- not possible to directly pass alpha values to :func:`matplotlib.pyplot.imshow`.
9
+ map. The function makes it easy to visualize a 2-D matrix as an image and add
10
+ transparency to the output. For example, one can plot a statistic (such as a
11
+ t-statistic) and color the transparency of each pixel according to its p-value.
12
+ This example demonstrates how you can achieve this effect.
15
13
16
14
First we will generate some data, in this case, we'll create two 2-D "blobs"
17
15
in a 2-D grid. One blob will be positive, and the other negative.
@@ -50,42 +48,38 @@ def normal_pdf(x, mean, var):
50
48
# We'll also create a grey background into which the pixels will fade
51
49
greys = np .full ((* weights .shape , 3 ), 70 , dtype = np .uint8 )
52
50
53
- # First we'll plot these blobs using only ``imshow``.
51
+ # First we'll plot these blobs using ``imshow`` without transparency .
54
52
vmax = np .abs (weights ).max ()
55
- vmin = - vmax
56
- cmap = plt .cm .RdYlBu
53
+ imshow_kwargs = {
54
+ 'vmax' : vmax ,
55
+ 'vmin' : - vmax ,
56
+ 'cmap' : 'RdYlBu' ,
57
+ 'extent' : (xmin , xmax , ymin , ymax ),
58
+ }
57
59
58
60
fig , ax = plt .subplots ()
59
61
ax .imshow (greys )
60
- ax .imshow (weights , extent = ( xmin , xmax , ymin , ymax ), cmap = cmap )
62
+ ax .imshow (weights , ** imshow_kwargs )
61
63
ax .set_axis_off ()
62
64
63
65
###############################################################################
64
66
# Blending in transparency
65
67
# ========================
66
68
#
67
69
# The simplest way to include transparency when plotting data with
68
- # :func:`matplotlib.pyplot.imshow` is to convert the 2-D data array to a
69
- # 3-D image array of rgba values. This can be done with
70
- # :class:`matplotlib.colors.Normalize`. For example, we'll create a gradient
70
+ # :func:`matplotlib.pyplot.imshow` is to pass an array matching the shape of
71
+ # the data to the ``alpha`` argument. For example, we'll create a gradient
71
72
# moving from left to right below.
72
73
73
74
# Create an alpha channel of linearly increasing values moving to the right.
74
75
alphas = np .ones (weights .shape )
75
76
alphas [:, 30 :] = np .linspace (1 , 0 , 70 )
76
77
77
- # Normalize the colors b/w 0 and 1, we'll then pass an MxNx4 array to imshow
78
- colors = Normalize (vmin , vmax , clip = True )(weights )
79
- colors = cmap (colors )
80
-
81
- # Now set the alpha channel to the one we created above
82
- colors [..., - 1 ] = alphas
83
-
84
78
# Create the figure and image
85
79
# Note that the absolute values may be slightly different
86
80
fig , ax = plt .subplots ()
87
81
ax .imshow (greys )
88
- ax .imshow (colors , extent = ( xmin , xmax , ymin , ymax ) )
82
+ ax .imshow (weights , alpha = alphas , ** imshow_kwargs )
89
83
ax .set_axis_off ()
90
84
91
85
###############################################################################
@@ -102,18 +96,11 @@ def normal_pdf(x, mean, var):
102
96
alphas = Normalize (0 , .3 , clip = True )(np .abs (weights ))
103
97
alphas = np .clip (alphas , .4 , 1 ) # alpha value clipped at the bottom at .4
104
98
105
- # Normalize the colors b/w 0 and 1, we'll then pass an MxNx4 array to imshow
106
- colors = Normalize (vmin , vmax )(weights )
107
- colors = cmap (colors )
108
-
109
- # Now set the alpha channel to the one we created above
110
- colors [..., - 1 ] = alphas
111
-
112
99
# Create the figure and image
113
100
# Note that the absolute values may be slightly different
114
101
fig , ax = plt .subplots ()
115
102
ax .imshow (greys )
116
- ax .imshow (colors , extent = ( xmin , xmax , ymin , ymax ) )
103
+ ax .imshow (weights , alpha = alphas , ** imshow_kwargs )
117
104
118
105
# Add contour lines to further highlight different levels.
119
106
ax .contour (weights [::- 1 ], levels = [- .1 , .1 ], colors = 'k' , linestyles = '-' )
0 commit comments