pythonmatplotlib制作图像_Matplotlib:如何绘制图像而不是点?

有两种方法可以做到这一点。使用imshow和extentkwarg集合根据您希望图像位于的位置绘制图像。

在AnnotationBbox内部使用OffsetImage。

第一种方法最容易理解,但第二种方法有很大的优势。K注释框方法将允许图像在放大时保持恒定大小。使用imshow将把图像的大小与绘图的数据坐标联系起来。

下面是第二个选项的示例:import numpy as np

import matplotlib.pyplot as plt

from matplotlib.offsetbox import OffsetImage, AnnotationBbox

from matplotlib.cbook import get_sample_data

def main():

x = np.linspace(0, 10, 20)

y = np.cos(x)

image_path = get_sample_data('ada.png')

fig, ax = plt.subplots()

imscatter(x, y, image_path, zoom=0.1, ax=ax)

ax.plot(x, y)

plt.show()

def imscatter(x, y, image, ax=None, zoom=1):

if ax is None:

ax = plt.gca()

try:

image = plt.imread(image)

except TypeError:

# Likely already an array...

pass

im = OffsetImage(image, zoom=zoom)

x, y = np.atleast_1d(x, y)

artists = []

for x0, y0 in zip(x, y):

ab = AnnotationBbox(im, (x0, y0), xycoords='data', frameon=False)

artists.append(ax.add_artist(ab))

ax.update_datalim(np.column_stack([x, y]))

ax.autoscale()

return artists

main()

WAk8k.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值