使用Matplotlib在图上叠加旋转的图像 [英] Overlay rotated Images on plot with Matplotlib

查看:73
本文介绍了使用Matplotlib在图上叠加旋转的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前已经使用矩形补丁构建了一个绘图,以显示一系列位置.

用于生成此代码的代码(基于 RLPy 库构建)-

  def visualize_trajectory(self,trajectory = [[0,0,0,0],[0.1,0.1,0,0]]):domain_fig = plt.figure()对于 i, s in enumerate(trajectory):x, y, 速度, 航向 = s[:4]car_xmin = x - self.REAR_WHEEL_RELATIVE_LOCcar_ymin = y-self.CAR_WIDTH/2.car_fig = matplotlib.patches.Rectangle([car_xmin,car_ymin],self.CAR_LENGTH,self.CAR_WIDTH,alpha =(0.8 * i)/len(轨迹))旋转 = Affine2D().rotate_deg_around(x,y,标题* 180/np.pi)+ plt.gca().transDatacar_fig.set_transform(旋转)plt.gca().add_patch(car_fig)

有没有办法用图像覆盖每个补丁?理想情况下,在每个位置上都将有汽车图像而不是矩形.

我玩过AnnotationBbox和

I've currently constructed a plot using rectangle Patches to display a sequence of positions.

EDIT: Code used to generate this (built off of the RLPy library)-

def visualize_trajectory(self, trajectory=[[0,0,0,0], [0.1,0.1,0,0]]):
    domain_fig = plt.figure()

    for i, s in enumerate(trajectory):
        x, y, speed, heading = s[:4]
        car_xmin = x - self.REAR_WHEEL_RELATIVE_LOC
        car_ymin = y - self.CAR_WIDTH / 2.

        car_fig = matplotlib.patches.Rectangle(
            [car_xmin,
             car_ymin],
            self.CAR_LENGTH,
            self.CAR_WIDTH,
            alpha=(0.8 * i) / len(trajectory) )
        rotation = Affine2D().rotate_deg_around(
            x, y, heading * 180 / np.pi) + plt.gca().transData
        car_fig.set_transform(rotation)
        plt.gca().add_patch(car_fig)

Is there any way to overlay each of these patches with images? Ideally, there would be a car image instead of a rectangle at each of the positions.

I've played around with AnnotationBbox and TransformedBbox, but both seem to be inflexible when dealing with rotations.

解决方案

Take a look at demo_affine_image from the matplotlib gallery. It shows how to rotate an image.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
import matplotlib.cbook as cbook

def get_image():
    fn = cbook.get_sample_data("necked_tensile_specimen.png")
    arr = plt.imread(fn)
    # make background transparent
    # you won't have to do this if your car image already has a transparent background
    mask = (arr == (1,1,1,1)).all(axis=-1)
    arr[mask] = 0
    return arr

def imshow_affine(ax, z, *args, **kwargs):
    im = ax.imshow(z, *args, **kwargs)
    x1, x2, y1, y2 = im.get_extent()
    im._image_skew_coordinate = (x2, y1)
    return im

N = 7
x = np.linspace(0, 1, N)
y = x**1.1
heading = np.linspace(10, 90, N)
trajectory = list(zip(x, y, heading))
width, height = 0.3, 0.3
car = get_image()
fig, ax = plt.subplots()
for i, t in enumerate(trajectory, start=1):
    xi, yi, deg = t
    im = imshow_affine(ax, car, interpolation='none',
                       extent=[0, width, 0, height], clip_on=True,
                       alpha=0.8*i/len(trajectory))
    center_x, center_y = width//2, height//2
    im_trans = (mtransforms.Affine2D()
                .rotate_deg_around(center_x, center_y, deg)
                .translate(xi, yi)
                + ax.transData)
    im.set_transform(im_trans)

ax.set_xlim(-0.5, 1.5)
ax.set_ylim(-0.5, 1.7)
plt.show()

这篇关于使用Matplotlib在图上叠加旋转的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆