有没有办法在Plotly中修复我的日期滑块,以便它为每天生成动画帧?

2024-10-03 23:27:47 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是我迄今为止的数据和可视化:

import pandas as pd 
import plotly.express as px
  
# intialise data of lists. 
data = {'Subreddit':["90DayFiance","Anarchism","Animemes","AskALiberal",
                "AskALiberal","AskReddit","AskReddit","AskReddit","AskReddit","AskTeenGirls","CatastrophicFailure",
                "Conservative","Deji","DhGateReps","DontFundMe","Drama","Drizzy","DuggarsSnark",
                "Eminem","Enough_Sanders_Spam","Enough_Sanders_Spam","Enough_Sanders_Spam","Enough_Sanders_Spam",
                "FixedPoliticalMemes"], 
        'Date':["2020-07-08 00:00:00-04:00","2020-07-08 00:00:00-04:00","2020-07-08 00:00:00-04:00",
               "2020-07-09 00:00:00-04:00","2020-07-09 00:00:00-04:00","2020-07-09 00:00:00-04:00",
               "2020-07-09 00:00:00-04:00","2020-07-04 00:00:00-04:00","2020-07-04 00:00:00-04:00",
               "2020-07-04 00:00:00-04:00","2020-07-04 00:00:00-04:00","2020-07-10 00:00:00-04:00",
               "2020-07-10 00:00:00-04:00","2020-07-10 00:00:00-04:00","2020-07-10 00:00:00-04:00",
               "2020-07-10 00:00:00-04:00","2020-07-10 00:00:00-04:00","2020-07-18 00:00:00-04:00",
               "2020-07-18 00:00:00-04:00","2020-07-18 00:00:00-04:00","2020-07-09 00:00:00-04:00",
               "2020-07-09 00:00:00-04:00","2020-07-08 00:00:00-04:00","2020-07-10 00:00:00-04:00"],
       'Count':["3","2","2","2","2","3","5","6","12","7","2","7","4","3","5","3","5","15","5","4","2","3","5",'5'],
       'Day':["08", "08", "08", "09", "09", "09", "09", "04", "04", "04", "04", "10", "10", "10", "10",
              "10", "10", "18", "18", "18", "09", "09", "08", "10"]} 
  
# Create DataFrame 
df = pd.DataFrame(data) 



fig = px.bar(df,
             x="Subreddit",
             y="Count",
             animation_frame="Day",
             animation_group="Subreddit", 
             range_y=[0, 50])

fig.show()

有没有办法使滑块响应日期?到目前为止,我只是把这一天作为虚拟的一天来代表这一天,但显然我更愿意在时间上从早一点滑到晚一点,并显示每天每个子Reddit的频率


Tags: importdataframedfdataascountspampd
1条回答
网友
1楼 · 发布于 2024-10-03 23:27:47

我认为这是因为时间序列数据没有排序。 如果您想知道,我将日期列转换为日期格式,然后在排序之前使用仅日期信息对其进行更新

df['Date'] = pd.to_datetime(df['Date'])
df['Day'] = df['Date'].dt.day
df.sort_values('Day', ascending=True, ignore_index=True, inplace=True)

import plotly.express as px
  
fig = px.bar(df,
             x="Subreddit",
             y="Count",
             animation_frame="Day",
             animation_group="Subreddit", 
             range_y=[0, 50])

fig.show()

enter image description here

相关问题 更多 >