亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Plotly:使用 plotly express 行時如何在 hoverinfo 中格式化日期?

Plotly:使用 plotly express 行時如何在 hoverinfo 中格式化日期?

慕田峪4524236 2022-10-25 15:33:46
我正在使用以下代碼使用 plotly express 線顯示時間序列數據。fig = px.line(df, x="date", y="close", color="type" ,category_orders = co ,color_discrete_sequence = colors,              line_group="type", title = company)fig.update_layout(height=500, width=1500)#hovermode="x unified"fig.show()但是在懸停時的繪圖中,它以以下格式顯示日期:“月,年”,即它不顯示日期。但我希望日期以以下格式顯示:“月日,年”。
查看完整描述

1 回答

?
慕哥6287543

TA貢獻1831條經驗 獲得超10個贊

text您可以通過和的正確組合來做到這一點hovertemplate:


for ser in fig['data']:

    ser['text']=list(set([d.strftime('%Y-%m-%d') for d in df['dates']]))

    ser['hovertemplate']='category=open<br>dates=%{text}<br>price=%{y}<extra></extra>'

fig.show()

ser['text'] 之所以如此混亂,是因為結果圖在 x 軸上顯示了唯一的日期。而且,由于plotly.express適用于 tidy 或long 而不是 wide data,因此數據集中包含您的日期的列很可能沒有唯一的日期值。


這是一個基于一些具有不同類別的金融時間序列數據的示例,這是一個完美的案例px.line:

http://img1.sycdn.imooc.com//6357917800018da910710555.jpg

帶有示例數據的完整代碼:


# imports

import pandas as pd

import plotly.graph_objects as go

from datetime import datetime

import plotly.express as px


# data

open_data = [33.0, 33.3, 33.5, 33.0, 34.1]

high_data = [33.1, 33.3, 33.6, 33.2, 34.8]

low_data = [32.7, 32.7, 32.8, 32.6, 32.8]

close_data = [33.0, 32.9, 33.3, 33.1, 33.1]

dates = [datetime(year=2020, month=10, day=10),

         datetime(year=2020, month=10, day=11),

         datetime(year=2020, month=10, day=12),

         datetime(year=2020, month=10, day=13),

         datetime(year=2020, month=10, day=14)]


# data organized in a pandas dataframe

df=pd.DataFrame(dict(open=open_data,

                    high=high_data,

                    low=low_data,

                    close=close_data,

                    dates=dates))


# transform the data from wide to long

df = pd.melt(df, id_vars=['dates'], value_vars=df.columns[:-1],

         var_name='category', value_name = 'price')


# setup for a perfect plotly time series figure

fig = px.line(df, x="dates", y="price", title='Prices', color = 'category')


# edit text and hovertemplate

for ser in fig['data']:

    ser['text']=list(set([d.strftime('%Y-%m-%d') for d in df['dates']]))

    ser['hovertemplate']='category=open<br>dates=%{text}<br>price=%{y}<extra></extra>'


fig.show()


查看完整回答
反對 回復 2022-10-25
  • 1 回答
  • 0 關注
  • 263 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號