import os
import numpy as np
import matplotlib.pyplot as plt
import imageio
# # 生成40个取值在30-40的数
y = np.random.randint(30, 40, size=(40))
# # 绘制折线
# plt.plot(y)
# # 设置y轴最小值和最大值
# plt.ylim(20, 50)
#
# # 显示
# plt.show()
# # 生成40个取值在30-40的数
# y = np.random.randint(30, 40, size=(40))
#
# # 第一张图
# plt.plot(y[:-3])
# plt.ylim(20, 50)
# plt.savefig('1.png')
# plt.show()
#
# # 第二张图
# plt.plot(y[:-2])
# plt.ylim(20, 50)
# plt.savefig('2.png')
# plt.show()
#
# # 第三张图
# plt.plot(y[:-1])
# plt.ylim(20, 50)
# plt.savefig('3.png')
# plt.show()
#
# # 第四张图
# plt.plot(y)
# plt.ylim(20, 50)
# plt.savefig('4.png')
# plt.show()
# # 生成Gif
# with imageio.get_writer('mygif.gif', mode='I') as writer:
# for filename in ['1.png', '2.png', '3.png', '4.png']:
# image = imageio.imread(filename)
# writer.append_data(image)
# filenames = []
# num = 0
# for i in y:
# num += 1
# # 绘制40张折线图
# plt.plot(y[:num])
# plt.ylim(20, 50)
#
# # 保存图片文件
# filename = f'{num}.png'
# filenames.append(filename)
# plt.savefig(filename)
# plt.close()
#
# # 生成gif
# with imageio.get_writer('mygif.gif', mode='I') as writer:
# for filename in filenames:
# image = imageio.imread(filename)
# writer.append_data(image)
#
# # 删除40张折线图
# for filename in set(filenames):
# os.remove(filename)
# x = [1, 2, 3, 4, 5]
# coordinates_lists = [[0, 0, 0, 0, 0],
# [10, 30, 60, 30, 10],
# [70, 40, 20, 40, 70],
# [10, 20, 30, 40, 50],
# [50, 40, 30, 20, 10],
# [75, 0, 75, 0, 75],
# [0, 0, 0, 0, 0]]
# filenames = []
# for index, y in enumerate(coordinates_lists):
# # 条形图
# plt.bar(x, y)
# plt.ylim(0, 80)
#
# # 保存图片文件
# filename = f'{index}.png'
# filenames.append(filename)
#
# # 重复最后一张图形15帧(数值都为0),15张图片
# if (index == len(coordinates_lists) - 1):
# for i in range(15):
# filenames.append(filename)
#
# # 保存
# plt.savefig(filename)
# plt.close()
#
# # 生成gif
# with imageio.get_writer('mygif.gif', mode='I') as writer:
# for filename in filenames:
# image = imageio.imread(filename)
# writer.append_data(image)
#
# # 删除20几张柱状图
# for filename in set(filenames):
# os.remove(filename)
x = [1, 2, 3, 4, 5]
# coordinates_lists = [[0, 0, 0, 0, 0],
# [10, 30, 60, 30, 10],
# [70, 40, 20, 40, 70],
# [10, 20, 30, 40, 50],
# [50, 40, 30, 20, 10],
# [75, 0, 75, 0, 75],
# [0, 0, 0, 0, 0]]
# filenames = []
# for index, y in enumerate(coordinates_lists):
# # 条形图
# plt.bar(x, y)
# plt.ylim(0, 80)
#
# # 保存图片文件
# filename = f'{index}.png'
# filenames.append(filename)
#
# # 重复最后一张图形15帧(数值都为0),15张图片
# if (index == len(coordinates_lists) - 1):
# for i in range(15):
# filenames.append(filename)
#
# # 保存
# plt.savefig(filename)
# plt.close()
#
# # 生成gif
# with imageio.get_writer('mygif.gif', mode='I') as writer:
# for filename in filenames:
# image = imageio.imread(filename)
# writer.append_data(image)
#
# # 删除20几张柱状图
# for filename in set(filenames):
# os.remove(filename)
# n_frames = 10
# x = [1, 2, 3, 4, 5]
# coordinates_lists = [[0, 0, 0, 0, 0],
# [10, 30, 60, 30, 10],
# [70, 40, 20, 40, 70],
# [10, 20, 30, 40, 50],
# [50, 40, 30, 20, 10],
# [75, 0, 75, 0, 75],
# [0, 0, 0, 0, 0]]
# print('生成图表\n')
# filenames = []
# for index in np.arange(0, len(coordinates_lists) - 1):
# # 获取当前图像及下一图像的y轴坐标值
# y = coordinates_lists[index]
# y1 = coordinates_lists[index + 1]
#
# # 计算当前图像与下一图像y轴坐标差值
# y_path = np.array(y1) - np.array(y)
# for i in np.arange(0, n_frames + 1):
# # 分配每帧的y轴移动距离
# # 逐帧增加y轴的坐标值
# y_temp = (y + (y_path / n_frames) * i)
# # 绘制条形图
# plt.bar(x, y_temp)
# plt.ylim(0, 80)
# # 保存每一帧的图像
# filename = f'images/frame_{index}_{i}.png'
# filenames.append(filename)
# # 最后一帧重复,画面停留一会
# if (i == n_frames):
# for i in range(5):
# filenames.append(filename)
# # 保存图片
# plt.savefig(filename)
# plt.close()
# print('保存图表\n')
# # 生成GIF
# print('生成GIF\n')
# with imageio.get_writer('mybars.gif', mode='I') as writer:
# for filename in filenames:
# image = imageio.imread(filename)
# writer.append_data(image)
# print('保存GIF\n')
# print('删除图片\n')
# # 删除图片
# for filename in set(filenames):
# os.remove(filename)
# print('完成')
# n_frames = 10
# bg_color = '#95A4AD'
# bar_color = '#283F4E'
# gif_name = 'bars'
# x = [1, 2, 3, 4, 5]
# coordinates_lists = [[0, 0, 0, 0, 0],
# [10, 30, 60, 30, 10],
# [70, 40, 20, 40, 70],
# [10, 20, 30, 40, 50],
# [50, 40, 30, 20, 10],
# [75, 0, 75, 0, 75],
# [0, 0, 0, 0, 0]]
# print('生成图表\n')
# filenames = []
# for index in np.arange(0, len(coordinates_lists) - 1):
# y = coordinates_lists[index]
# y1 = coordinates_lists[index + 1]
# y_path = np.array(y1) - np.array(y)
# for i in np.arange(0, n_frames + 1):
# y_temp = (y + (y_path / n_frames) * i)
# # 绘制条形图
# fig, ax = plt.subplots(figsize=(8, 4))
# ax.set_facecolor(bg_color)
# plt.bar(x, y_temp, width=0.4, color=bar_color)
# plt.ylim(0, 80)
# # 移除图表的上边框和右边框
# ax.spines['right'].set_visible(False)
# ax.spines['top'].set_visible(False)
# # 设置虚线网格线
# ax.set_axisbelow(True)
# ax.yaxis.grid(color='gray', linestyle='dashed', alpha=0.7)
# # 保存每一帧的图像
# filename = f'images/frame_{index}_{i}.png'
# filenames.append(filename)
#
# # 最后一帧重复,画面停留一会
# if (i == n_frames):
# for i in range(5):
# filenames.append(filename)
# # 保存图片
# plt.savefig(filename, dpi=96, facecolor=bg_color)
# plt.close()
# print('保存图表\n')
# # 生成GIF
# print('生成GIF\n')
# with imageio.get_writer(f'{gif_name}.gif', mode='I') as writer:
# for filename in filenames:
# image = imageio.imread(filename)
# writer.append_data(image)
# print('保存GIF\n')
# print('删除图片\n')
# # 删除图片
# for filename in set(filenames):
# os.remove(filename)
# print('完成')
coordinates_lists = [[[0], [0]],
[[100, 200, 300], [100, 200, 300]],
[[400, 500, 600], [400, 500, 600]],
[[400, 500, 600, 400, 500, 600], [400, 500, 600, 600, 500, 400]],
[[500], [500]],
[[0], [0]]]
gif_name = 'movie'
n_frames = 10
bg_color = '#95A4AD'
marker_color = '#283F4E'
marker_size = 25
print('生成图表\n')
filenames = []
for index in np.arange(0, len(coordinates_lists) - 1):
# 获取当前图像及下一图像的x与y轴坐标值
x = coordinates_lists[index][0]
y = coordinates_lists[index][1]
x1 = coordinates_lists[index + 1][0]
y1 = coordinates_lists[index + 1][1]
# 查看两