您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Matplotlib:如何绘制图像而不是点?

Matplotlib:如何绘制图像而不是点?

有两种方法可以做到这一点。

第一种方法最容易理解,但是第二种方法具有很大的优势。k注解框方法将使图像在放大时保持恒定大小。使用imshow将图像的大小与图的数据坐标绑定。

这是第二个选项的示例:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.offset@R_449_2419@ import OffsetImage, AnnotationB@R_449_2419@
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 = AnnotationB@R_449_2419@(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()

在此处输入图片说明

其他 2022/1/1 18:28:35 有432人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶