仅在 Jupyter 中显示 1 个子组的 Plotly 动画条形图

问题:当我运行我的代码时,只显示一个状态(子组)。数据集很简单,创建日期、状态和计数。我只能认为此时我的数据集可能有问题。为什么它只显示我拥有的三个状态中的一个状态,或者它可能更适合托管文件?它似乎只是遍历列表,直到最后才保持每个数据点完好无损。另一个代码块在 github 上运行良好。


我的数据集示例:


Status,Create Date,Count

None,17-Apr-12,8

None,30-Apr-12,9

None,23-Aug-12,10

None,3-Oct-12,11

None,9-Jan-13,12

None,29-Jan-13,13

QBOS,31-Jan-13,1

QBDS,1-Feb-13,1

我的代码:


import numpy as np

import pandas as pd

import plotly.graph_objects as go

import plotly.express as px


df = pd.read_csv('qb7.csv')

df.columns = ['Status','Create Date','Count']

includes=['None','QBDS', 'QBOS']

df=df[df['Status'].isin(includes)]

df['Create Date']= pd.to_datetime(df['Create Date']).dt.strftime('%Y-%m-%d')

fig = px.bar(df,

         x="Status", 

         y="Count",

         color="Status",

         animation_frame="Create Date", hover_name="Status", 

         range_y=[0,8000])

fig.show()

``

http://img3.mukewang.com/6412d2b9000154fe06530334.jpg

我想制作的示例:


import numpy as np

import pandas as pd

import plotly.graph_objects as go

import plotly.express as px


df = pd.read_csv('https://raw.githubusercontent.com/shinokada/covid-19-stats/master/data/daily-new- 

confirmed-cases-of-covid-19-tests-per-case.csv')

df.columns = ['Country','Code','Date','Confirmed','Days since confirmed']

includes=['United States','Russia', 'India','Brazil']

df=df[df['Country'].isin(includes)]

df['Date']= pd.to_datetime(df['Date']).dt.strftime('%Y-%m-%d')

fig = px.bar(df, x="Country", y="Confirmed", color="Country",

animation_frame="Date", animation_group="Country", range_y=[0,35000])

fig.show()`

http://img2.mukewang.com/6412d2c80001bb2706550339.jpg

小怪兽爱吃肉
浏览 74回答 1
1回答

蓝山帝景

我认为它没有显示预期图表的原因是因为数据数量不同。当数据数量对齐时,可以达到预期的结果。import pandas as pdimport numpy as npimport iodata = '''Status,Create Date,CountNone,17-Apr-12,8None,30-Apr-12,9None,23-Aug-12,10None,3-Oct-12,11None,9-Jan-13,12None,29-Jan-13,13QBOS,17-Apr-12,8QBOS,30-Apr-12,9QBOS,23-Aug-12,10QBOS,3-Oct-12,11QBOS,9-Jan-13,12QBOS,29-Jan-13,13QBDS,17-Apr-12,8QBDS,30-Apr-12,9QBDS,23-Aug-12,10QBDS,3-Oct-12,11QBDS,9-Jan-13,12QBDS,29-Jan-13,13'''df = pd.read_csv(io.StringIO(data), sep=',')import plotly.graph_objects as goimport plotly.express as px# df = pd.read_csv('qb7.csv')df.columns = ['Status','Create Date','Count']includes=['None','QBDS', 'QBOS']df=df[df['Status'].isin(includes)]df['Create Date']= pd.to_datetime(df['Create Date']).dt.strftime('%Y-%m-%d')fig = px.bar(df,         x="Status",          y="Count",         color="Status",         animation_frame="Create Date", hover_name="Status",          range_y=[0,30])fig.show()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python