[英]Hovering plotly
我正在繪制 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
。 這是文檔。
使用hoverinfo參數。
trace1 = go.Scatter3d(x = df['x'],
y = df['y'],
z = df['z'],
text = ['t: %d<br>Score: %d'%(t,s) for t,s in df.loc[:,['t','score']].values],
hoverinfo = 'text',
marker=dict(color=df['score'],
showscale=True,
colorbar=dict(title='score')
),
mode='markers')
這個很有效,而且非常簡單,如果你想向懸停添加更多數據,我會給你一些額外的例子:
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],'t2':[6,8,5,9], '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',
hoverinfo = 'Text t: ' + df['t'] # Display for 't' + content in df
+ '<br>' + # Intro space
'Text t2: ' + df['t2'] # Text to display for 't'
)
layout = go.Layout (
scene = dict(
xaxis = dict (title = xtitle),
yaxis = dict (title = ytitle),
zaxis = dict (title = ztitle)
)
)
fig = go.Figure(data=[trace1], layout = layout)
plotly.offline.iplot(fig)
重要提示:如果您的圖形是帶有痕跡的圖形:您應該使用“hovertext”而不是“hoverinfo”
fig = go.Figure(layout=layout)
fig.add_trace(go.Bar(
x=df[xtitle],
y= df[ytitle],
z = df[ztitle],
marker=dict(
color=df['score'],
showscale=True,
colorbar=dict(title='score)')
),
mode='markers',
hovertext = 'Text t: ' + df['t'] # Display for 't' + content in df
+ '<br>' + # Intro space
'Text t2: ' + df['t2'] # Text to display for 't'
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.