[英]Putting a small image on the corner of a plot with matplotlib
这个问题借给Joe Kington的代码( 如何用matplotlib在一个小区的角上插入一个小图片? ):
import matplotlib.pyplot as plt
import Image
import numpy as np
im = Image.open('/home/jofer/logo.png')
height = im.size[1]
# We need a float array between 0-1, rather than
# a uint8 array between 0-255
im = np.array(im).astype(np.float) / 255
fig = plt.figure()
plt.plot(np.arange(10), 4 * np.arange(10))
# With newer (1.0) versions of matplotlib, you can
# use the "zorder" kwarg to make the image overlay
# the plot, rather than hide behind it... (e.g. zorder=10)
fig.figimage(im, 0, fig.bbox.ymax - height)
# (Saving with the same dpi as the screen default to
# avoid displacing the logo image)
fig.savefig('/home/jofer/temp.png', dpi=80)
plt.show()
我尝试了以下方法:
import matplotlib.pyplot as plt
import Image
import numpy as np
im = Image.open('/home/po/pic.jpg')
height = im.size[1]
im = np.array(im).astype(np.float) / 255
fig = plt.figure()
fig.subplots_adjust(top=0.80)
fig.patch.set_facecolor('black')
ax1 = fig.add_subplot(1, 1, 1, axisbg='white')
fig.figimage(im, 0, fig.bbox.ymax - height)
但我的形象是在中心,而不是在中间,是否有办法将其转移,我已经尝试过阅读http://effbot.org/imagingbook/image.htm但无济于事
提前致谢:)
当matplotlib具有相同的功能时,我不认为需要额外的模块导入。
如果要创建插图,只需将额外的轴对象添加(并定位)到图形窗口即可。 它比figimage
方法有一些优点,因为figimage
将未重采样的图像添加到图中
(matplotlib docs)。
这是一个例子:
from scipy import misc
lena = misc.lena()
import matplotlib.pyplot as plt
plt.plot(range(10), range(10))
ax = plt.axes([0.5,0.8, 0.1, 0.1], frameon=True) # Change the numbers in this array to position your image [left, bottom, width, height])
ax.imshow(lena)
ax.axis('off') # get rid of the ticks and ticklabels
plt.show()
我使用lena作为图像,以确保任何人都可以运行代码,但您可以通过键入以下内容来使用matplotlib加载图像:
image = plt.imread('/home/po/pic.jpg')
这将取代您对Image模块的调用,使其过时。 变量image
与上面的小脚本中的变量lena
具有相同的目的。
根据您的使用情况,在照片编辑器中调整图像大小然后使用两行代码可能会更快更清晰:
img = image.imread("my_image.png")
plt.figimage(img, 100, 200, zorder=1, alpha=0.3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.