matplotlib组合图

代码

 1 #练习:#根据如下数据,绘制如下图形,并保存,保存格式为 JPG。
 2 #折线图数据
 3 import random
 4 import matplotlib.pyplot as plt
 5 import numpy as np
 6 plt.rcParams['font.sans-serif']=['SimHei']
 7 plt.rcParams['axes.unicode_minus']=False
 8 x = range(60)
 9 y_shanghai = [random.uniform(15, 18) for i in x]
10 y_beijing = [random.uniform(1, 5) for i in x]
11 y_guangzhou = [random.uniform(1, 5) for i in x]
12 y_xian = [random.uniform(1, 5) for i in x]
13 #条形折线组合
14 Y2016 = [15600,12700,11300,4270,3620]
15 Y2017 = [17400,14800,12000,5200,4020]
16 labels = ['北京','上海','香港','深圳','广州']
17 #直方图
18 data_hist = np.random.normal(0,1,100)
19 #饼图数据
20 data_pie = ['2000','1500','3000','3000','500']
21 data_labels = ['娱乐','吃饭','房租','购物','剩余']
22 plt.figure(num=1,figsize=[15,9])
23 ax1 = plt.subplot2grid((3,3),(0,0),rowspan=1,colspan=3)
24 ax2 = plt.subplot2grid((3,3),(1,0),rowspan=1,colspan=2)
25 ax3 = plt.subplot2grid((3,3),(1,2),rowspan=1,colspan=1)
26 ax4 = plt.subplot2grid((3,3),(2,0),rowspan=1,colspan=1)
27 ax5 = plt.subplot2grid((3,3),(2,1),rowspan=1,colspan=1)
28 ax6 = plt.subplot2grid((3,3),(2,2),rowspan=1,colspan=1)
29 ax1.plot(x,y_shanghai)
30 ax1.plot(x,y_beijing)
31 ax1.legend(['北京','上海'],loc=5)
32 ax1.set_yticks(np.arange(2.5,20,2.5))
33 
34 # ax1.set_yticklabels([2.5,5.0,7.5,10,12.5,15,17.5])
35 explode = [0,0,0,0,0.2]
36 ax2.pie(data_pie,explode=explode,labels=labels,autopct='%1.1f%%')
37 
38 # ax2.figure(figsize=(10,5))ax2.axis('auto')
39 ax2.legend(data_labels,loc='upper left',ncol=2)
40 ax3.plot(x,y_guangzhou,color='purple',linestyle=':')
41 ax3.legend(['广州'],loc=1)
42 
43 # ax3.axis([1.0,])
44 ax3.set_yticks(np.arange(1.0,5.5,0.5))
45 
46 # ax3.set_yticklabels([1.0,1.5,2,2.5,3,3.5,4,4.5,5.0])
47 ax4.hist(data_hist,bins=20,facecolor='y',edgecolor='k')
48 ax4.set_yticks(range(0,16,2))
49 ax4.set_yticklabels([0,2,4,6,8,10,12,14])
50 ax4.legend(['高斯分布'],loc=1)
51 ax5.bar(labels,Y2016,color='r')
52 ax5.plot(labels,Y2017,color='purple')
53 ax5.legend(['Y2016亿万资产家庭数','Y2016亿万资产家庭数'],loc=1)
54 ax5.set_yticks(range(2500,20000,2500))
55 ax6.plot(x,y_xian,color='steelblue',linestyle='--')
56 ax6.set_yticks(np.arange(1.0,5.5,0.5))
57 ax6.set_yticklabels([1.0,1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.0])
58 ax6.legend(['高斯分布'],loc=4)
59 plt.show()

 

posted @ 2022-06-02 09:55  @唯一的你。  阅读(50)  评论(0)    收藏  举报