1
+ """
2
+ Displays the difference between interpolation = 'none' and
3
+ interpolation = 'nearest'.
4
+
5
+ Interpolation = 'none' and 'nearest' are equivalent when converting a
6
+ figure to an image file, such as a PNG. Interpolation = 'none'
7
+ and interpolation = 'nearest' behave quite differently, however, when
8
+ converting a figure to a vector graphics file, such as a PDF. As shown,
9
+ Interpolation = 'none' works well when a big image is scaled down, while
10
+ interpolation = 'nearest' works well when a small image is blown up.
11
+ """
12
+
13
+ import numpy as np
14
+ import matplotlib .pyplot as plt
15
+ import matplotlib .cbook as cbook
16
+
17
+ #Load big image
18
+ big_im_path = cbook .get_sample_data ('necked_tensile_specimen.png' )
19
+ big_im = plt .imread (big_im_path )
20
+ #Define small image
21
+ small_im = np .array ([[0.25 , 0.75 , 1.0 , 0.75 ], [0.1 , 0.65 , 0.5 , 0.4 ], \
22
+ [0.6 , 0.3 , 0.0 , 0.2 ], [0.7 , 0.9 , 0.4 , 0.6 ]])
23
+
24
+ #Create a 2x2 table of plots
25
+ fig = plt .figure (figsize = [8.0 , 7.5 ])
26
+ ax = plt .subplot (2 ,2 ,1 )
27
+ ax .imshow (big_im , interpolation = 'none' )
28
+ ax = plt .subplot (2 ,2 ,2 )
29
+ ax .imshow (big_im , interpolation = 'nearest' )
30
+ ax = plt .subplot (2 ,2 ,3 )
31
+ ax .imshow (small_im , interpolation = 'none' )
32
+ ax = plt .subplot (2 ,2 ,4 )
33
+ ax .imshow (small_im , interpolation = 'nearest' )
34
+ plt .subplots_adjust (left = 0.24 , wspace = 0.2 , hspace = 0.1 , \
35
+ bottom = 0.05 , top = 0.86 )
36
+
37
+ #Label the rows and columns of the table
38
+ fig .text (0.03 , 0.645 , 'Big Image\n Scaled Down' , ha = 'left' )
39
+ fig .text (0.03 , 0.225 , 'Small Image\n Blown Up' , ha = 'left' )
40
+ fig .text (0.383 , 0.90 , "Interpolation = 'none'" , ha = 'center' )
41
+ fig .text (0.75 , 0.90 , "Interpolation = 'nearest'" , ha = 'center' )
42
+
43
+ #Save as a png and as a pdf
44
+ txt = fig .text (0.452 , 0.95 , 'Saved as a PNG' , fontsize = 18 )
45
+ plt .savefig ('Nearest_vs_none.png' , bbox_inches = 'tight' )
46
+ txt .set_text ('Saved as a PDF' )
47
+ plt .savefig ('Nearest_vs_none.pdf' , bbox_inches = 'tight' )
0 commit comments