matplotlib 带图像的3D图-图像不可见

yx2lnoni  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(172)

我有一个简单的情节,我想插入无人机的图像,但它不显示。我相信注解框是在绘图区域之外的某个地方,但不知道将其移动到哪里。现在我想把它放在[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()
z3yyvxxp

z3yyvxxp1#

我找不到注解框的问题,但我设法通过将图像添加到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)

输出:

相关问题