简体   繁体   English

如何在 Python 中制作赛车条形图可视化

[英]How to make a racing Bar Chart Visualization in Python

how Animated Bar Chart Race Python: How to make a bar change its position automatically.如何动画条形图竞赛 Python:如何使条形图自动更改其 position。 For example, in the below code example while for countries like USA having more values, the bar should gradually move up.例如,在下面的代码示例中,对于像美国这样具有更多值的国家/地区,条形图应逐渐向上移动。

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

As far as I know, bar chart tracing using potly is not feasible.据我所知,使用 potly 进行条形图跟踪是不可行的。 There is already a dedicated library that I will use to answer your question.已经有一个专门的图书馆,我将用它来回答你的问题。 Since the data is at the daily level, it will take a long time to play back, so I will need to resample or summarize the data into years.由于数据是日级别的,回放时间会比较长,所以我需要重新抽样或者总结成年。

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)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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