Skip to content

Commit 0558507

Browse files
committed
Merge branch 'image-interpolation-example' of github.com:Stretch97/matplotlib into image-interpolation-example
2 parents b01b74e + 57cb972 commit 0558507

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

CHANGELOG

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2014-04-22 Added an example showing the difference between
2+
interpolation = 'none' and interpolation = 'nearest' in
3+
`imshow()` when saving vector graphics files.
4+
15
2014-04-10 Fixed the triangular marker rendering error. The "Up" triangle was
26
rendered instead of "Right" triangle and vice-versa.
37

examples/images_contours_and_fields/interpolation_none_vs_nearest.py

+26-11
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
Displays the difference between interpolation = 'none' and
33
interpolation = 'nearest'.
44
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.
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.
1112
"""
1213

1314
import numpy as np
@@ -40,8 +41,22 @@
4041
fig.text(0.383, 0.90, "Interpolation = 'none'", ha = 'center')
4142
fig.text(0.75, 0.90, "Interpolation = 'nearest'", ha = 'center')
4243

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')
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

Comments
 (0)