美文网首页Matplotlib
多个图形的合并

多个图形的合并

作者: apricoter | 来源:发表于2019-02-19 22:43 被阅读3次

    通过matplotlib模块中的subplot2grid函数,可以是m*n的矩阵风格,也可以是跨行或跨列的矩阵风格

    # 读取数据
    Prod_Trade = pd.read_excel(r'F:\Prod_Trade.xlsx')
    Prod_Trade.head()
    

    以某集市商品交易数据为例


    # 衍生出交易年份和月份字段
    Prod_Trade['year'] = Prod_Trade.Date.dt.year
    Prod_Trade['month'] = Prod_Trade.Date.dt.month
    #再次查看数据
    Prod_Trade.head()
    
    # 设置大图框的长和高
    plt.figure(figsize = (12,6))
    # 设置第一个子图的布局
    ax1 = plt.subplot2grid(shape = (2,3), loc = (0,0))
    # 统计2012年各订单等级的数量
    Class_Counts = Prod_Trade.Order_Class[Prod_Trade.year == 2012].value_counts()
    Class_Percent = Class_Counts/Class_Counts.sum()
    # 将饼图设置为圆形(否则有点像椭圆)
    ax1.set_aspect(aspect = 'equal')
    # 绘制订单等级饼图
    ax1.pie(x = Class_Percent.values, labels = Class_Percent.index, autopct = '%.1f%%')
    # 添加标题
    ax1.set_title('各等级订单比例')
    
    # 设置第二个子图的布局
    ax2 = plt.subplot2grid(shape = (2,3), loc = (0,1))
    # 统计2012年每月销售额
    Month_Sales = Prod_Trade[Prod_Trade.year == 2012].groupby(by = 'month').aggregate({'Sales':np.sum})
    # 绘制销售额趋势图
    Month_Sales.plot(title = '2012年各月销售趋势', ax = ax2, legend = False)
    # 删除x轴标签
    ax2.set_xlabel('')
    
    # 设置第三个子图的布局
    ax3 = plt.subplot2grid(shape = (2,3), loc = (0,2), rowspan = 2)
    # 绘制各运输方式的成本箱线图
    sns.boxplot(x = 'Transport', y = 'Trans_Cost', data = Prod_Trade, ax = ax3)
    # 添加标题
    ax3.set_title('各运输方式成本分布')
    # 删除x轴标签
    ax3.set_xlabel('')
    # 修改y轴标签
    ax3.set_ylabel('运输成本')
    
    # 设置第四个子图的布局
    ax4 = plt.subplot2grid(shape = (2,3), loc = (1,0), colspan = 2)
    # 2012年客单价分布直方图
    sns.distplot(Prod_Trade.Sales[Prod_Trade.year == 2012], bins = 40, norm_hist = True, ax = ax4, hist_kws = {'color':'steelblue'}, kde_kws=({'linestyle':'--', 'color':'red'}))
    # 添加标题
    ax4.set_title('2012年客单价分布图')
    # 修改x轴标签
    ax4.set_xlabel('销售额')
    
    # 调整子图之间的水平间距和高度间距
    plt.subplots_adjust(hspace=0.6, wspace=0.3)
    # 图形显示
    plt.show()
    

    绘制每一幅子图之前,运用subplot2grid函数控制子图的位置,传递给ax1,ax2..
    为子图添加标题等,需要单独指定


    相关文章

      网友评论

        本文标题:多个图形的合并

        本文链接:https://www.haomeiwen.com/subject/phyqyqtx.html