I have the following code, that generates a 3D scatter plot:
JavaScript
x
15
15
1
df_subset = pd.DataFrame(a_dict)
2
3
from matplotlib import pyplot as plt
4
from mpl_toolkits.mplot3d import Axes3D
5
from matplotlib.colors import ListedColormap
6
cmap = ListedColormap(sns.color_palette("husl", 256).as_hex())
7
8
fig = plt.figure(figsize=(16,15))
9
ax = Axes3D(fig)
10
fig.add_axes(ax)
11
sc = ax.scatter(tsne[:,0], tsne[:,1], tsne[:,2], s=40, c=tsne[:,0], marker='o', cmap=cmap, alpha=1)
12
ax.set_xlabel('First Dimention')
13
ax.set_ylabel('Second Dimention')
14
ax.set_zlabel('Third Dimention')
15
What I’m trying to do is connect those 2 points using a directional arrow.
What I want: What i want
Tried ax.annotation
but it doesn’t work. Any suggestions? Preferencially, a for loop to annotate N points, considering the (x1, y1, z1) and (x2, y2, z2)
coordinates.
Advertisement
Answer
This is a possible solution, p1
and p2
are just some points for testing purpose:
JavaScript
1
15
15
1
p1 = np.arange(4)
2
p2 = np.arange(4) * 2
3
4
fig = plt.figure()
5
ax = plt.axes(projection='3d')
6
7
sc1 = ax.scatter(p1[0], p1[1], p1[2],c=p1[3], marker='o', cmap='jet')
8
sc2 = ax.scatter(p2[0], p2[1], p2[2],c=p2[3], marker='o', cmap='jet')
9
10
ax.set_xlabel('First Dimension')
11
ax.set_ylabel('Second Dimension')
12
ax.set_zlabel('Third Dimension')
13
14
ax.quiver3D(p1[0], p1[1], p1[2], (p2[0]-p1[0]), (p2[1]-p1[1]), (p2[2]-p1[2]), length=1, arrow_length_ratio=0.1)
15
Output:
basically ax.quiver3D
has following parameter in the parenthesis:
JavaScript
1
2
1
(x, y, z, dx, dy, dz)
2
x, y, z
is the initial position and dx, dy, dz
is the vector direction.
As for the loop, I think you will manage to adapt the code yourself, will you?