简体   繁体   English

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

[英]Matplotlib: How to plot images instead of points?

I want to read a list of images into Python/Matplotlib and then plot this images instead of other markers (like points) in a graph.我想将图像列表读入 Python/Matplotlib,然后在图形中绘制这些图像而不是其他标记(如点)。 I have tried with imshow but I didn't succeed, because I cannot shift the image to another position and scale it appropriately.我曾尝试使用 imshow 但我没有成功,因为我无法将图像移动到另一个位置并适当地缩放它。 Maybe somebody has a good idea : )也许有人有一个好主意:) 它应该看起来像这样! (没有海绵宝宝,但当然有非常严肃的数据!)

There are two ways to do this.有两种方法可以做到这一点。

  1. Plot the image using imshow with the extent kwarg set based on the location you want the image at.使用imshow绘制图像,并根据您想要图像的位置设置extent kwarg。
  2. Use an OffsetImage inside an AnnotationBbox .AnnotationBbox使用OffsetImage

The first way is the easiest to understand, but the second has a large advantage.第一种方式最容易理解,但第二种方式有很大的优势。 kThe annotation box approach will allow the image to stay at a constant size as you zoom in. Using imshow will tie the size of the image to the data coordinates of the plot. k注释框方法将允许图像在放大时保持恒定大小。使用imshow会将图像的大小与绘图的数据坐标联系起来。

Here's an example of the second option:这是第二个选项的示例:

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()

在此处输入图片说明

If you want different images:如果你想要不同的图像:

This is now the first reply when googling "matplotlib scatter with images".现在这是谷歌搜索“matplotlib scatter with images”时的第一个回复。 If you're like me and actually need to plot different images on each image, try this minimalied example instead.如果你像我一样,实际上需要在每张图像上绘制不同的图像,试试这个最小化的例子。 Just be sure to input your own images.请务必输入您自己的图像。

import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox

def getImage(path):
    return OffsetImage(plt.imread(path))

paths = [
    'a.jpg',
    'b.jpg',
    'c.jpg',
    'd.jpg',
    'e.jpg']

x = [0,1,2,3,4]
y = [0,1,2,3,4]

fig, ax = plt.subplots()
ax.scatter(x, y) 

for x0, y0, path in zip(x, y,paths):
    ab = AnnotationBbox(getImage(path), (x0, y0), frameon=False)
    ax.add_artist(ab)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM