python - 阴谋地徘徊

标签 python python-3.x 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 值。

enter image description here

在数据框 df 中,我还有一个名为 t 的列,我希望当我将鼠标悬停在一个点上时,它会显示 x、y、z、t并得分。

我该怎么做?

最佳答案

您可以在跟踪中使用 texthoverinfo

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 属性、hoverinfohovertextHere is the documentation .

enter image description here

关于python - 阴谋地徘徊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51264528/

相关文章:

python - 在pycharm中安装包 "cufflinks"时出现错误

python - Cherrypy 与 Mysql 错误 : "tuple indices must be integers, not str" when trying to add users

python - 当两个异步任务访问同一个可等待对象时是否安全?

r - 我可以放大一个图并让其他图也以同样的方式缩放吗? (ggplot 和plotly)

plotly - Graph 的工具提示未正确显示

r - 将图像悬停在 Shiny 应用程序中的plotly r 图表中

python - 使用透明标记但不透明边缘绘图

python - 嵌套正则表达式

python - 如何使用下面两个单元格的值更新数据帧单元格的值?

python - 无需任何显式调用即可更改变量值