MATLAB

imshowpair

  1. figure,imshowpair(X0,Twarp,'falsecolor')

Python

https://github.com/lebedov/imshowpair

  1. #!/usr/bin/env python3
  2. # Copyright (c) 2018, Lev E. Givon
  3. # All rights reserved.
  4. # Distributed under the terms of the BSD license:
  5. # http://www.opensource.org/licenses/bsd-license
  6. import matplotlib.pyplot as plt
  7. import numpy as np
  8. def imshowpair(a, b, method=None, show_all=True, axes_visible=False, cmap=None,
  9. interpolation=None, grid=False, *args, **kwargs):
  10. """
  11. Compare images.
  12. Parameters
  13. ----------
  14. a, b : numpy.ndarray
  15. Images to compare.
  16. method : callable
  17. Callable to apply to images. Must support at least two arguments;
  18. additional specified arguments are passed to `method`.
  19. If None, both images are displayed side by side.
  20. show_all : bool
  21. If True and `method` is defined, display original images
  22. alongside `method(a, b)`.
  23. axes_visible : bool
  24. Show axes if True.
  25. cmap : matplotlib.colors.Colormap
  26. Color map to use when displaying images.
  27. interpolation : str
  28. Interpolation method for `imshow` to use.
  29. grid : bool
  30. If True, display grid.
  31. """
  32. def turn_off_ticks(ax):
  33. for tic in ax.xaxis.get_major_ticks():
  34. tic.tick1On = tic.tick2On = False
  35. for tic in ax.yaxis.get_major_ticks():
  36. tic.tick1On = tic.tick2On = False
  37. a = np.asarray(a)
  38. b = np.asarray(b)
  39. a_shape = a.shape
  40. b_shape = b.shape
  41. ax_list = []
  42. if method == None:
  43. ax0 = ax = plt.subplot(121)
  44. plt.imshow(a, origin='upper', cmap=cmap, interpolation=interpolation)
  45. plt.grid(grid)
  46. turn_off_ticks(ax)
  47. ax_list.append(ax)
  48. ax = plt.subplot(122, sharex=ax0, sharey=ax0)
  49. plt.imshow(b, origin='upper', cmap=cmap, interpolation=interpolation)
  50. plt.grid(grid)
  51. turn_off_ticks(ax)
  52. if a_shape == b_shape:
  53. ax.set_yticklabels([])
  54. ax_list.append(ax)
  55. plt.subplots_adjust()
  56. elif callable(method):
  57. c = method(a, b, *args, **kwargs)
  58. c_shape = c.shape
  59. if show_all:
  60. ax0 = ax = plt.subplot(131)
  61. plt.imshow(a, origin='upper', cmap=cmap, interpolation=interpolation)
  62. plt.grid(grid)
  63. turn_off_ticks(ax)
  64. ax_list.append(ax)
  65. ax = plt.subplot(132, sharex=ax0, sharey=ax0)
  66. plt.imshow(b, origin='upper', cmap=cmap, interpolation=interpolation)
  67. plt.grid(grid)
  68. turn_off_ticks(ax)
  69. if a_shape == b_shape:
  70. ax.set_yticklabels([])
  71. ax_list.append(ax)
  72. ax = plt.subplot(133, sharex=ax0, sharey=ax0)
  73. plt.imshow(c, origin='upper', cmap=cmap, interpolation=interpolation)
  74. plt.grid(grid)
  75. turn_off_ticks(ax)
  76. plt.subplots_adjust()
  77. if c_shape == b_shape:
  78. ax.set_yticklabels([])
  79. ax_list.append(ax)
  80. else:
  81. ax = plt.subplot(111)
  82. plt.imshow(c, origin='upper', cmap=cmap, interpolation=interpolation)
  83. plt.grid(grid)
  84. turn_off_ticks(ax)
  85. if c_shape == b_shape:
  86. ax.set_yticklabels([])
  87. ax_list.append(ax)
  88. else:
  89. NotImplementedError
  90. if not axes_visible:
  91. for ax in ax_list:
  92. ax.set_xticklabels([])
  93. ax.set_yticklabels([])