栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

  1. 根据希望的位置,使用
    imshow
    带有
    extent
    kwarg集的图像进行绘制。
  2. OffsetImage
    里面使用
    AnnotationBbox

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

imshow
将图像的大小与图的数据坐标绑定。

这是第二个选项的示例:

import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.offsetbox import OffsetImage, AnnotationBboxfrom matplotlib.cbook import get_sample_datadef 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 artistsmain()



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/669868.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号