|
| 1 | +""" |
| 2 | +Displays the difference between interpolation = 'none' and |
| 3 | +interpolation = 'nearest'. |
| 4 | +
|
| 5 | +Interpolation = 'none' and interpolation = 'nearest' are equivalent when |
| 6 | +converting a figure to an image file, such as a PNG. |
| 7 | +Interpolation = 'none' and interpolation = 'nearest' behave quite |
| 8 | +differently, however, when converting a figure to a vector graphics file, |
| 9 | +such as a PDF. As shown, Interpolation = 'none' works well when a big |
| 10 | +image is scaled down, while interpolation = 'nearest' works well when a |
| 11 | +small image is blown up. |
| 12 | +""" |
| 13 | + |
| 14 | +import numpy as np |
| 15 | +import matplotlib.pyplot as plt |
| 16 | +import matplotlib.cbook as cbook |
| 17 | + |
| 18 | +#Load big image |
| 19 | +big_im_path = cbook.get_sample_data('necked_tensile_specimen.png') |
| 20 | +big_im = plt.imread(big_im_path) |
| 21 | +#Define small image |
| 22 | +small_im = np.array([[0.25, 0.75, 1.0, 0.75], [0.1, 0.65, 0.5, 0.4], \ |
| 23 | + [0.6, 0.3, 0.0, 0.2], [0.7, 0.9, 0.4, 0.6]]) |
| 24 | + |
| 25 | +#Create a 2x2 table of plots |
| 26 | +fig = plt.figure(figsize = [8.0, 7.5]) |
| 27 | +ax = plt.subplot(2,2,1) |
| 28 | +ax.imshow(big_im, interpolation = 'none') |
| 29 | +ax = plt.subplot(2,2,2) |
| 30 | +ax.imshow(big_im, interpolation = 'nearest') |
| 31 | +ax = plt.subplot(2,2,3) |
| 32 | +ax.imshow(small_im, interpolation = 'none') |
| 33 | +ax = plt.subplot(2,2,4) |
| 34 | +ax.imshow(small_im, interpolation = 'nearest') |
| 35 | +plt.subplots_adjust(left = 0.24, wspace = 0.2, hspace = 0.1, \ |
| 36 | + bottom = 0.05, top = 0.86) |
| 37 | + |
| 38 | +#Label the rows and columns of the table |
| 39 | +fig.text(0.03, 0.645, 'Big Image\nScaled Down', ha = 'left') |
| 40 | +fig.text(0.03, 0.225, 'Small Image\nBlown Up', ha = 'left') |
| 41 | +fig.text(0.383, 0.90, "Interpolation = 'none'", ha = 'center') |
| 42 | +fig.text(0.75, 0.90, "Interpolation = 'nearest'", ha = 'center') |
| 43 | + |
| 44 | +#If you were going to run this example on your local machine, you |
| 45 | +#would save the figure as a PNG, save the same figure as a PDF, and |
| 46 | +#then compare them. The following code would suffice. |
| 47 | +txt = fig1.text(0.452, 0.95, 'Saved as a PNG', fontsize = 18) |
| 48 | +# plt.savefig('None_vs_nearest-png.png') |
| 49 | +# txt.set_text('Saved as a PDF') |
| 50 | +# plt.savefig('None_vs_nearest-pdf.pdf') |
| 51 | + |
| 52 | +#Here, however, we need to display the PDF on a webpage, which means |
| 53 | +#the PDF must be converted into an image. For the purposes of this |
| 54 | +#example, the 'Nearest_vs_none-pdf.pdf' has been pre-converted into |
| 55 | +#'Nearest_vs_none-pdf.png' at 80 dpi. We simply need to load and |
| 56 | +#display it. |
| 57 | +pdf_im_path = cbook.get_sample_data('None_vs_nearest-pdf.png') |
| 58 | +pdf_im = plt.imread(pdf_im_path) |
| 59 | +fig2 = plt.figure(figsize = [8.0, 7.5]) |
| 60 | +plt.figimage(pdf_im) |
| 61 | + |
| 62 | +plt.show() |
0 commit comments