I have a simple plot, where I want to insert image of UAV, but it doesn't show up. I believe that the annotation box is somewhere out of area of plot, but can't figure out where to move it. Currently I want to have it at [2,4], just to test.
Here is my code:
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()
CodePudding user response:
I could not find the problem with annotation box, but I have managed to fix this by adding the image to the plot by imshow
instead. See the code:
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)
output: