如何使用 matplotlib 在绘图的一角插入小图像?
我想要的非常简单:我有一个名为“logo.png”的小图像文件,我想将其显示在绘图的左上角。但您在 matplotlib 示例库中找不到任何相关示例。
我正在使用 django,我的代码是这样的:
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
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 can't find any example of that in the matplotlib examples gallery.
I'm 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望图像位于实际图形的角点(而不是轴的角点),请查看
figimage
。也许是这样的? (使用 PIL 读取图像):
如果您想让图像固定,则还有另一种选择图形宽度/高度的一部分是创建一个“虚拟”轴并使用
imshow
将图像放置在其中。这样,图像的大小和位置就独立于 DPI 和图形的绝对大小: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):
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:现在有一种更简单的方法,使用新的
inset_axes
命令(需要 matplotlib >3.0)。此命令允许将一组新的轴定义为现有轴对象的子对象。这样做的优点是,您可以使用适当的
transform
表达式以任何您喜欢的单位定义插入轴,例如轴分数或数据坐标。所以这是一个代码示例:
此方法的优点是您的图像将随着您的轴重新缩放而自动缩放!
There is now a much easier way, using the new
inset_axes
command (matplotlib >3.0 required).This command allows one to define a new set of axes as a child of an existing
axes
object. The advantage of this is that you can define your inset axes in whatever units you please, like axes fraction or data coordinates, using the appropriatetransform
expression.So here's a code example:
The advantage of this method is that your image will scale automatically as your axes get rescaled!