简体   繁体   English

在 plotly python 中的 3d 表面 plot 上设置自定义工具提示

[英]Setting custom tooltip on 3d surface plot in plotly python

I've been trying for a while to set custom tooltips on a 3d surface plot, but cannot figure it out.我一直在尝试在 3d 表面 plot 上设置自定义工具提示,但无法弄清楚。 I can do something very simple, like make the tooltip the same for each point, but I'm having trouble putting different values for each point in the tooltip, when the fields aren't being graphed.我可以做一些非常简单的事情,比如使每个点的工具提示相同,但是当没有绘制字段时,我无法在工具提示中为每个点放置不同的值。

In my example, I have a dataset of 53 rows (weeks) and 7 columns (days of the week) that I'm graphing on a 3d surface plot, by passing the dataframe in the Z parameter.在我的示例中,我在 3d 表面 plot 上绘制了一个包含 53 行(周)和 7 列(一周中的天)的数据集,方法是在 Z6A8064B5DF479450550DZ5534 中传递 Z6A8064B5DF479450550D5534 参数。 It's a year's worth of data, so each day has its own numeric value that's being graphed.这是一年的数据,所以每一天都有自己的数字值被绘制出来。 I'm trying to label each point with the actual date (hence the custom tooltip, since I'm not passing the date itself to the graph), but cannot seem to align the tooltip values correctly.我正在尝试 label 每个点与实际日期(因此自定义工具提示,因为我没有将日期本身传递给图表),但似乎无法正确对齐工具提示值。

I tried a simple example to create a "tooltip array" of the same shape as the dataframe, but when I test whether I'm getting the shape right, by using a repeated word, I get an even weirder error where it uses the character values in the word as tooltips (eg, c or _).我尝试了一个简单的示例来创建一个与 dataframe 形状相同的“工具提示数组”,但是当我测试我是否得到正确的形状时,通过使用重复的单词,我得到了一个更奇怪的错误,它使用了字符单词中的值作为工具提示(例如,c 或 _)。 Does anyone have any thoughts or suggestions?有没有人有任何想法或建议? I can post more code, but tried to replicate my error with a simpler example.我可以发布更多代码,但尝试用一个更简单的示例来复制我的错误。

在此处输入图像描述

 labels=np.array([['test_label']*7]*53) fig = go.Figure(data=[ go.Surface(z=Z, text=labels, hoverinfo='text' )],) fig.show()

工具提示正在将标签分解为字符

We have created sample data similar to the data provided in the image.我们创建了类似于图像中提供的数据的示例数据。 I created a data frame with randomly generated values for the dates of one consecutive year, added the week number and day number, and formed it into Z data.我为连续一年的日期创建了一个随机生成值的数据框,添加了周数和天数,并将其形成为 Z 数据。 I have also added a date only data column.我还添加了一个仅限日期的数据列。 So your code will make the hover text display the date.因此,您的代码将使 hover 文本显示日期。

import numpy as np
import plotly.graph_objects as go
import pandas as pd

df = pd.DataFrame({'date':pd.to_datetime(pd.date_range('2021-01-01','2021-12-31',freq='1d')),'value':np.random.rand(365)})

df['day_of_week'] = df['date'].dt.weekday
df['week'] = df['date'].dt.isocalendar().week
df['date2'] = df['date'].dt.date
Z = df[['week','day_of_week','value']].pivot(index='week', columns='day_of_week')
labels = df[['week','day_of_week','date2']].pivot(index='week', columns='day_of_week').fillna('')

fig = go.Figure(data=[
    go.Surface(z=Z,
               text=labels,
               hoverinfo='text'
              )]
               )
fig.update_layout(autosize=False, width=800, height=600)
fig.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM