66Blend transparency with color to highlight parts of data with imshow.
77
88A 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.
1513
1614First we will generate some data, in this case, we'll create two 2-D "blobs"
1715in a 2-D grid. One blob will be positive, and the other negative.
@@ -50,42 +48,38 @@ def normal_pdf(x, mean, var):
5048# We'll also create a grey background into which the pixels will fade
5149greys = np .full ((* weights .shape , 3 ), 70 , dtype = np .uint8 )
5250
53- # First we'll plot these blobs using only ``imshow``.
51+ # First we'll plot these blobs using ``imshow`` without transparency .
5452vmax = 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+ }
5759
5860fig , ax = plt .subplots ()
5961ax .imshow (greys )
60- ax .imshow (weights , extent = ( xmin , xmax , ymin , ymax ), cmap = cmap )
62+ ax .imshow (weights , ** imshow_kwargs )
6163ax .set_axis_off ()
6264
6365###############################################################################
6466# Blending in transparency
6567# ========================
6668#
6769# 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
7172# moving from left to right below.
7273
7374# Create an alpha channel of linearly increasing values moving to the right.
7475alphas = np .ones (weights .shape )
7576alphas [:, 30 :] = np .linspace (1 , 0 , 70 )
7677
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-
8478# Create the figure and image
8579# Note that the absolute values may be slightly different
8680fig , ax = plt .subplots ()
8781ax .imshow (greys )
88- ax .imshow (colors , extent = ( xmin , xmax , ymin , ymax ) )
82+ ax .imshow (weights , alpha = alphas , ** imshow_kwargs )
8983ax .set_axis_off ()
9084
9185###############################################################################
@@ -102,18 +96,11 @@ def normal_pdf(x, mean, var):
10296alphas = Normalize (0 , .3 , clip = True )(np .abs (weights ))
10397alphas = np .clip (alphas , .4 , 1 ) # alpha value clipped at the bottom at .4
10498
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-
11299# Create the figure and image
113100# Note that the absolute values may be slightly different
114101fig , ax = plt .subplots ()
115102ax .imshow (greys )
116- ax .imshow (colors , extent = ( xmin , xmax , ymin , ymax ) )
103+ ax .imshow (weights , alpha = alphas , ** imshow_kwargs )
117104
118105# Add contour lines to further highlight different levels.
119106ax .contour (weights [::- 1 ], levels = [- .1 , .1 ], colors = 'k' , linestyles = '-' )
0 commit comments