
Matplotlib绘制分组聚合流程图
发布日期:2021-05-10 12:02:28
浏览次数:7
分类:精选文章
本文共 4460 字,大约阅读时间需要 14 分钟。
Matplotlib ���������������������������
��������������� Matplotlib
������������������������������������������������������������������������������������������
���������������������������
import matplotlib as mplimport matplotlib.pyplot as plt# ������������������������������mpl.rcParams['font.sans-serif'] = ['SimHei']mpl.rcParams['axes.unicode_minus'] = Falsedef draw_dataframe(df, loc=None, width=None, ax=None, linestyle=None, textstyle=None): """ ������������������������������ Parameters: df (DataFrame): ��������� loc (tuple, optional): ������������������������������ (0, 0) width (float, optional): ������������������������ 1 ax (axes, optional): ��������������������������������� linestyle (dict, optional): ��������������������������������������� textstyle (dict, optional): ���������������������������������������12 """ loc = loc or [0, 0] width = width or 1 x, y = loc if ax is None: ax = plt.gca() ncols = len(df.columns) + 1 nrows = len(df.index) + 1 dx = dy = width / ncols if linestyle is None: linestyle = {'color': 'black'} if textstyle is None: textstyle = {'size': 12} textstyle.update({'ha': 'center', 'va': 'center'}) # ������������������ for i in range(ncols + 1): ax.plot(2 * [x + i * dx], [y, y + dy * nrows], **linestyle) # ������������������ for i in range(nrows + 1): ax.plot([x, x + dx * ncols], 2 * [y + i * dy], **linestyle) # ������������������ for i in range(nrows - 1): ax.text(x + 0.5 * dx, y + (i + 0.5) * dy, str(df.index[::-1][i]), **textstyle) # ��������������� for i in range(ncols - 1): ax.text(x + (i + 1.5) * dx, y + (nrows - 0.5) * dy, str(df.columns[i]), style='italic', **textstyle) # ������������������ if df.index.name: ax.text(x + 0.5 * dx, y + (nrows - 0.5) * dy, str(df.index.name), style='italic', **textstyle) # ������������������ for i in range(nrows - 1): for j in range(ncols - 1): ax.text(x + (j + 1.5) * dx, y + (i + 0.5) * dy, str(df.values[::-1][i, j]), **textstyle)
���������������
# ������������plt.figure(figsize=(20, 6), facecolor='white')ax = plt.axes([0, 0, 1, 1])ax.axis('off')# ������������������ys = [1.8, 0.5, -0.5]result = []# ������������������for i, (deal_date, split) in enumerate(data.groupby('deal_date')): # ������������������������ draw_dataframe(split.set_index('deal_date'), [1.4, ys[i]]) # ��������������������������� split.loc[split.area == 'A���', 'A���'] = split.order_id split.loc[split.area == 'B���', 'B���'] = split.order_id split.loc[split.area == 'C���', 'C���'] = split.order_id # ��������������������������� split = split.set_index('deal_date')['A���', 'B���', 'C���'] draw_dataframe(split, [2.7, ys[i]]) # ��������������������� sum_df = split.count().to_frame().T sum_df.index.name = 'deal_date' draw_dataframe(sum_df, [3.8, ys[i] + 0.25]) # ��������������� result.append(sum_df)# ������������result_df = pd.concat(result)draw_dataframe(result_df, [5.1, 0.6])
���������������������
# ������������������style = dict(fontsize=16, ha='center', weight='bold')# ������������������plt.text(0.5, 2.4, "Input", **style)# ������������������plt.text(1.9, 2.9, "Split", **style)# ������������������plt.text(3.75, 2.9, "Apply(count)", **style)# ������������������plt.text(5.6, 1.7, "Combine", **style)# ������������������arrowprops = dict(facecolor='black', width=1, headwidth=6)# ���������������������������plt.annotate('', (1.4, 2.3), (1.0, 1.55), arrowprops=arrowprops)plt.annotate('', (1.4, 1), (1.0, 1), arrowprops=arrowprops)plt.annotate('', (1.4, -0.1), (1.0, 0.45), arrowprops=arrowprops)# ���������������������������plt.annotate('', (2.7, 2.3), (2.4, 2.3), arrowprops=arrowprops)plt.annotate('', (2.7, 1), (2.4, 1), arrowprops=arrowprops)plt.annotate('', (2.7, -0.1), (2.4, -0.1), arrowprops=arrowprops)# ������������������������plt.annotate('', (5.1, 1.2), (4.8, 2.3), arrowprops=arrowprops)plt.annotate('', (5.1, 1), (4.8, 1), arrowprops=arrowprops)plt.annotate('', (5.1, 0.8), (4.8, -0.1), arrowprops=arrowprops)
������������
������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
很好
[***.229.124.182]2025年04月28日 11时27分14秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Android--消息摘要MD5,SHA加密
2021-05-10
Android--屏幕适配
2021-05-10
python3 HTTP Error 403:Forbidden
2021-05-10
Linux软件安装--源码编译
2021-05-10
【DockerCE】CentOS 7.9安装DockerCE 20.10.6
2021-05-10
Java 中的多态详解
2021-05-10
Java常用包系列--JWT
2021-05-10
并发相关问题
2021-05-10
Linux应用编程示例
2021-05-10
flutter 基于Timer实现的倒计时以及周期性任务
2021-05-10
Java时间类Date全解析
2021-05-10
linux系统下双屏显示
2021-05-10
关于高斯模糊核
2021-05-10
PDF.js —— vue项目中使用pdf.js显示pdf文件(流)
2021-05-10
vue源码解析 —— 数据绑定
2021-05-10
JS单线程的理解
2021-05-10
vue解决数据更新,界面未更新的方法
2021-05-10
我用wxPython搭建GUI量化系统之wx.grid实现excel功能
2021-05-10
我用wxPython搭建GUI量化系统之wx.Toolbar实现工具栏
2021-05-10
我用wxPython搭建GUI量化系统之wx.TextCtrl实现文本框
2021-05-10