如何在 Python 中制作赛车条形图可视化
How to make a racing Bar Chart Visualization in Python
如何动画条形图竞赛Python:如何使条形图自动改变其位置。例如,在下面的代码示例中,对于像美国这样具有更多值的国家/地区,条形应该逐渐向上移动。
import plotly.express as px
import pandas as pd
import plotly.graph_objects as go
import numpy as np
url='https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv'
def read_file(url):
df = pd.read_csv(url)
return df
def filter_specific_country(df, selected_countries):
df1=df[df['Country/Region'].isin(selected_countries) ]
countrywise_grouped_df = df1.groupby(df['Country/Region']).sum().drop(['Lat','Long'], axis=1)
countrywise_grouped_df
return countrywise_grouped_df
def transpose_and_reformat_data(df):
df_t=df.transpose()
df_t.reset_index(inplace=True)
df_t.rename(columns={'Country/Region':'Index_Col', 'index':'Dates'}, inplace=True)
return df_t
confirmed_dataset = read_file(url)
selected_countries=['India','China','Italy','Spain','France','Australia','Germany','Japan','Korea, South','Pakistan',
'Russia','United Kingdom','Canada','Iran','Brazil','Singapore','South Africa','US']
ds=filter_specific_country(confirmed_dataset,selected_countries)
data=transpose_and_reformat_data(ds).melt(id_vars=["Dates"], var_name="Country", value_name="Confirmed_Count")
#plot_title="Global Spread of Covid-19 : (Selected Countries)"
plot_title='Visualizing the spread of Novel Coronavirus COVID-19 (2019-nCoV) - Created by Dibyendu Banerjee'
fig = px.bar(data, y="Country", x="Confirmed_Count", color="Country",
animation_frame="Dates", range_x=[1,14000000], orientation='h' )
fig.update_layout(title=plot_title,yaxis_title='Countries', xaxis_tickangle=90, font=dict(family="Arial",size=10,color="#7f7f7f"))
fig.show()
据我所知,使用 potly 进行条形图跟踪是不可行的。已经有一个专门的 library 可以用来回答你的问题。由于数据是日级别的,回放时间会比较长,所以我会需要重新抽样或者汇总成年
from raceplotly.plots import barplot
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as np
url='https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv'
def read_file(url):
df = pd.read_csv(url)
return df
def filter_specific_country(df, selected_countries):
df1=df[df['Country/Region'].isin(selected_countries) ]
countrywise_grouped_df = df1.groupby(df['Country/Region']).sum().drop(['Lat','Long'], axis=1)
countrywise_grouped_df
return countrywise_grouped_df
def transpose_and_reformat_data(df):
df_t=df.transpose()
df_t.reset_index(inplace=True)
df_t.rename(columns={'Country/Region':'Index_Col', 'index':'Dates'}, inplace=True)
return df_t
confirmed_dataset = read_file(url)
selected_countries=['India','China','Italy','Spain','France','Australia','Germany','Japan','Korea, South','Pakistan',
'Russia','United Kingdom','Canada','Iran','Brazil','Singapore','South Africa','US']
ds=filter_specific_country(confirmed_dataset,selected_countries)
data=transpose_and_reformat_data(ds).melt(id_vars=["Dates"], var_name="Country", value_name="Confirmed_Count")
covid_race = barplot(data, item_column='Country', value_column='Confirmed_Count',time_column='Dates')
covid_race.plot(item_label='Top 10 crops', value_label = 'Covid Confirmed Count', date_format='%Y/%m/%d', frame_duration=800)
如何动画条形图竞赛Python:如何使条形图自动改变其位置。例如,在下面的代码示例中,对于像美国这样具有更多值的国家/地区,条形应该逐渐向上移动。
import plotly.express as px
import pandas as pd
import plotly.graph_objects as go
import numpy as np
url='https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv'
def read_file(url):
df = pd.read_csv(url)
return df
def filter_specific_country(df, selected_countries):
df1=df[df['Country/Region'].isin(selected_countries) ]
countrywise_grouped_df = df1.groupby(df['Country/Region']).sum().drop(['Lat','Long'], axis=1)
countrywise_grouped_df
return countrywise_grouped_df
def transpose_and_reformat_data(df):
df_t=df.transpose()
df_t.reset_index(inplace=True)
df_t.rename(columns={'Country/Region':'Index_Col', 'index':'Dates'}, inplace=True)
return df_t
confirmed_dataset = read_file(url)
selected_countries=['India','China','Italy','Spain','France','Australia','Germany','Japan','Korea, South','Pakistan',
'Russia','United Kingdom','Canada','Iran','Brazil','Singapore','South Africa','US']
ds=filter_specific_country(confirmed_dataset,selected_countries)
data=transpose_and_reformat_data(ds).melt(id_vars=["Dates"], var_name="Country", value_name="Confirmed_Count")
#plot_title="Global Spread of Covid-19 : (Selected Countries)"
plot_title='Visualizing the spread of Novel Coronavirus COVID-19 (2019-nCoV) - Created by Dibyendu Banerjee'
fig = px.bar(data, y="Country", x="Confirmed_Count", color="Country",
animation_frame="Dates", range_x=[1,14000000], orientation='h' )
fig.update_layout(title=plot_title,yaxis_title='Countries', xaxis_tickangle=90, font=dict(family="Arial",size=10,color="#7f7f7f"))
fig.show()
据我所知,使用 potly 进行条形图跟踪是不可行的。已经有一个专门的 library 可以用来回答你的问题。由于数据是日级别的,回放时间会比较长,所以我会需要重新抽样或者汇总成年
from raceplotly.plots import barplot
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as np
url='https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv'
def read_file(url):
df = pd.read_csv(url)
return df
def filter_specific_country(df, selected_countries):
df1=df[df['Country/Region'].isin(selected_countries) ]
countrywise_grouped_df = df1.groupby(df['Country/Region']).sum().drop(['Lat','Long'], axis=1)
countrywise_grouped_df
return countrywise_grouped_df
def transpose_and_reformat_data(df):
df_t=df.transpose()
df_t.reset_index(inplace=True)
df_t.rename(columns={'Country/Region':'Index_Col', 'index':'Dates'}, inplace=True)
return df_t
confirmed_dataset = read_file(url)
selected_countries=['India','China','Italy','Spain','France','Australia','Germany','Japan','Korea, South','Pakistan',
'Russia','United Kingdom','Canada','Iran','Brazil','Singapore','South Africa','US']
ds=filter_specific_country(confirmed_dataset,selected_countries)
data=transpose_and_reformat_data(ds).melt(id_vars=["Dates"], var_name="Country", value_name="Confirmed_Count")
covid_race = barplot(data, item_column='Country', value_column='Confirmed_Count',time_column='Dates')
covid_race.plot(item_label='Top 10 crops', value_label = 'Covid Confirmed Count', date_format='%Y/%m/%d', frame_duration=800)