Python绘制动态图形


日常生活中,我们经常需要对多年的数据进行分析。如果对每一年的数据都进行可视化,那么将需要我们重复的采用相同的代码对数据进行可视化。在年份较多的情况下,这无疑增加了我们的工作量,且不能很好地帮助我们发现随着时间的不同数据的变化情况。基于此,本文将众多年份合并在一张图上,用动态变化图形对数据进行可视化。本文主要阐述了几种基本图形的动态可视化。

一、柱形图
import plotly.express as px
from vega_datasets import data
# d读取数据
df = data.disasters()
df = df[df.Year > 1990]
#  画图
fig = px.bar(df,
             y="Entity",
             x="Deaths",
             animation_frame="Year", 
             orientation='h',  #  水平方向
             range_x=[0, df.Deaths.max()],  # 变动的范围
             # 柱子的颜色
             color="Entity" )
# 图形大小,背景等设置
fig.update_layout(width=800, height=500,
                  xaxis_showgrid=False,
                  yaxis_showgrid=False,  
                  paper_bgcolor='rgba(0,0,0,0)', # 图片背景颜色
                  plot_bgcolor='rgba(0,0,0,0)',  # 柱形图部分的背景颜色
                  title_text='Evolution of Natural Disasters',   # 标题
                  showlegend=False)
fig.update_xaxes(title_text='Number of Deaths')  # x 轴标题
fig.update_yaxes(title_text='')   # y 轴标题
fig.show()

在这里插入图片描述
小编还不会保存gif图形,这里将就看一下啦!

二、气泡图
import plotly.express as px
# 读取数据
df = px.data.gapminder()
fig = px.scatter(df, x="gdpPercap", y="lifeExp", animation_frame="year", size="pop",
                 color="continent", hover_name="country", log_x=True, size_max=55,
                  range_x=[100, 100000], range_y=[25, 90])
fig.update_layout(width=800,
                  height=500,
                  xaxis_showgrid=False,
                  yaxis_showgrid=False,
                  paper_bgcolor='rgba(0,0,0,0)',
                  plot_bgcolor='rgba(0,0,0,0)')

在这里插入图片描述

三、折线图
fig = px.line( df, x="gdpPercap", y="lifeExp", line_dash=None, animation_frame='year',  color="continent",
                 hover_name="country",log_x=True, range_x=[100, 100000], range_y=[25, 90])
    
fig.update_layout(width=800,
                  height=500,
                  xaxis_showgrid=False,
                  yaxis_showgrid=False,
                  paper_bgcolor='rgba(0,0,0,0)',
                  plot_bgcolor='rgba(0,0,0,0)')    
fig.show()

在这里插入图片描述

四、小提琴图
fig = px.violin( df, x="continent", y="pop", animation_frame='year', color="continent")
    
fig.update_layout(width=800, height=500, xaxis_showgrid=False, yaxis_showgrid=False,
                  paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)')    
    

在这里插入图片描述

五、面积图
fig = px.area( df, x="continent", y="lifeExp", animation_frame='year', color="continent")
    
fig.update_layout(width=800, height=500, xaxis_showgrid=False, yaxis_showgrid=False,
                  paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)')       

在这里插入图片描述

六、漏斗图
fig = px.funnel( df, x="continent", y="lifeExp", animation_frame='year', color="continent")
    
fig.update_layout(width=800, height=500, xaxis_showgrid=False, yaxis_showgrid=False,
                  paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)')    

在这里插入图片描述

七、带状图
fig = px.strip( df, x="continent", y="pop", animation_frame='year', color="continent", stripmode='overlay')
    
fig.update_layout(width=800, height=500, xaxis_showgrid=False, yaxis_showgrid=False,
                  paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)')    

在这里插入图片描述

  • 10
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值