堆叠柱状图叠加折线图

parent eab84693
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from matplotlib import ticker
from matplotlib.ticker import FuncFormatter from matplotlib.ticker import FuncFormatter
from matplotlib.font_manager import FontProperties from matplotlib.font_manager import FontProperties
...@@ -16,58 +17,59 @@ from matplotlib.font_manager import FontProperties ...@@ -16,58 +17,59 @@ from matplotlib.font_manager import FontProperties
def to_percent(temp, position): def to_percent(temp, position):
return '%1.0f' % temp + '%' return '%1.0f' % temp + '%'
def draw_stacked_column_chart(): def draw_stacked_column_chart(product_list, cumulative):
"""堆叠柱状图""" """堆叠柱状图"""
plt.title('Scores by group and gender') # plt.title('Scores by group and gender')
# plt.ylabel('Scores')
# ind = np.arange(12) # [ 0 1 2 3 4 5 6 7 8 9 10 11 12] # 初始化
# plt.xticks(ind, ('M1', 'M2', 'M3', 'M4', 'M5', 'M6', 'M7', 'M8', 'M9', 'M10', 'M11', 'M12')) fig = plt.figure(figsize=(12, 8))
# ax1 = fig.add_subplot()
# plt.yticks(np.arange(0, 81, 20)) ax2 = ax1.twiny()
# max_x_count = max([x['data'].size for x in product_list])
# Bottom = (52, 49, 48, 47, 44, 43, 41, 41, 40, 38, 36, 31, 29) loc = np.arange(max_x_count) # the x locations for the groups
# Center = (38, 40, 45, 42, 48, 51, 53, 54, 57, 59, 57, 64, 62)
# Top = (10, 11, 7, 11, 8, 6, 6, 5, 3, 3, 7, 5, 9)
#
# d = []
# for i in range(0, len(Bottom)):
# sum = Bottom[i] + Center[i]
# d.append(sum)
#
# width = 0.8 # 设置条形图一个长条的宽度
# p1 = plt.bar(ind, Bottom, width, color='blue')
# p2 = plt.bar(ind, Center, width, bottom=Bottom, color='green')
# p3 = plt.bar(ind, Top, width, bottom=d, color='red')
#
# plt.legend((p1[0], p2[0], p3[0]), ('Bottom', 'Center', 'Top'), loc=3)
#
# plt.show()
N = 5
product1 = np.array([10, 20, 30, 40, 50])
product2 = np.array([20, 20, 20, 20, 20])
product3 = np.array([20, 20, 20, 20, 20])
line = np.array([10, 15, 5, 20, 36])
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars: can also be len(x) sequence width = 0.35 # the width of the bars: can also be len(x) sequence
color_list = ['#222A77', '#6C71AA', '#E1BC95', '#102A77', '#6CB1AA', '#CCBC95']
p1 = plt.bar(ind, product1, width, color='#222A77', alpha=0.8) # 堆叠柱状图
p2 = plt.bar(ind, product2, width, bottom=product1, color='#6C71AA', alpha=0.8) prod_legend = []
p3 = plt.bar(ind, product3, width, bottom=product2+product1, color='#E1BC95', alpha=0.8) for i in range(len(product_list)):
ax = None
# plt.ylabel('Scores') bottom = np.zeros(max_x_count)
plt.title('Scores by group and gender') if i == 0:
plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5')) ax = ax1.bar(loc, product_list[i]['data'], width, color=color_list[i], alpha=0.8)
# plt.yticks(np.arange(0, 81, 10)) 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])
ax1 = plt.legend((p1[0], p2[0], p3[0]), ('product1', 'product2', 'product3')) # 坐标轴
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)
# 画折线图 # 画折线图
ax2 = ax1.twinx() # 最大收益
line = ax2.plot(ind, line, color='#C6A774', marker='', ms=1000, label=u"总体收益") 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')
plt.gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
plt.show() plt.show()
if __name__ == '__main__': if __name__ == '__main__':
draw_stacked_column_chart() 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)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment