|
1 | 1 | """
|
2 |
| -============================================= |
3 |
| -Blend transparency with color with 2-D images |
4 |
| -============================================= |
| 2 | +=========================================== |
| 3 | +Blend transparency with color in 2-D images |
| 4 | +=========================================== |
5 | 5 |
|
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. In this case, it's common to visualize the statistic of choice (e.g., |
10 |
| -a t-statistic) alongisde another value of interest (e.g., the p-value for that |
11 |
| -statistic). One way to do this is to map the p-value onto the transparency of |
12 |
| -the image such that data with "significant" values are highlighted. |
13 |
| -
|
14 |
| -This example demonstrates how you can achieve this effect using |
15 |
| -:class:`matplotlib.colors.Normalize. Note that it is not possible to directly |
16 |
| -pass alpha values to :func:`matplotlib.pyplot.imshow`. |
| 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`. |
17 | 15 |
|
18 | 16 | First we will generate some data, in this case, we'll create two 2-D "blobs"
|
19 | 17 | in a 2-D grid. One blob will be positive, and the other negative.
|
20 | 18 | """
|
21 |
| -# sphinx_gallery_thumbnail_number = 2 |
| 19 | +# sphinx_gallery_thumbnail_number = 3 |
22 | 20 | import numpy as np
|
23 | 21 | from scipy.stats import multivariate_normal
|
24 | 22 | import matplotlib.pyplot as plt
|
|
31 | 29 | grid = np.array(np.meshgrid(xx, yy))
|
32 | 30 | grid = grid.transpose(2, 1, 0)
|
33 | 31 |
|
34 |
| -# Generate the blobs |
| 32 | +# Generate the blobs. The range of the values is roughly -.0002 to .0002 |
35 | 33 | means_high = [20, 50]
|
36 | 34 | means_low = [50, 60]
|
37 | 35 | cov = [[500, 0], [0, 800]]
|
38 | 36 | gauss_high = multivariate_normal.pdf(grid, means_high, cov)
|
39 | 37 | gauss_low = -1 * multivariate_normal.pdf(grid, means_low, cov)
|
40 | 38 | weights = gauss_high + gauss_low
|
41 | 39 |
|
42 |
| -# We'll plot these blobs using only ``imshow``. |
43 |
| -vmax = np.abs(weights).max() * 1.5 |
| 40 | +# We'll also create a grey background into which the pixels will fade |
| 41 | +greys = np.ones(weights.shape + (3,)) * 70 |
| 42 | + |
| 43 | +# First we'll plot these blobs using only ``imshow``. |
| 44 | +vmax = np.abs(weights).max() |
44 | 45 | vmin = -vmax
|
45 | 46 | cmap = plt.cm.RdYlBu
|
| 47 | + |
46 | 48 | fig, ax = plt.subplots()
|
| 49 | +ax.imshow(greys) |
47 | 50 | ax.imshow(weights, extent=(xmin, xmax, ymin, ymax), cmap=cmap)
|
48 | 51 | ax.set_axis_off()
|
49 | 52 |
|
50 |
| -################################################################################ |
| 53 | +############################################################################### |
51 | 54 | # Blending in transparency
|
52 | 55 | # ========================
|
53 | 56 | #
|
54 |
| -# Below, we'll recreate the same plot, but this time we'll blend in |
55 |
| -# transparency with the image so that the extreme values are highlighted. |
56 |
| -# We'll also add in contour lines to highlight the image values. |
| 57 | +# The simplest way to include transparency when plotting data with |
| 58 | +# :func:`matplotlib.pyplot.imshow` is to convert the 2-D data array to a |
| 59 | +# 3-D image array of rgba values. This can be done with |
| 60 | +# :class:`matplotlib.colors.Normalize`. For example, we'll create a gradient |
| 61 | +# moving from left to right below. |
| 62 | + |
| 63 | +# Create an alpha channel of linearly increasing values moving to the right. |
| 64 | +alphas = np.ones(weights.shape) |
| 65 | +alphas[:, 30:] = np.linspace(1, 0, 70) |
| 66 | + |
| 67 | +# Normalize the colors b/w 0 and 1, we'll then pass an MxNx4 array to imshow |
| 68 | +colors = Normalize(vmin, vmax, clip=True)(weights) |
| 69 | +colors = cmap(colors) |
| 70 | + |
| 71 | +# Now set the alpha channel to the one we created above |
| 72 | +colors[..., -1] = alphas |
| 73 | + |
| 74 | +# Create the figure and image |
| 75 | +# Note that the absolute values may be slightly different |
| 76 | +fig, ax = plt.subplots() |
| 77 | +ax.imshow(greys) |
| 78 | +ax.imshow(colors, extent=(xmin, xmax, ymin, ymax)) |
| 79 | +ax.set_axis_off() |
| 80 | + |
| 81 | +############################################################################### |
| 82 | +# Using transparency to highlight values with high amplitude |
| 83 | +# ========================================================== |
| 84 | +# |
| 85 | +# Finally, we'll recreate the same plot, but this time we'll use transparency |
| 86 | +# to highlight the extreme values in the data. This is often used to highlight |
| 87 | +# data points with smaller p-values. We'll also add in contour lines to |
| 88 | +# highlight the image values. |
57 | 89 |
|
58 | 90 | # Create an alpha channel based on weight values
|
| 91 | +# Any value whose absolute value is > .0001 will have zero transparency |
59 | 92 | alphas = Normalize(0, .0001, clip=True)(np.abs(weights))
|
60 |
| -alphas = np.clip(alphas, .4, 1) |
| 93 | +alphas = np.clip(alphas, .4, 1) # alpha value clipped at the bottom at .4 |
61 | 94 |
|
62 | 95 | # Normalize the colors b/w 0 and 1, we'll then pass an MxNx4 array to imshow
|
63 | 96 | colors = Normalize(vmin, vmax)(weights)
|
|
69 | 102 | # Create the figure and image
|
70 | 103 | # Note that the absolute values may be slightly different
|
71 | 104 | fig, ax = plt.subplots()
|
| 105 | +ax.imshow(greys) |
72 | 106 | ax.imshow(colors, extent=(xmin, xmax, ymin, ymax))
|
73 | 107 |
|
74 | 108 | # Add contour lines to further highlight different levels.
|
|
0 commit comments