首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >matplotlib:添加一个图片作为注解并旋转它

matplotlib:添加一个图片作为注解并旋转它
EN

Stack Overflow用户
提问于 2020-11-18 22:32:53
回答 1查看 182关注 0票数 1

我尝试在matplotlib图的某个位置添加一些图像。

图像应独立于底层图形的缩放因子,但应具有一定的旋转。(实际上,我想显示一个飞机符号和一个带有正确航向的跑道符号,作为计算出的路径的叠加,这是一个普通的matplotlib线状图。)

我按照这里的建议将图像插入到了正确的位置和正确的大小:How to insert an image (a picture or a photo) in a matplotlib figure,但我还不能将显示的图像旋转到正确的标题中。

有什么想法吗?老实说,我对Matplotlib注解是个新手,所以我不知道用谷歌搜索什么。

干杯,

费利克斯

EN

回答 1

Stack Overflow用户

发布于 2020-11-19 06:03:56

我不确定这是否回答了你的问题,但希望它能给你一个起点!我还没有完成角度的计算,但再次,希望它能给你一些想法。

代码语言:javascript
复制
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
import numpy as np
from scipy import interpolate, ndimage

# Set x value of image centre
x_image = 7


# Set up matplotlib figure with axis
fig, ax = plt.subplots()


# Create arbitrary curve for path
def f_curve(x):
    return 0.1*x**2

x = np.arange(0,10,0.01)   # start,stop,step
y = f_curve(x)

plt.plot(x, y)


# create spline function (to get image centre y-value and derivative at that point)
f = interpolate.InterpolatedUnivariateSpline(x, y, k=1)
xs = np.linspace(min(x), max(x), 100)
plt.plot(xs, f(xs), 'g', lw=3)


# Get y-value for image centre 
# (replace this with your own y-value if you already know it)
y_image = f(x_image)


# Read in image
img = plt.imread('batman.png')

# Convert image into numpy array
image_arr = np.array(img)

# Calculate the derivative of the path curve at x_image to get the angle of rotation required
angle = np.rad2deg(f.derivative()(x_image))-90

# Rotate image
image_arr = ndimage.rotate(image_arr, angle, reshape=True)

# inspired by 
# https://moonbooks.org/Articles/How-to-insert-an-image-a-picture-or-a-photo-in-a-matplotlib-figure/
imagebox = OffsetImage(image_arr, zoom=0.2)
ab = AnnotationBbox(imagebox, (x_image, y_image),bboxprops={'edgecolor':'none','alpha':0.1})
ax.add_artist(ab)
plt.draw()

plt.show()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64895152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档