python之画动态图 gif效果图

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


# set up matplotlib
is_ipython = 'inline' in matplotlib.get_backend()
if is_ipython:
    from IPython import display

plt.ion()

def find_csv_files(directory):
    csv_files = []  # 用于存储找到的 CSV 文件路径

    # 遍历指定路径下的所有文件和子目录
    for root, dirs, files in os.walk(directory):
        for file in files:
            # 检查文件是否以 ".csv" 结尾
            if file.endswith(".csv"):
                # 构建完整的文件路径并添加到列表中
                file_path = os.path.join(root, file)
                csv_files.append(file_path)

    return csv_files

# 指定要查找的路径
directory_to_search = 'carpet'  # 将此路径替换为你想要查找的路径

# 调用函数获取符合条件的 CSV 文件路径列表
csv_files_list = find_csv_files(directory_to_search)

# 打印找到的 CSV 文件路径
for csv_file in csv_files_list:
    print(csv_file)



def plot_curves(csv_file):
    # 读取CSV文件
    df = pd.read_csv(csv_file)

    num_rows, num_columns = df.shape
    lines = []

    # 绘制每一行的曲线图
    for i in range(num_rows):
        plt.clf() # 清空画布上的所有内容。此处不能调用此函数,不然之前画出的轨迹,将会被清空。
        y_values = df.iloc[i]  # 使用前frame+1个数据
        x_values = range(1, len(y_values) + 1)  # 横坐标使用数据个数
        line, = plt.plot(x_values, y_values)
        lines.append(line)
        plt.pause(0.00001)  # pause a bit so that plots are updated
        if is_ipython:
            display.clear_output(wait=True)
            display.display(plt.gcf())
    # 添加标题和标签
    plt.title('-5mm# Ctm Carpet')
    plt.xlabel('X')
    plt.ylabel('Y')

    # 添加图例
    plt.legend()

    # 显示图形
    plt.show()

# 用法示例
csv_file_path = csv_files_list[0] # 替换为你的CSV文件路径
plot_curves(csv_file_path)

下面这两句是动图gif的重点语句

plt.pause(0.00001)  # pause a bit so that plots are updated
        if is_ipython:
            display.clear_output(wait=True)
            display.display(plt.gcf())
        plt.clf() # 清空画布上的所有内容。此处不能调用此函数,不然之前画出的轨迹,将会被清空。

一个是暂停时间播放下一帧,一个是画布清除

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基本的代码示例,它可以用Python和matplotlib库生成地铁线路的动态图。这里使用的是北京地铁的数据,具体包括地铁线路的站点、站点之间的距离、站点坐标等信息。代码如下: ```python import pandas as pd import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation # 读取地铁线路数据 subway_data = pd.read_csv('subway_data.csv', encoding='gbk') # 绘制地铁线路底图 fig, ax = plt.subplots(figsize=(10, 8)) ax.set_aspect('equal') ax.set_facecolor('black') # 绘制地铁线路 for line, color in zip(subway_data.groupby('line_name'), subway_data['line_color'].unique()): line_stations = line[1].sort_values('station_order') ax.plot(line_stations['x'], line_stations['y'], color=color, linewidth=3) # 绘制地铁站点 for station in subway_data[['station_name', 'x', 'y']].drop_duplicates().values: ax.scatter(station[1], station[2], color='white', s=50) ax.annotate(station[0], xy=(station[1], station[2]), xytext=(-10, 10), textcoords='offset points', color='white') # 列车运行效果 train, = ax.plot([], [], 'o', color='red', markersize=15) # 更新列车位置 def update(frame): train.set_data(subway_data.loc[frame]['x'], subway_data.loc[frame]['y']) return train, # 创建动态图像 ani = animation.FuncAnimation(fig, update, frames=subway_data.shape[0], interval=100) # 保存为GIF动图 ani.save('subway.gif', writer='PillowWriter', fps=30) ``` 这个代码中,首先读取了地铁线路数据,然后使用matplotlib库绘制了地铁线路的底图,包括线路路径和站点坐标等信息。接着,使用动态效果来表示列车在地铁线路上的运行情况,最后将生成的动态图像保存为GIF图像。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值