draw.py 10.2 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
from matplotlib.ticker import FuncFormatter
from matplotlib.font_manager import FontProperties

16
# 中文字体初始化
17
plt.rcParams['font.sans-serif']=['SimSun']
18

pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
19 20 21

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

23 24 25

def draw_month_return_chart(xlabels, product_list, cumulative):
    """月度回报表现图"""
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
26

27 28
    # plt.title('Scores by group and gender')
    # plt.ylabel('Scores')
29 30 31
    figsize = (20, 12)
    # 标签文字大小
    fontsize = 20
32
    # 初始化
33
    fig = plt.figure(figsize=figsize)
34
    ax1 = fig.add_subplot(111)
35
    ax2 = ax1.twinx()
36 37
    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
38
    width = 0.35  # the width of the bars: can also be len(x) sequence
39
    color_list = ['#222A77', '#6C71AA', '#E1BC95', '#F9DBB8']
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    # 坐标轴
    ax1.tick_params(labelsize=fontsize)
    ax2.tick_params(labelsize=fontsize)
    # 坐标轴颜色
    ax2.tick_params(axis='y', colors='#C6A774')
    ax1.set_xticks(loc)
    ax1.set_xticklabels(xlabels)
    ax1.yaxis.set_major_formatter(FuncFormatter(to_percent))
    ax2.yaxis.set_major_formatter(FuncFormatter(to_percent))
    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_ylim(0, max_rate + 15)

    # 柱状图
57 58 59 60 61 62 63 64 65
    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']
66 67 68 69
            if i < len(color_list):
                ax = ax1.bar(loc, product_list[i]['data'], width, bottom=bottom, color=color_list[i], alpha=0.8)
            else:
                ax = ax1.bar(loc, product_list[i]['data'], width, bottom=bottom, alpha=0.8)
70
        prod_legend.append(ax[0])
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    ax1.legend(prod_legend, [prod['name'] for prod in product_list], loc='upper left', fontsize=fontsize)

    # 画折线图
    ax2.plot(loc, cumulative['data'], color='#C6A774', marker='', linewidth=3, label=cumulative['name'])
    ax2.legend(loc='upper center', fontsize=fontsize)

    plt.show()


def draw_contribution_chart(xlabels, product_list, cumulative):
    """贡献分解图"""

    # plt.title('Scores by group and gender')
    # plt.ylabel('Scores')
    figsize = (20, 12)
    # 标签文字大小
    fontsize = 22
    # 初始化
    fig = plt.figure(figsize=figsize)
    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
    width = 0.35  # the width of the bars: can also be len(x) sequence
    color_list = ['#222A77', '#6C71AA', '#E1BC95', '#F9DBB8']
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
96

97
    # 坐标轴
98
    ax1.tick_params(labelsize=fontsize)
99
    ax1.set_xticks(loc)
100 101
    ax1.set_xticklabels(xlabels)
    ax1.yaxis.set_major_formatter(FuncFormatter(to_percent))
102 103 104 105 106 107
    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)
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

    # 堆叠柱状图
    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']
            if i < len(color_list):
                ax = ax1.bar(loc, product_list[i]['data'], width, bottom=bottom, color=color_list[i], alpha=0.8)
            else:
                ax = ax1.bar(loc, product_list[i]['data'], width, bottom=bottom, alpha=0.8)
        prod_legend.append(ax[0])
    ax1.legend(prod_legend, [prod['name'] for prod in product_list], bbox_to_anchor=(0.8, -0.1), ncol=4, fontsize=fontsize)

    # 画折线图
127
    ax2.plot(loc, cumulative['data'], color='#C6A774', marker='', linewidth=3, label=cumulative['name'])
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    ax2.legend(loc='upper left', fontsize=fontsize)

    plt.show()


def draw_comment_chart(xlabels, source_prod, target_prod):
    """个基点评图"""
    figsize = (20, 12)
    # 标签文字大小
    fontsize = 22
    # 初始化
    fig = plt.figure(figsize=figsize)
    ax1 = fig.add_subplot()
    ax2 = ax1.twiny()
    # ax = plt.gca()  # gca:get current axis得到当前轴
    # ax.spines['bottom'].set_position(('data', 0))  # data表示通过值来设置x轴的位置,将x轴绑定在y=0的位置
    product_list = [source_prod, target_prod]
    max_x_count = max([x['data'].size for x in product_list])
    loc = np.arange(max_x_count)  # the x locations for the groups

    # 坐标轴
    ax1.tick_params(labelsize=fontsize)
    ax2.tick_params(labelsize=fontsize)
    ax1.set_xticks(loc)
    ax1.set_xticklabels(xlabels)
    ax1.yaxis.set_major_formatter(FuncFormatter(to_percent))
    max_rate = np.max(np.hstack((source_prod['data'], target_prod['data'])))
    ax2.set_xticks([])

    # 个基折线图
    ax1.plot(loc, source_prod['data'], color='#C6A774', marker='', linewidth=3, label=source_prod['name'])
    ax1.legend(loc='upper left', fontsize=fontsize)

    # 指数折线图
    ax2.plot(loc, target_prod['data'], color='black', marker='', linewidth=3, label=target_prod['name'])
    ax2.legend(loc='upper center', fontsize=fontsize)

    plt.show()


def draw_combination_chart(xlabels, new_combination, origin_combination, index):
    """组合对比图"""
    figsize = (20, 12)
    # 标签文字大小
    fontsize = 22
    # 初始化
    fig = plt.figure(figsize=figsize)
    ax1 = fig.add_subplot()
    ax2 = ax1.twiny()
    ax3 = ax1.twiny()
    # ax = plt.gca()  # gca:get current axis得到当前轴
    # ax.spines['bottom'].set_position(('data', 0))  # data表示通过值来设置x轴的位置,将x轴绑定在y=0的位置
    product_list = [origin_combination, new_combination, index]
    max_x_count = max([x['data'].size for x in product_list])
    loc = np.arange(max_x_count)  # the x locations for the groups

    # 坐标轴
    ax1.tick_params(labelsize=fontsize)
    ax2.tick_params(labelsize=fontsize)
    ax3.tick_params(labelsize=fontsize)
    ax1.set_xticks(loc)
    ax1.set_xticklabels(xlabels)
    ax1.yaxis.set_major_formatter(FuncFormatter(to_percent))
    ax2.set_xticks([])
    ax3.set_xticks([])

    # 新组合折线图
    ax1.plot(loc, new_combination['data'], color='#C6A774', marker='', linewidth=3, label=origin_combination['name'])
    ax1.legend(loc='upper left', fontsize=fontsize)

    # 原组合折线图
    ax2.plot(loc, origin_combination['data'], color='#222A77', marker='', linewidth=3, label=new_combination['name'])
    ax2.legend(loc='upper center', fontsize=fontsize)

    # 指数折线图
    ax3.plot(loc, index['data'], color='black', marker='', linewidth=3, label=index['name'])
    ax3.legend(loc='upper right', fontsize=fontsize)
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
205 206 207

    plt.show()

208 209 210 211 212 213

def draw_correlation_chart():
    """相关性分析图"""
    pass


pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
214
if __name__ == '__main__':
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    # xlabels = ('2020-1', '2020-2', '2020-3', '2020-4', '2020-5', '2020-6', '2020-7', '2020-8', '2020-9', '2020-10', '2020-11', '2020-12')
    # product = {'name': '月度回报率', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
    # contrast = {'name': '同比上涨', 'data': np.array([10, 50, 120, 100, 36, 0, 50, 120, 100, 36, 23, 98])}
    # draw_month_return_chart(xlabels, [product], contrast)

    # xlabels = ('2020-1', '2020-2', '2020-3', '2020-4', '2020-5', '2020-6', '2020-7', '2020-8', '2020-9', '2020-10', '2020-11', '2020-12')
    # product1 = {'name': '塞亚成长1号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
    # product2 = {'name': '塞亚成长2号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
    # product3 = {'name': '塞亚成长3号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
    # product4 = {'name': '塞亚成长4号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
    # product5 = {'name': '塞亚成长5号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
    # product6 = {'name': '塞亚成长6号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
    # product7 = {'name': '塞亚成长7号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
    # product8 = {'name': '塞亚成长8号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
    # product_list = [product1, product2, product3, product4, product5, product6, product7, product8]
    # cumulative = {'name': '总收益', 'data': np.array([10, 50, 120, 100, 36, 0, 50, 120, 100, 36, 23, 98])}
    # draw_contribution_chart(xlabels, product_list, cumulative)

    # xlabels = ('2020-1', '2020-2', '2020-3', '2020-4', '2020-5', '2020-6', '2020-7', '2020-8', '2020-9', '2020-10', '2020-11', '2020-12')
    # source_prod = {'name': '远澜银杏1号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
    # target_prod = {'name': '上证指数', 'data': np.array([-10, 10, 5, 55, 24, 10, 20, 8, 10, 31, 40, 32])}
    # draw_comment_chart(xlabels, source_prod, target_prod)

    xlabels = ('2020-1', '2020-2', '2020-3', '2020-4', '2020-5', '2020-6', '2020-7', '2020-8', '2020-9', '2020-10', '2020-11', '2020-12')
    new_combination = {'name': '新组合', 'data': np.array([20, 30, 40, 50, 60, 20, 30, 40, 50, 60, 50, 60])}
    origin_combination = {'name': '原组合', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
    index = {'name': '上证指数', 'data': np.array([-10, 10, 5, 55, 24, 10, 20, 8, 10, 31, 40, 32])}
    draw_combination_chart(xlabels, new_combination, origin_combination, index)