draw.py 2.96 KB
Newer Older
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
1 2 3 4 5 6 7 8 9 10 11
# -*- encoding: utf-8 -*-
# -----------------------------------------------------------------------------
# @File Name  : draw.py
# @Time       : 2020/11/19 上午10:51
# @Author     : X. Peng
# @Email      : acepengxiong@163.com
# @Software   : PyCharm
# -----------------------------------------------------------------------------

import numpy as np
import matplotlib.pyplot as plt
12
from matplotlib import ticker
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
13 14 15 16 17 18
from matplotlib.ticker import FuncFormatter
from matplotlib.font_manager import FontProperties


def to_percent(temp, position):
    return '%1.0f' % temp + '%'
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
19

20
def draw_stacked_column_chart(product_list, cumulative):
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
21 22
    """堆叠柱状图"""

23 24 25 26 27 28 29 30
    # plt.title('Scores by group and gender')
    # plt.ylabel('Scores')
    # 初始化
    fig = plt.figure(figsize=(12, 8))
    ax1 = fig.add_subplot()
    ax2 = ax1.twiny()
    max_x_count = max([x['data'].size for x in product_list])
    loc = np.arange(max_x_count)  # the x locations for the groups
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
31
    width = 0.35  # the width of the bars: can also be len(x) sequence
32
    color_list = ['#222A77', '#6C71AA', '#E1BC95', '#102A77', '#6CB1AA', '#CCBC95']
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
33

34 35 36 37 38 39 40 41 42 43 44 45
    # 堆叠柱状图
    prod_legend = []
    for i in range(len(product_list)):
        ax = None
        bottom = np.zeros(max_x_count)
        if i == 0:
            ax = ax1.bar(loc, product_list[i]['data'], width, color=color_list[i], alpha=0.8)
        else:
            for j in range(i):
                bottom = bottom + product_list[j]['data']
            ax = ax1.bar(loc, product_list[i]['data'], width, bottom=bottom, color=color_list[i], alpha=0.8)
        prod_legend.append(ax[0])
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
46

47 48 49 50 51
    # 坐标轴
    ax1.set_xticks(loc)
    ax1.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
    plt.gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
    ax1.legend(prod_legend, [prod['name'] for prod in product_list], bbox_to_anchor=(0.7, -0.1), ncol=4)
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
52

pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
53
    # 画折线图
54 55 56 57 58 59 60 61 62
    # 最大收益
    temp_rate = np.zeros(max_x_count)
    for i in range(len(product_list)):
        temp_rate += product_list[i]['data']
    max_rate = np.max(np.hstack((temp_rate, cumulative['data'])))
    ax2.set_xticks([])
    ax2.set_ylim(0, max_rate + 10)
    ax2.plot(loc, cumulative['data'], color='#C6A774', marker='', linewidth=3, label=cumulative['name'])
    ax2.legend(loc='upper left')
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
63 64 65 66

    plt.show()

if __name__ == '__main__':
67 68 69 70 71 72 73 74 75
    product1 = {'name': 'product1', 'data': np.array([10, 20, 30, 40, 50])}
    product2 = {'name': 'product2', 'data': np.array([20, 20, 20, 20, 20])}
    product3 = {'name': 'product3', 'data': np.array([20, 20, 20, 20, 20])}
    product4 = {'name': 'product4', 'data': np.array([20, 20, 20, 20, 20])}
    product5 = {'name': 'product5', 'data': np.array([20, 20, 20, 20, 20])}
    product6 = {'name': 'product6', 'data': np.array([20, 20, 20, 20, 20])}
    product_list = [product1, product2, product3, product4, product5, product6]
    cumulative = {'name': 'totalincome', 'data': np.array([10, 50, 120, 100, 36])}
    draw_stacked_column_chart(product_list, cumulative)