欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python 绘制动态韦恩图

程序员文章站 2022-03-09 20:17:38
...

绘制动态韦恩图

# -*- coding: utf-8 -*-
"""
@author: xuan
@time: 2021/07/30 10:07
@FileName: 绘制动态韦恩图.PY
"""
import matplotlib.pyplot as plt
from matplotlib_venn import venn3
import pandas as pd
import imageio
import os
import sys

# 处理中文显示乱码的问题
plt.rcParams['font.sans-serif'] = ['SimHei']


class DrawGif(object):
    def __init__(self, dir_path, excel_path):
        # 项目文件夹
        self.dir_path = dir_path
        # 表格数据文件夹
        self.excel_path = excel_path

    def read_excel(self):
        df = pd.read_excel(self.excel_path)
        df2 = df.fillna(0)
        a_list = [i for i in df2['A'].values.tolist()]
        b_list = [i for i in df2['B'].values.tolist()]
        c_list = [i for i in df2['C'].values.tolist()]
        a_b = [i for i in df2['A-B'].values.tolist()]
        a_c = [i for i in df2['A-C'].values.tolist()]
        b_c = [i for i in df2['B-C'].values.tolist()]
        a_b_c = [i for i in df2['A-B-C'].values.tolist()]
        result = list(zip(a_list, b_list, a_b, c_list, a_c, b_c, a_b_c))
        return result

    # 绘制韦恩图
    def draw_png(self):
        _list = self.read_excel()
        for index, value in enumerate(_list):
            v = venn3(subsets=value, set_labels=('A', 'B', 'C'))
            # v.get_label_by_id('A').set_text('Favourite group!')
            plt.savefig(os.path.join(self.dir_path, '{}.png'.format(index+1)))
            plt.close()

    def get_all_png(self):
        path_list = os.listdir(self.dir_path)
        end_num = len(path_list + 1)
        pic_list = [os.path.join(self.dir_path, '{}.png'.format(i)) for i in range(1, end_num)]
        return pic_list

    # 多张png 保存一个gif
    def draw_gif(self):
        pic_list = self.get_all_png()
        gif_name = os.path.join(self.dir_path, 'result.gif')
        # 设置GIF播放速度
        duration_time = 2
        frames = []
        for img in pic_list:
            frames.append(imageio.imread(img))
        imageio.mimsave(gif_name, frames, 'GIF', duration=duration_time)
相关标签: python绘制图型