带图像的Matplotlib 3D绘图-图像不可见

fykwrbwg  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(200)

我有一个简单的绘图,我想在其中插入无人机的图像,但它没有出现。我相信注解框是在绘图区域之外的某个地方,但不能找出在哪里移动它。目前我想有它在[2,4],只是为了测试。
下面是我的代码:

from mpl_toolkits import mplot3d

import numpy as np
import matplotlib.pyplot as plt
import random

from matplotlib.offsetbox import (OffsetImage, AnnotationBbox)
import matplotlib.image as image

fig = plt.figure()
ax = plt.axes(projection="3d")

num_bars = 3
x_pos = random.sample(range(20), num_bars)
y_pos = random.sample(range(20), num_bars)
z_pos = [0] * num_bars
x_size = np.ones(num_bars)
y_size = np.ones(num_bars)
z_size = random.sample(range(20), num_bars)

#ax.bar3d(x_pos, y_pos, z_pos, x_size, y_size, z_size, color='grey')

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_xlim(0,20)
ax.set_ylim(0,20)
ax.set_zlim(0,30)
"""
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])
"""

img="./UAV.png"
uav = image.imread(img)

arr_img = plt.imread("./UAV.png", format='png')
imagebox = OffsetImage(arr_img, zoom = .15)
imagebox.image.axes = ax
#ab = AnnotationBbox(imagebox, (5, 10), xybox = (5.0, 10.0), box_alignment=(1, 0))
ab = AnnotationBbox(imagebox, [2., 4.],
                        xycoords='data',
                        boxcoords="offset points",
                        pad=0
                        )
ax.add_artist(ab)

ax.bar3d(0,0,0,4,4,25,color="grey")

ax.bar3d(16,16,0,4,4,27,color="grey")

ax.bar3d(0,16,0,4,4,23,color="grey")

plt.tight_layout()
plt.show()
zzlelutf

zzlelutf1#

我找不到注解框的问题,但是我已经设法通过imshow将图像添加到绘图中来解决这个问题。参见代码:

arr_img = plt.imread("./UAV.png", format='png')
newax = fig.add_axes([0.45,0.5,0.2,0.2], anchor='NE', zorder=1)
newax.imshow(arr_img)
newax.patch.set_alpha(0.01)
newax.get_xaxis().set_ticks([])
newax.get_yaxis().set_ticks([])
newax.spines['top'].set_visible(False)
newax.spines['right'].set_visible(False)
newax.spines['bottom'].set_visible(False)
newax.spines['left'].set_visible(False)

输出:

相关问题