酷炫优雅的Python动态图工具~

今天给大家安利一波用Python做动态图的方法~

安装

pip install pandas_alive

#或者
conda install pandas_alive -c conda-forge

玩起来

支持数据数据格式如下,动态地图
结合geopandas,动态水平bar

import pandas as pd
import pandas_alive
import matplotlib.pyplot as plt

plt.style.use('ggplot')

#读入数据
elec_df = pd.read_csv("Aus_Elec_Gen_1980_2018.csv",
                      index_col=0,
                      parse_dates=[0],
                      thousands=',')

#定义求和def
def current_total(values):
    total = values.sum()
    s = f'Total : {int(total)}'
    return {'x': .85, 'y': .2, 's': s, 'ha': 'right', 'size': 11}


#缺省值0填充、绘图
elec_df.fillna(0).tail(n=10).plot_animated(
    'electricity-generated-australia.gif',  #保存gif名称
    period_fmt="%d/%m/%Y",  #动态更新图中时间戳
    title='Australian Electricity Sources 1980-2018',  #标题
    perpendicular_bar_func='mean',  #添加均值辅助线
    period_summary_func=current_total,  #汇总
    cmap='Set1',  #定义调色盘
    n_visible=5,  #柱子显示数
    orientation='h',#柱子方向
)

动态垂直bar动态折线

elec_df.diff().fillna(0).tail(n=10).plot_animated(filename='line-chart.gif',
                                                 kind='line',#指定折线模式
                                                 cmap='Set1',
                                                 period_label={
                                                     'x': 0.25,
                                                     'y': 0.9
                                                 },
                                                 line_width=1,
                                                 add_legend=True,
                                                 fill_under_line_color='#01a2d9')

动态累积bar

import pandas_alive
covid_df.sum(axis=1).fillna(0).tail(n=10).plot_animated(
    filename='sumbar-chart.gif',
    kind='bar',   #指定bar模式
    cmap='Set1',  #定义调色盘
    period_label={
        'x': 0.1,
        'y': 0.9
    },
    orientation='h',
    enable_progress_bar=True,
    steps_per_period=2,
    interpolate_period=True,
    period_length=200)

动态散点图

import pandas as pd
import pandas_alive

#max散点数据
max_temp_df = pd.read_csv(
    "Newcastle_Australia_Max_Temps.csv",
    parse_dates={"Timestamp": ["Year", "Month", "Day"]},
)

#min散点数据
min_temp_df = pd.read_csv(
    "Newcastle_Australia_Min_Temps.csv",
    parse_dates={"Timestamp": ["Year", "Month", "Day"]},
)

#按时间戳merge max/min数据
merged_temp_df = pd.merge_asof(max_temp_df, min_temp_df, on="Timestamp")

merged_temp_df.index = pd.to_datetime(
    merged_temp_df["Timestamp"].dt.strftime('%Y/%m/%d'))

keep_columns = [
    "Minimum temperature (Degree C)", "Maximum temperature (Degree C)"
]

merged_temp_df.head(n=5000)[keep_columns].resample("Y").mean().plot_animated(
    filename='scatter-chart.gif',
    cmap='Set1', 
    kind="scatter",#指定散点模式
    size=10,
    title='Max & Min Temperature Newcastle, Australia')

动态气泡图

import pandas_alive

multi_index_df = pd.read_csv("multi.csv", header=[0, 1], index_col=0)

multi_index_df.index = pd.to_datetime(multi_index_df.index, dayfirst=True)

map_chart = multi_index_df.tail(n=40).plot_animated(
    kind="bubble",  #指定气泡模式
    filename="bubble-chart.gif",
    x_data_label="Longitude",
    y_data_label="Latitude",
    size_data_label="Cases",
    color_data_label="Cases",
    vmax=5,
    steps_per_period=1,
    interpolate_period=True,
    period_length=500,
    dpi=150)

多子图一起动
这部分可以结合matplotlib的多子图绘制,实现各种个性化动图,核心代码如下,

更多个性化设置

https://github.com/JackMcKew/pandas_alive

文中数据获取

https://github.com/JackMcKew/pandas_alive/tree/main/data

本文数据源和更多详细操作都可以在上面的github找到~

以上。

 
●Graveyard分析模型是真的牛X!●品牌知名度分析实例
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Python创建3D酷炫烟花效果,可以利用如`pyOpenGL`库这样的图形渲染工具。下面是一个简化的示例程序框架,展示如何基于OpenGL在3D空间内绘制烟花的效果。 首先,确保安装了必要的库: ```bash pip install pyopengl ``` 下面是一个简单的例子,展示了如何创建一个基本的3D烟花效果: ### 示例代码 ```python import numpy as np from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * # 初始化函数 def init(): glClearColor(0.0, 0.0, 0.0, 0.0) gluPerspective(45.0, 800 / 600, 0.1, 100.0) glEnable(GL_DEPTH_TEST) # 绘制烟花的核心函数 def draw_firework(positions): for pos in positions: glBegin(GL_QUAD_STRIP) for angle in np.linspace(-np.pi, np.pi, num=100): x = np.cos(angle) * pos y = np.sin(angle) * pos z = pos glVertex3f(x, y, z) glVertex3f(x + 0.01, y + 0.01, z) glEnd() glColor3f(np.random.uniform(0, 1), np.random.uniform(0, 1), np.random.uniform(0, 1)) # 主循环更新函数 def display(): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # 观察点设置,这里是相机的位置、目标位置以及视口的方向向量 gluLookAt(1.5, 1.5, 1.5, 0, 0, 0, 0, 1, 0) draw_firework([(pos_x, pos_y, 0) for pos_x in np.linspace(-2, 2, 10)] + [(pos_x, -2, 0) for pos_x in np.linspace(-2, 2, 10)] + [(pos_x, 2, 0) for pos_x in np.linspace(-2, 2, 10)]) glutSwapBuffers() # 设置显示模式并启动主循环 glutInit(sys.argv) glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) glutCreateWindow("3D Fireworks") init() glutDisplayFunc(display) glutIdleFunc(display) glutMainLoop() ``` ### 相关问题: 1. 这段代码是如何实现烟花效果的? 2. 使用`pyOpenGL`和类似库进行三维动画渲染需要注意哪些关键步骤? 3. 针对特定应用场景,如何调整和优化这个烟花特效程序? 注意:上述代码只是一个基础示范,实际应用中可能会需要更多的交互性、更复杂的粒子系统、动态颜色变化以及碰撞检测等复杂功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值