简体   繁体   中英

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. 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. 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.

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 _). 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. I have also added a date only data column. So your code will make the hover text display the date.

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()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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