在二维图像中混合透明和颜色

混合透明与颜色,以突出显示部分数据与显示。

matplotlib.pyplot.imshow()的一个常见用途是绘制二维统计图。虽然imshow可以很容易地将二维矩阵可视化为图像,但它并不容易让您为输出添加透明度。例如,可以绘制统计量(例如t统计量)并根据其p值为每个像素的透明度着色。此示例演示如何使用matplotlib.colors.Normalize实现此效果。 请注意,无法直接将alpha值传递给matplotlib.pyplot.imshow()

首先我们将生成一些数据,在这种情况下,我们将在二维网格中创建两个2维“blob”。一个blob是正面的,另一个是负面的。

  1. # sphinx_gallery_thumbnail_number = 3
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from matplotlib.colors import Normalize
  5. def normal_pdf(x, mean, var):
  6. return np.exp(-(x - mean)**2 / (2*var))
  7. # Generate the space in which the blobs will live
  8. xmin, xmax, ymin, ymax = (0, 100, 0, 100)
  9. n_bins = 100
  10. xx = np.linspace(xmin, xmax, n_bins)
  11. yy = np.linspace(ymin, ymax, n_bins)
  12. # Generate the blobs. The range of the values is roughly -.0002 to .0002
  13. means_high = [20, 50]
  14. means_low = [50, 60]
  15. var = [150, 200]
  16. gauss_x_high = normal_pdf(xx, means_high[0], var[0])
  17. gauss_y_high = normal_pdf(yy, means_high[1], var[0])
  18. gauss_x_low = normal_pdf(xx, means_low[0], var[1])
  19. gauss_y_low = normal_pdf(yy, means_low[1], var[1])
  20. weights_high = np.array(np.meshgrid(gauss_x_high, gauss_y_high)).prod(0)
  21. weights_low = -1 * np.array(np.meshgrid(gauss_x_low, gauss_y_low)).prod(0)
  22. weights = weights_high + weights_low
  23. # We'll also create a grey background into which the pixels will fade
  24. greys = np.empty(weights.shape + (3,), dtype=np.uint8)
  25. greys.fill(70)
  26. # First we'll plot these blobs using only ``imshow``.
  27. vmax = np.abs(weights).max()
  28. vmin = -vmax
  29. cmap = plt.cm.RdYlBu
  30. fig, ax = plt.subplots()
  31. ax.imshow(greys)
  32. ax.imshow(weights, extent=(xmin, xmax, ymin, ymax), cmap=cmap)
  33. ax.set_axis_off()

在二维图像中混合透明和颜色

混合透明度

在使用matplotlib.pyplot.imshow()绘制数据时,包含透明度的最简单方法是将二维数据数组转换为RGBA值的三维图像数组。这可以用matplotlib.colors.Normalize来实现。例如,我们将创建一个从左向右移动的渐变。

  1. # Create an alpha channel of linearly increasing values moving to the right.
  2. alphas = np.ones(weights.shape)
  3. alphas[:, 30:] = np.linspace(1, 0, 70)
  4. # Normalize the colors b/w 0 and 1, we'll then pass an MxNx4 array to imshow
  5. colors = Normalize(vmin, vmax, clip=True)(weights)
  6. colors = cmap(colors)
  7. # Now set the alpha channel to the one we created above
  8. colors[..., -1] = alphas
  9. # Create the figure and image
  10. # Note that the absolute values may be slightly different
  11. fig, ax = plt.subplots()
  12. ax.imshow(greys)
  13. ax.imshow(colors, extent=(xmin, xmax, ymin, ymax))
  14. ax.set_axis_off()

在二维图像中混合透明和颜色2

使用透明度高亮显示高振幅值

最后,我们将重新创建相同的图,但是这一次我们将使用透明来突出显示数据中的极端值。这通常用于突出显示具有较小p值的数据点。我们还将添加等高线,以突出显示图像值。评星

  1. # Create an alpha channel based on weight values
  2. # Any value whose absolute value is > .0001 will have zero transparency
  3. alphas = Normalize(0, .3, clip=True)(np.abs(weights))
  4. alphas = np.clip(alphas, .4, 1) # alpha value clipped at the bottom at .4
  5. # Normalize the colors b/w 0 and 1, we'll then pass an MxNx4 array to imshow
  6. colors = Normalize(vmin, vmax)(weights)
  7. colors = cmap(colors)
  8. # Now set the alpha channel to the one we created above
  9. colors[..., -1] = alphas
  10. # Create the figure and image
  11. # Note that the absolute values may be slightly different
  12. fig, ax = plt.subplots()
  13. ax.imshow(greys)
  14. ax.imshow(colors, extent=(xmin, xmax, ymin, ymax))
  15. # Add contour lines to further highlight different levels.
  16. ax.contour(weights[::-1], levels=[-.1, .1], colors='k', linestyles='-')
  17. ax.set_axis_off()
  18. plt.show()
  19. ax.contour(weights[::-1], levels=[-.0001, .0001], colors='k', linestyles='-')
  20. ax.set_axis_off()
  21. plt.show()

在二维图像中混合透明和颜色3

参考

本例中显示了以下函数、方法和类的使用:

  1. import matplotlib
  2. matplotlib.axes.Axes.imshow
  3. matplotlib.pyplot.imshow
  4. matplotlib.axes.Axes.contour
  5. matplotlib.pyplot.contour
  6. matplotlib.colors.Normalize
  7. matplotlib.axes.Axes.set_axis_off

下载这个示例