演示轴 RGB

RGBAxes显示RGB合成图像。

演示轴 RGB

演示轴 RGB2

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from mpl_toolkits.axes_grid1.axes_rgb import make_rgb_axes, RGBAxes
  4. def get_demo_image():
  5. from matplotlib.cbook import get_sample_data
  6. f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
  7. z = np.load(f)
  8. # z is a numpy array of 15x15
  9. return z, (-3, 4, -4, 3)
  10. def get_rgb():
  11. Z, extent = get_demo_image()
  12. Z[Z < 0] = 0.
  13. Z = Z/Z.max()
  14. R = Z[:13, :13]
  15. G = Z[2:, 2:]
  16. B = Z[:13, 2:]
  17. return R, G, B
  18. def make_cube(r, g, b):
  19. ny, nx = r.shape
  20. R = np.zeros([ny, nx, 3], dtype="d")
  21. R[:, :, 0] = r
  22. G = np.zeros_like(R)
  23. G[:, :, 1] = g
  24. B = np.zeros_like(R)
  25. B[:, :, 2] = b
  26. RGB = R + G + B
  27. return R, G, B, RGB
  28. def demo_rgb():
  29. fig, ax = plt.subplots()
  30. ax_r, ax_g, ax_b = make_rgb_axes(ax, pad=0.02)
  31. #fig.add_axes(ax_r)
  32. #fig.add_axes(ax_g)
  33. #fig.add_axes(ax_b)
  34. r, g, b = get_rgb()
  35. im_r, im_g, im_b, im_rgb = make_cube(r, g, b)
  36. kwargs = dict(origin="lower", interpolation="nearest")
  37. ax.imshow(im_rgb, **kwargs)
  38. ax_r.imshow(im_r, **kwargs)
  39. ax_g.imshow(im_g, **kwargs)
  40. ax_b.imshow(im_b, **kwargs)
  41. def demo_rgb2():
  42. fig = plt.figure(2)
  43. ax = RGBAxes(fig, [0.1, 0.1, 0.8, 0.8], pad=0.0)
  44. #fig.add_axes(ax)
  45. #ax.add_RGB_to_figure()
  46. r, g, b = get_rgb()
  47. kwargs = dict(origin="lower", interpolation="nearest")
  48. ax.imshow_rgb(r, g, b, **kwargs)
  49. ax.RGB.set_xlim(0., 9.5)
  50. ax.RGB.set_ylim(0.9, 10.6)
  51. for ax1 in [ax.RGB, ax.R, ax.G, ax.B]:
  52. for sp1 in ax1.spines.values():
  53. sp1.set_color("w")
  54. for tick in ax1.xaxis.get_major_ticks() + ax1.yaxis.get_major_ticks():
  55. tick.tick1line.set_mec("w")
  56. tick.tick2line.set_mec("w")
  57. return ax
  58. demo_rgb()
  59. ax = demo_rgb2()
  60. plt.show()

下载这个示例