Python 盘旋

Python 盘旋,python,python-3.x,plotly,Python,Python 3.x,Plotly,我正在绘制三维散点图: 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],

我正在绘制三维散点图:

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和分数

我该怎么做呢?

使用参数

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')
trace1=go.Scatter3d(x=df['x'],
y=df['y'],
z=df['z'],
text=['t:%d
分数:%d%%(t,s)表示df.loc[:,['t','Score']]中的t,s。值], hoverinfo='text', marker=dict(颜色=df['score'], showscale=True, colorbar=dict(title='score') ), 模式(=”标记“)

您可以在跟踪中使用
文本
悬停信息

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)
导入plotly.graph\u objs as go
从plotly.offline导入下载\u plotlyjs,初始化\u笔记本\u模式,绘图,iplot
初始笔记本模式(已连接=真)
作为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],
记号笔(
颜色=df[“分数”],
showscale=True,
colorbar=dict(title='score'))
),
mode='markers',

text=[“t:{}”。df['t']#中x的格式(x)这一个非常有效而且非常简单,如果您想向悬停添加更多数据,我将给您一些额外的示例:

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)
导入plotly.graph\u objs as go
从plotly.offline导入下载\u plotlyjs,初始化\u笔记本\u模式,绘图,iplot
初始笔记本模式(已连接=真)
作为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],
记号笔(
颜色=df[“分数”],
showscale=True,
colorbar=dict(title='score'))
),
mode='markers',
hoverinfo='Text t:'+df['t']#显示df中的't'+内容
+“
”+#介绍空间 'Text t2:'+df['t2']#为't'显示的文本 ) 布局=开始。布局( 场景=口述( xaxis=dict(title=xtitle), yaxis=dict(title=ytitle), zaxis=dict(title=ztitle) ) ) 图=开始图(数据=[trace1],布局=布局) plotly.offline.iplot(图)
重要提示:如果图形是带有轨迹的图形:应使用“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'
  
    )
fig=go.Figure(布局=布局)
图添加轨迹(go.Bar(
x=df[xtitle],
y=df[ytitle],
z=df[ztitle],
记号笔(
颜色=df[“分数”],
showscale=True,
colorbar=dict(title='score'))
),
mode='markers',
hovertext='Text t:'+df['t']#显示df中的't'内容
+“
”+#介绍空间 'Text t2:'+df['t2']#为't'显示的文本 )