Python画图进阶_动态图——imageio制作GIF

该博客展示了如何使用Python的matplotlib库绘制四种不同经济周期阶段的饼图,每个阶段显示了股票、现金、债券和商品的分配。然后通过imageio库将这四个饼图合成为动图,以便于理解经济周期的变化。
摘要由CSDN通过智能技术生成

在这里插入图片描述

简化代码

png_list=[]
for i in glob.glob(r"C:\Users\Administrator\Desktop\新建文件夹\图片/*.png"):#存放照片的文件夹
    png_list.append(imageio.imread(i) )
imageio.mimsave("try.gif",png_list,'GIF',duration=0.1)
# coding=utf-8
import math
import tushare as ts
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import talib
import pandas as pd
import datetime 
matplotlib.rcParams['axes.unicode_minus']=False
plt.rcParams['font.sans-serif']=['SimHei']
import numpy as np
import imageio
import datetime 
def f1():    
    fig, ax = plt.subplots()

    size = 0.3
    vals = np.array([[20., 22.], [20., 22.], [20., 22.],[20., 22.]])

    cmap = plt.get_cmap("tab20c")
    outer_colors = cmap(np.arange(4)*2)
    inner_colors = cmap([1, 2, 5, 6, 9, 10])

    ax.pie(vals.sum(axis=1), radius=1, colors=outer_colors,
           wedgeprops=dict(width=size, edgecolor='w'))

    plt.text(0.5, 0.5, '股票')
    plt.text(-0.65, -0.65, '现金')
    plt.text(-0.66, 0.6, '债券')
    plt.text(0.55, -0.55, '商品')

    ax.annotate('复苏期',
                xy=(1.2, -0.6), xycoords='data',
                xytext=(0, 112), textcoords='offset points',
                arrowprops=dict(facecolor='black', shrink=11),
                horizontalalignment='right', verticalalignment='bottom')

    ax.set(aspect="equal", title='')
    plt.show()
def f2():    
    fig, ax = plt.subplots()

    size = 0.3
    vals = np.array([[20., 22.], [20., 22.], [20., 22.],[20., 22.]])

    cmap = plt.get_cmap("tab20c")
    outer_colors = cmap(np.arange(4)*2)
    inner_colors = cmap([1, 2, 5, 6, 9, 10])

    ax.pie(vals.sum(axis=1), radius=1, colors=outer_colors,
           wedgeprops=dict(width=size, edgecolor='w'))

    plt.text(0.5, 0.5, '股票')
    plt.text(-0.65, -0.65, '现金')
    plt.text(-0.66, 0.6, '债券')
    plt.text(0.55, -0.55, '商品')


    ax.annotate('过热期',
                xy=(-0.65, -1.2), xycoords='data',
                xytext=(152, 0), textcoords='offset points',
                arrowprops=dict(facecolor='black', shrink=11),
                horizontalalignment='right', verticalalignment='bottom')

    ax.set(aspect="equal", title='')
    plt.show()
def f4():    
    fig, ax = plt.subplots()

    size = 0.3
    vals = np.array([[20., 22.], [20., 22.], [20., 22.],[20., 22.]])

    cmap = plt.get_cmap("tab20c")
    outer_colors = cmap(np.arange(4)*2)
    inner_colors = cmap([1, 2, 5, 6, 9, 10])

    ax.pie(vals.sum(axis=1), radius=1, colors=outer_colors,
           wedgeprops=dict(width=size, edgecolor='w'))

    plt.text(0.5, 0.5, '股票')
    plt.text(-0.65, -0.65, '现金')
    plt.text(-0.66, 0.6, '债券')
    plt.text(0.55, -0.55, '商品')

   

    ax.annotate('衰退期',
                xy=(0.65, 1.2), xycoords='data',
                xytext=(-122, 0), textcoords='offset points',
                arrowprops=dict(facecolor='black', shrink=11),
                horizontalalignment='right', verticalalignment='bottom')
    
    ax.set(aspect="equal", title='')
    plt.show()
def f3():    
    fig, ax = plt.subplots()

    size = 0.3
    vals = np.array([[20., 22.], [20., 22.], [20., 22.],[20., 22.]])

    cmap = plt.get_cmap("tab20c")
    outer_colors = cmap(np.arange(4)*2)
    inner_colors = cmap([1, 2, 5, 6, 9, 10])

    ax.pie(vals.sum(axis=1), radius=1, colors=outer_colors,
           wedgeprops=dict(width=size, edgecolor='w'))

    plt.text(0.5, 0.5, '股票')
    plt.text(-0.65, -0.65, '现金')
    plt.text(-0.66, 0.6, '债券')
    plt.text(0.55, -0.55, '商品')


    ax.annotate('滞涨期',
                xy=(-1.2, 0.66), xycoords='data',
                xytext=(0, -122), textcoords='offset points',
                arrowprops=dict(facecolor='black', shrink=11),
                horizontalalignment='right', verticalalignment='bottom')
    ax.set(aspect="equal", title='')
    plt.show()

在这里插入图片描述

def f1():
    fig, ax = plt.subplots()
    size = 0.7#控制外圈大小
    vals = np.array([[20., 22.], [20., 22.], [20., 22.],[20., 22.]])

    cmap = plt.get_cmap("tab20c")
    outer_colors = cmap(np.arange(4)*2)
    inner_colors = cmap([1, 2, 5, 6, 9, 10])

    ax.pie(vals.sum(axis=1), radius=1, colors=outer_colors,
           wedgeprops=dict(width=size, edgecolor='w'))

    plt.text(0.45, 0.6, '股票')
    plt.text(-0.68, -0.65, '滞涨期')
    plt.text(-0.66, 0.6, '债券')
    plt.text(-0.66, 0.45, '衰退期')
    plt.text(0.35, -0.65, '过热期')
    plt.text(0.41, -0.50, '商品')
    plt.text(0.40, 0.45, '复苏期')
    plt.text(-0.65, -0.50, '现金')    
   
    ax.annotate('',
                xy=(.35, 0.5), xycoords='data',
                xytext=(-50, 0), textcoords='offset points',
                arrowprops=dict(facecolor='black', shrink=11),
                horizontalalignment='right', verticalalignment='bottom')  

    ax.set(aspect="equal", title='')
    plt.savefig('t4.png',dpi=500)

依次画出四张图

#!/bin/python3
import imageio
import glob
import re
def create_gif(image_list, gif_name):  
    frames = []  
    for image_name in image_list:  
        frames.append(imageio.imread(image_name))  
    # Save them as frames into a gif   
    imageio.mimsave(gif_name, frames, 'GIF', duration = 0.000001)  
    return 
def find_all_png():
    png_filenames = glob.glob(r"C:\Users\Administrator\Desktop\新建文件夹\图片/*.png")#存放图片的文件夹 
    buf=[]
    for png_file in png_filenames:
        buf.append(png_file)
    return buf
if __name__ == '__main__':
    buff = find_all_png()
    create_gif(buff,'created_gif.gif' )

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小李、不姓李

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值