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)

������������

������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:100毫秒过滤一百万字文本的停用词
下一篇:对比Pandas轻松理解MySQL分组聚合的实现原理

发表评论

最新留言

很好
[***.229.124.182]2025年04月28日 11时27分14秒