Note
Go to the end to download the full example code
Image Interpolation Methods¶
matplotview.view()
and matplotview.inset_zoom_axes()
support specifying an
image interpolation method via the image_interpolation parameter. This image interpolation
method is used to resize images when displaying them in the view.
9 import matplotlib.pyplot as plt
10 from matplotview import view
11 import numpy as np
12
13 fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
14
15 fig.suptitle("Different interpolations when zoomed in on the bottom left corner.")
16
17 ax1.set_title("Original")
18 ax1.imshow(np.random.rand(100, 100), cmap="Blues", origin="lower")
19 ax1.add_patch(plt.Rectangle((0, 0), 10, 10, ec="red", fc=(0, 0, 0, 0)))
20
21 for ax, interpolation, title in zip([ax2, ax3, ax4], ["nearest", "bilinear", "bicubic"], ["Nearest (Default)", "Bilinear", "Cubic"]):
22 ax.set_title(title)
23 ax.set_xlim(0, 10)
24 ax.set_ylim(0, 10)
25 ax.set_aspect("equal")
26 view(ax, ax1, image_interpolation=interpolation)
27
28 fig.tight_layout()
29 fig.show()
If you want to avoid interpolation artifacts, you can use pcolormesh instead of imshow.
34 import matplotlib.pyplot as plt
35 from matplotview import view
36 import numpy as np
37
38 fig, (ax1, ax2) = plt.subplots(1, 2)
39
40 ax1.set_title("Original")
41 ax1.pcolormesh(np.random.rand(100, 100), cmap="Blues")
42 ax1.add_patch(plt.Rectangle((0, 0), 10, 10, ec="red", fc=(0, 0, 0, 0)))
43 ax1.set_aspect("equal")
44
45 ax2.set_title("Zoomed in View")
46 ax2.set_xlim(0, 10)
47 ax2.set_ylim(0, 10)
48 ax2.set_aspect("equal")
49 view(ax2, ax1)
50
51 fig.tight_layout()
52 fig.show()
Total running time of the script: (0 minutes 3.037 seconds)