如何使用matplotlib将一个小图像插入到阴谋的角落? [英] how to insert a small image on the corner of a plot with matplotlib?
问题描述
我使用django,而我的代码就是这样的
def get_bars(request)
...
fig =图(facecolor ='#F0F0F0',figsize =(4.6,4))
...
ax1 = fig.add_subplot(111,ylabel =Valeur,xlabel =Code,autoscale_on = True)
ax1.bar(ind,values,width = width, color ='#FFCC00',edgecolor ='#B33600',linewidth = 1)
...
canvas = FigureCanvas(fig)
response = HttpResponse(content_type ='image / png' )
canvas.print_png(响应)
返回响应
任何想法? thxs提前
如果您想要图像在实际图形的角落(而不是轴的角)查看 figimage
。
也许这样吗? (使用PIL阅读图像):
import matplotlib.pyplot as plt
import Image
import numpy as np
im = Image.open('/ home / jofer / logo.png')
height = im.size [1]
#我们需要在0-1之间的一个float数组,而不是
#a uint8在0-255之间的数组
im = np.array(im).astype(np.float)/ 255
fig = plt.figure()
plt.plot(np.arange(10),4 * np.arange(10))
#用较新的(1.0 )版本的matplotlib,你可以
#使用zorderkwarg使图像叠加
#的情节,而不是隐藏在它后面(例如zorder = 10)
fig .jpg($,$,$)
#(保存与屏幕默认为
#相同的dpi,避免移动徽标图像)
fig.savefig('/ home / jofer / temp.png',dpi = 80)
plt.show()
另一个选择,如果你想h ave图像是图形宽度/高度的固定分数是创建一个虚拟轴,并使用 imshow
将图像放在其中。这样,图像的大小和位置与DPI和图形的绝对大小无关:
import matplotlib.pyplot as plt
从matplotlib.cbook import get_sample_data
im = plt.imread(get_sample_data('grace_hopper.jpg'))
fig,ax = plt.subplots()
ax.plot(range(10))
#将图像放在图的右上角
#-------------- ------------------------------------------
#我们是在_figure_坐标中指定位置和大小,因此图像
#会缩小/增长,因为图形大小调整。删除zorder = -1将
#图像放在轴前。
newax = fig.add_axes([0.8,0.8,0.2,0.2],anchor ='NE',zorder = -1)
newax.imshow(im)
newax.axis('关闭')
plt.show()
What i want is really simple. I have a small image file called "logo.png" that i want to display on the upper left corner of my plots. But you cant find any example of that in the examples gallery of matplotlib
Im using django, and my code is something like this
def get_bars(request)
...
fig = Figure(facecolor='#F0F0F0',figsize=(4.6,4))
...
ax1 = fig.add_subplot(111,ylabel="Valeur",xlabel="Code",autoscale_on=True)
ax1.bar(ind,values,width=width, color='#FFCC00',edgecolor='#B33600',linewidth=1)
...
canvas = FigureCanvas(fig)
response = HttpResponse(content_type='image/png')
canvas.print_png(response)
return response
any ideas?? thxs in advance
If you want the image at the corner of your actual figure (rather than the corner of your axis), look into figimage
.
Perhaps something like this? (using PIL to read the image):
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()
Another option, if you'd like to have the image be a fixed fraction of the figure's width/height is to create a "dummy" axes and place the image in it with imshow
. This way the image's size and position is independent of DPI and the figure's absolute size:
import matplotlib.pyplot as plt
from matplotlib.cbook import get_sample_data
im = plt.imread(get_sample_data('grace_hopper.jpg'))
fig, ax = plt.subplots()
ax.plot(range(10))
# Place the image in the upper-right corner of the figure
#--------------------------------------------------------
# We're specifying the position and size in _figure_ coordinates, so the image
# will shrink/grow as the figure is resized. Remove "zorder=-1" to place the
# image in front of the axes.
newax = fig.add_axes([0.8, 0.8, 0.2, 0.2], anchor='NE', zorder=-1)
newax.imshow(im)
newax.axis('off')
plt.show()
这篇关于如何使用matplotlib将一个小图像插入到阴谋的角落?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!