怎么使用python的可视化工具Pandas_Alive
发表于:2026-11-04 作者:千家信息网编辑
千家信息网最后更新 2026年11月04日,这篇文章主要介绍"怎么使用python的可视化工具Pandas_Alive",在日常操作中,相信很多人在怎么使用python的可视化工具Pandas_Alive问题上存在疑惑,小编查阅了各式资料,整理
千家信息网最后更新 2026年11月04日怎么使用python的可视化工具Pandas_Alive
这篇文章主要介绍"怎么使用python的可视化工具Pandas_Alive",在日常操作中,相信很多人在怎么使用python的可视化工具Pandas_Alive问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"怎么使用python的可视化工具Pandas_Alive"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
数据动画可视化制作在日常工作中是非常实用的一项技能。目前支持动画可视化的库主要以Matplotlib-Animation为主,其特点为:配置复杂,保存动图容易报错。
安装方法
pip install pandas_alive # 或者conda install pandas_alive -c conda-forge
使用说明
pandas_alive 的设计灵感来自 bar_chart_race,为方便快速进行动画可视化制作,在数据的格式上需要满足如下条件:
每行表示单个时间段
每列包含特定类别的值
索引包含时间组件(可选)
支持示例展示
水平条形图
import pandas_alivecovid_df = pandas_alive.load_dataset()covid_df.plot_animated(filename='examples/perpendicular-example.gif',perpendicular_bar_func='mean')
垂直条形图比赛
import pandas_alivecovid_df = pandas_alive.load_dataset()covid_df.plot_animated(filename='examples/example-barv-chart.gif',orientation='v')
条形图
与时间与 x 轴一起显示的折线图类似
import pandas_alivecovid_df = pandas_alive.load_dataset()covid_df.sum(axis=1).fillna(0).plot_animated(filename='examples/example-bar-chart.gif',kind='bar', period_label={'x':0.1,'y':0.9}, enable_progress_bar=True, steps_per_period=2, interpolate_period=True, period_length=200)饼图
import pandas_alivecovid_df = pandas_alive.load_dataset()covid_df.plot_animated(filename='examples/example-pie-chart.gif',kind="pie",rotatelabels=True,period_label={'x':0,'y':0})多边形地理空间图
import geopandasimport pandas_aliveimport contextilygdf = geopandas.read_file('data/italy-covid-region.gpkg')gdf.index = gdf.regiongdf = gdf.drop('region',axis=1)map_chart = gdf.plot_animated(filename='examples/example-geo-polygon-chart.gif',basemap_format={'source':contextily.providers.Stamen.Terrain})多个图表
pandas_alive 支持单个可视化中的多个动画图表。
示例1
import pandas_aliveurban_df = pandas_alive.load_dataset("urban_pop")animated_line_chart = ( urban_df.sum(axis=1) .pct_change() .fillna(method='bfill') .mul(100) .plot_animated(kind="line", title="Total % Change in Population",period_label=False,add_legend=False))animated_bar_chart = urban_df.plot_animated(n_visible=10,title='Top 10 Populous Countries',period_fmt="%Y")pandas_alive.animate_multiple_plots('examples/example-bar-and-line-urban-chart.gif',[animated_bar_chart,animated_line_chart], title='Urban Population 1977 - 2018', adjust_subplot_top=0.85, enable_progress_bar=True)示例2
import pandas_alivecovid_df = pandas_alive.load_dataset()animated_line_chart = covid_df.diff().fillna(0).plot_animated(kind='line',period_label=False,add_legend=False)animated_bar_chart = covid_df.plot_animated(n_visible=10)pandas_alive.animate_multiple_plots('examples/example-bar-and-line-chart.gif',[animated_bar_chart,animated_line_chart], enable_progress_bar=True)示例3
import pandas_aliveimport pandas as pddata_raw = pd.read_csv( "https://raw.githubusercontent.com/owid/owid-datasets/master/datasets/Long%20run%20life%20expectancy%20-%20Gapminder%2C%20UN/Long%20run%20life%20expectancy%20-%20Gapminder%2C%20UN.csv")list_G7 = [ "Canada", "France", "Germany", "Italy", "Japan", "United Kingdom", "United States",]data_raw = data_raw.pivot( index="Year", columns="Entity", values="Life expectancy (Gapminder, UN)")data = pd.DataFrame()data["Year"] = data_raw.reset_index()["Year"]for country in list_G7: data[country] = data_raw[country].valuesdata = data.fillna(method="pad")data = data.fillna(0)data = data.set_index("Year").loc[1900:].reset_index()data["Year"] = pd.to_datetime(data.reset_index()["Year"].astype(str))data = data.set_index("Year")animated_bar_chart = data.plot_animated( period_fmt="%Y",perpendicular_bar_func="mean", period_length=200,fixed_max=True)animated_line_chart = data.plot_animated( kind="line", period_fmt="%Y", period_length=200,fixed_max=True)pandas_alive.animate_multiple_plots( "examples/life-expectancy.gif", plots=[animated_bar_chart, animated_line_chart], title="Life expectancy in G7 countries up to 2015", adjust_subplot_left=0.2, adjust_subplot_top=0.9, enable_progress_bar=True)示例4
import geopandasimport pandas as pdimport pandas_aliveimport contextilyimport matplotlib.pyplot as pltimport urllib.request, jsonwith urllib.request.urlopen( "https://data.nsw.gov.au/data/api/3/action/package_show?id=aefcde60-3b0c-4bc0-9af1-6fe652944ec2") as url: data = json.loads(url.read().decode())# Extract url to csv componentcovid_nsw_data_url = data["result"]["resources"][0]["url"]# Read csv from data API urlnsw_covid = pd.read_csv(covid_nsw_data_url)postcode_dataset = pd.read_csv("data/postcode-data.csv")# Prepare data from NSW health datasetnsw_covid = nsw_covid.fillna(9999)nsw_covid["postcode"] = nsw_covid["postcode"].astype(int)grouped_df = nsw_covid.groupby(["notification_date", "postcode"]).size()grouped_df = pd.DataFrame(grouped_df).unstack()grouped_df.columns = grouped_df.columns.droplevel().astype(str)grouped_df = grouped_df.fillna(0)grouped_df.index = pd.to_datetime(grouped_df.index)cases_df = grouped_df# Clean data in postcode dataset prior to matchinggrouped_df = grouped_df.Tpostcode_dataset = postcode_dataset[postcode_dataset['Longitude'].notna()]postcode_dataset = postcode_dataset[postcode_dataset['Longitude'] != 0]postcode_dataset = postcode_dataset[postcode_dataset['Latitude'].notna()]postcode_dataset = postcode_dataset[postcode_dataset['Latitude'] != 0]postcode_dataset['Postcode'] = postcode_dataset['Postcode'].astype(str)# Build GeoDataFrame from Lat Long dataset and make map chartgrouped_df['Longitude'] = grouped_df.index.map(postcode_dataset.set_index('Postcode')['Longitude'].to_dict())grouped_df['Latitude'] = grouped_df.index.map(postcode_dataset.set_index('Postcode')['Latitude'].to_dict())gdf = geopandas.GeoDataFrame( grouped_df, geometry=geopandas.points_from_xy(grouped_df.Longitude, grouped_df.Latitude),crs="EPSG:4326")gdf = gdf.dropna()# Prepare GeoDataFrame for writing to geopackagegdf = gdf.drop(['Longitude','Latitude'],axis=1)gdf.columns = gdf.columns.astype(str)gdf['postcode'] = gdf.indexgdf.to_file("data/nsw-covid19-cases-by-postcode.gpkg", layer='nsw-postcode-covid', driver="GPKG")# Prepare GeoDataFrame for plottinggdf.index = gdf.postcodegdf = gdf.drop('postcode',axis=1)gdf = gdf.to_crs("EPSG:3857") #Web Mercatormap_chart = gdf.plot_animated(basemap_format={'source':contextily.providers.Stamen.Terrain},cmap='cool')cases_df.to_csv('data/nsw-covid-cases-by-postcode.csv')from datetime import datetimebar_chart = cases_df.sum(axis=1).plot_animated( kind='line', label_events={ 'Ruby Princess Disembark':datetime.strptime("19/03/2020", "%d/%m/%Y"), 'Lockdown':datetime.strptime("31/03/2020", "%d/%m/%Y") }, fill_under_line_color="blue", add_legend=False)map_chart.ax.set_title('Cases by Location')grouped_df = pd.read_csv('data/nsw-covid-cases-by-postcode.csv', index_col=0, parse_dates=[0])line_chart = ( grouped_df.sum(axis=1) .cumsum() .fillna(0) .plot_animated(kind="line", period_label=False, title="Cumulative Total Cases", add_legend=False))def current_total(values): total = values.sum() s = f'Total : {int(total)}' return {'x': .85, 'y': .2, 's': s, 'ha': 'right', 'size': 11}race_chart = grouped_df.cumsum().plot_animated( n_visible=5, title="Cases by Postcode", period_label=False,period_summary_func=current_total)import timetimestr = time.strftime("%d/%m/%Y")plots = [bar_chart, line_chart, map_chart, race_chart]from matplotlib import rcParamsrcParams.update({"figure.autolayout": False})# make sure figures are `Figure()` instancesfigs = plt.Figure()gs = figs.add_gridspec(2, 3, hspace=0.5)f3_ax1 = figs.add_subplot(gs[0, :])f3_ax1.set_title(bar_chart.title)bar_chart.ax = f3_ax1f3_ax2 = figs.add_subplot(gs[1, 0])f3_ax2.set_title(line_chart.title)line_chart.ax = f3_ax2f3_ax3 = figs.add_subplot(gs[1, 1])f3_ax3.set_title(map_chart.title)map_chart.ax = f3_ax3f3_ax4 = figs.add_subplot(gs[1, 2])f3_ax4.set_title(race_chart.title)race_chart.ax = f3_ax4timestr = cases_df.index.max().strftime("%d/%m/%Y")figs.suptitle(f"NSW COVID-19 Confirmed Cases up to {timestr}")pandas_alive.animate_multiple_plots( 'examples/nsw-covid.gif', plots, figs, enable_progress_bar=True)到此,关于"怎么使用python的可视化工具Pandas_Alive"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
可视化
示例
工具
动画
学习
时间
条形
支持
实用
单个
图表
多个
数据
方法
更多
制作
帮助
复杂
接下来
使用说明
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网金融监管科技数据
互联网科技公司税收优惠
中资互联网科技
互联网络科技有限公司
华宇互联网科技
广西互联网科技公司
长亮科技互联网金融
居家互联网科技有限公司
互联网科技素材视频竖屏
海南购全球互联网科技
阿里互联网科技公司
湖南一路畅行互联网科技
畅思互联网科技
互联网科技创新的路径
南通创西澳互联网科技有限公司
互联网与数字科技关系
郑州欣桑互联网科技有限公司
互联网公司属于科技吗
上海万顺科技互联网有限公司招聘
北京天晋互联网科技有限公司
科技互联网企业
科技与互联网发展
互联网科技金融公司
友谊互联网络科技
长亮科技互联网金融
如何创新互联网科技
中国互联网公司排名锤子科技
国大互联网教育科技有限公司薪资
电子商务与互联网科技的关系
互联网企业和网络科技有限公司