我正在绘制 3D 散点图:
d = {'x':[1,2,3,4], 'y':[2,3,1,5], 'z':[3,2,3,2], 't':[4,1,2,3], 'score':[2,3,1,2]}
df = pd.DataFrame (d)
xtitle = 'x'
ytitle = 'y'
ztitle = 'z'
trace1 = go.Scatter3d(x=df[xtitle],
y= df[ytitle],
z = df[ztitle],
marker=dict(color=df['score'],
showscale=True,
colorbar=dict(
title='score)'
)),
mode='markers')
layout = go.Layout (
scene = Scene(
xaxis = dict (title = xtitle),
yaxis = dict (title = ytitle),
zaxis = dict (title = ztitle)
)
)
fig = go.Figure(data=[trace1], layout = layout)
plotly.offline.iplot(fig)
当我将鼠标悬停在一个点上时,它会显示 x、y 和 z 值。
在数据框 df
中,我还有一个名为 t
的列,我希望当我将鼠标悬停在一个点上时,它会显示 x、y、z、t并得分。
我该怎么做?
最佳答案
您可以在跟踪中使用 text
或 hoverinfo
。
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
import pandas as pd
d = {'x':[1,2,3,4], 'y':[2,3,1,5], 'z':[3,2,3,2], 't':[4,1,2,3], 'score':[2,3,1,2]}
df = pd.DataFrame (d)
xtitle = 'x'
ytitle = 'y'
ztitle = 'z'
trace1 = go.Scatter3d(
x=df[xtitle],
y= df[ytitle],
z = df[ztitle],
marker=dict(
color=df['score'],
showscale=True,
colorbar=dict(title='score)')
),
mode='markers',
text = ["t: {}".format(x) for x in df['t'] ] # <-- added line!
# hoverinfo = df['t'] # alternative
)
layout = go.Layout (
scene = dict(
xaxis = dict (title = xtitle),
yaxis = dict (title = ytitle),
zaxis = dict (title = ztitle)
)
)
fig = go.Figure(data=[trace1], layout = layout)
iplot(fig)
有用的链接:SO answer 0 , SO answer 1 , SO answer 2 .您想要使用 text
属性、hoverinfo
和 hovertext
。 Here is the documentation .
关于python - 阴谋地徘徊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51264528/