我一般来说是 matplotlib/python 的新手,并使用 matplotlib 创建了一个简单的 3d 散点图。我能够生成散点图,但现在我希望当鼠标悬停在每个点上时显示每个点的名称。这些名称将来自我正在使用的数据集:
df 是数据框
z = df['est_diameter_max']
x = df['miss_distance']
y = df['relative_velocity']
n = df['name']
fig = plt.figure(figsize = (20,17))
ax = plt.axes(projection ="3d")
ax.xaxis.set_pane_color((0.0, 0.0, 0.0, 0.0))
ax.yaxis.set_pane_color((0.0, 0.0, 0.0, 0.0))
ax.zaxis.set_pane_color((0.0, 0.0, 0.0, 0.0))
ax.xaxis.line.set_color("red")
ax.yaxis.line.set_color("green")
ax.zaxis.line.set_color("white")
ax.tick_params(axis='x', colors='red') # only affects
ax.tick_params(axis='y', colors='green') # tick labels
ax.tick_params(axis='z', colors='white') # not tick marks
ax.scatter3D(x, y, z, color = "white")
ax.set_facecolor("black")
plt.title("Hazardous Near Earth Objects that Missed (2020-2022)", fontweight ='bold')
ax.set_xlabel('Missed Distance in KM', fontweight ='bold', color='red')
ax.set_ylabel('Velocity Relative to Earth', fontweight ='bold', color='green')
ax.set_zlabel('Estimate Max Diameter in KM', fontweight ='bold', color='white')
plt.show()
我可以在网上找到的任何代码,但它要么什么也不做,要么给我提供我不理解的错误消息。我担心这超出了我的能力,但我想如果向我解释一下我就能理解。
试试这个(从如何向绘图添加悬停注释借用了一些代码):
import pandas as pd
import matplotlib.pyplot as plt
def update_annot(ind):
pos = sc.get_offsets()[ind["ind"][0]]
annot.xy = pos
text = "{}".format(" ".join([data[n][3] for n in ind["ind"]]))
annot.set_text(text)
annot.get_bbox_patch().set_alpha(0.4)
def on_plot_hover(event):
if event.inaxes == ax:
cont, ind = sc.contains(event)
if cont:
update_annot(ind)
annot.set_visible(True)
fig.canvas.draw_idle()
else:
annot.set_visible(False)
fig.canvas.draw_idle()
data = [
[123, 456, 78, 'larry'],
[321, 654., 87, 'moe'],
[111, 222, 33, 'curly']
]
df = pd.DataFrame(data, columns=['est_diameter_max','miss_distance','relative_velocity','name'])
z = df['est_diameter_max']
x = df['miss_distance']
y = df['relative_velocity']
n = df['name']
fig = plt.figure(figsize = (20,17))
ax = plt.axes(projection ="3d")
ax.xaxis.set_pane_color((0.0, 0.0, 0.0, 0.0))
ax.yaxis.set_pane_color((0.0, 0.0, 0.0, 0.0))
ax.zaxis.set_pane_color((0.0, 0.0, 0.0, 0.0))
ax.xaxis.line.set_color("red")
ax.yaxis.line.set_color("green")
ax.zaxis.line.set_color("white")
ax.tick_params(axis='x', colors='red') # only affects
ax.tick_params(axis='y', colors='green') # tick labels
ax.tick_params(axis='z', colors='white') # not tick marks
sc = ax.scatter3D(x, y, z, color = "white", label='my data')
ax.set_facecolor("black")
plt.title("Hazardous Near Earth Objects that Missed (2020-2022)", fontweight ='bold')
ax.set_xlabel('Missed Distance in KM', fontweight ='bold', color='red')
ax.set_ylabel('Velocity Relative to Earth', fontweight ='bold', color='green')
ax.set_zlabel('Estimate Max Diameter in KM', fontweight ='bold', color='white')
fig.canvas.mpl_connect('motion_notify_event', on_plot_hover)
annot = ax.annotate("", xy=(0,0), xytext=(20,20),textcoords="offset points",
bbox=dict(boxstyle="round", fc="w"),
arrowprops=dict(arrowstyle="->"))
annot.set_visible(False)
plt.show()