# 请编写代码绘制住宅商品房平均销售价格柱状图
import matplotlib
matplotlib.use("Agg")
# 请在此添加实现代码 #
# ********** Begin *********#
import numpy as np
import matplotlib.pyplot as plt
#\是为了让数字换行
sale_price='12914 11826 12997 12306.41 \
12327.28 11406 10608 8378 8667.02\
8052.78 6922.52 5744 4196 4336 4588 4751'
year='2015 2014 2013 2012 2011 \
2010 2009 2008 2007 2006 \
2005 2004 2003 2002 2001 2000'
#处理x,y的数据,需要分割,并且进行排序
#先排序,再转化成int型列表
y=sale_price.split()
y.reverse()
y=[float(e) for e in y]
x=year.split()
x.reverse()
x=[int(e) for e in x]
#
x_labels=[]
for i in range(2000,2016):
x_labels.append(i)
plt.xticks(x,x_labels,rotation=45) #横轴标签 旋转rotation
'''
plt.yticks(range(4000,13500,1000))
plt.ylim(4000,13500)
'''
y_labels=[]
for j in range(4000,13500,1000):
y_labels.append(j)
plt.yticks(y_labels)
plt.ylim(4000,13500)
plt.bar(x, y, color='#800080')
plt.savefig("picture/step1/fig1.png")
# ********** End **********#
# -*- coding: utf-8 -*-
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
xstring = '2015 2014 2013 2012 2011 \
2010 2009 2008 2007 2006 \
2005 2004 2003 2002 2001 2000' #x轴标签
n = 6
ystring = ['']*n #y轴对应的6组数据
ystring[0] = '6793 6324 6237 5790.99 5357.1 5032 4681 3800 3863.9 3366.79 3167.66 2778 2359 2250 2170 2112'
ystring[1] = '6473 5933 5850 5429.93 4993.17 4725 4459 3576 3645.18 3119.25 2936.96 2608 2197 2092 2017 1948'
ystring[2] = '15157 12965 12591 11460.19 10993.92 10934 9662 7801 7471.25 6584.93 5833.95 5576 4145 4154 4348 4288'
ystring[3] = '12914 11826 12997 12306.41 12327.28 11406 10608 8378 8667.02 8052.78 6922.52 5744 4196 4336 4588 4751'
ystring[4] = '9566 9817 9777 9020.91 8488.21 7747 6871 5886 5773.83 5246.62 5021.75 3884 3675.14 3488.57 3273.53 3260.38'
ystring[5] = '4845 5177 4907 4305.73 4182.11 4099 3671 3219 3351.44 3131.31 2829.35 2235 2240.74 1918.83 2033.08 1864.37'
labels = ['Commercial housing', 'Residential commercial housing',
'high-end apartments', 'Office Building', 'Business housing', 'Others'] #图例标签
colors = ['#ff7f50', '#87cefa', '#DA70D6', '#32CD32', '#6495ED', '#FF69B4'] #指定颜色
# 请在此添加实现代码 #
# ********** Begin *********#
#处理x/y的数据
x=xstring.split()
x.reverse()
x_labels=[int(e) for e in x]
y=[]
'''
方法一:
'''
for i in range(6):
ystring[i]=ystring[i].split()
ystring[i].reverse()
y.append(list(map(float,ystring[i])))
'''
方法二:
for e in ystring:
y.append(list(map(float,re.findall(r'[0-9]+\.?[0-9]*',e)))[::-1])
'''
bar_width=0.8
#设置纵横坐标
xindex=np.arange(1,92,6)#第一组数据的柱形起始位分别为 1,7,13,...91 ,间隔为 6
fig, ax = plt.subplots()
'''
fig,ax = plt.subplots()等价于:
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
'''
for i in range(0,6):
ax.bar(xindex+bar_width*i, y[i], bar_width ,color=colors[i])
#x轴数据起始位置为index序列
ax.set_xlim(-1,98) #闭区间
plt.xticks(xindex+bar_width*2.5,x_labels,rotation=45)
ax.set_ylim(1450,15300)#坐标轴范围
plt.yticks(np.arange(2000,16000,2000))#轴刻度
plt.legend(labels,loc='upper left')
plt.title('Selling Prices of Six Types of Housing')
plt.savefig('picture/step2/fig2.png')
# ********** End **********#
# 请绘制育龄妇女的受教育程度分布饼图
import matplotlib
matplotlib.use("Agg")
# 请在此添加实现代码 #
# ********** Begin *********#
import matplotlib.pyplot as plt
labels=['none', 'primary', 'junior', 'senior',
'specialties', 'bachelor', 'master']
colors=['red','orange','yellow','green','purple','blue','black']
sizes=[2052380,11315444,20435242,7456627,3014264,1972395,185028]
explode=(0,0,0.1,0,0,0,0)
plt.pie(sizes, explode=explode, labels=labels,colors=colors,
shadow=True)
plt.axis('equal') # 用于显示为一个长宽相等的饼图
plt.show() #展示图像
plt.savefig("picture/step3/fig3.png")
# ********** End **********#
编程要求
2010 年全国按受教育程度分的 15 - 64 岁妇女平均活产子女数和平均存活子女数如下表所示:
请编写代码绘制图像,详细数据在右侧代码中已给出,具体编程要求如下:
包括两幅子图,左图是不同受教育程度育龄妇女生育子女的平均个数统计(按存活子女数统计),右图是不同受教育程度育龄妇女生育子女的存活比例(即存活子女数/活产子女数);
画布大小的宽高分别为 14, 5 英尺;
两幅子图都需添加横轴标签,标签列表labels在右侧代码中已给出;
左图线条颜色设置为红色red,线性采用默认设置;
右图线条颜色设置为蓝色blue,线性采用默认设置;
请存储输出图像,图像名字为 picture/step4/fig4.png 。
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
labels = ['none', 'primary', 'junior', 'senior', 'specialties', 'bachelor', 'master'] # 标签
womenCount = [2052380, 11315444, 20435242, 7456627, 3014264, 1972395, 185028]
birthMen = [2795259, 12698141, 13982478, 2887164, 903910, 432333, 35915]
birthWomen = [2417485, 11000637, 11897674, 2493829, 786862, 385718, 32270]
liveMen = [2717613, 12477914, 13847346, 2863706, 897607, 429809, 35704]
liveWomen = [2362007, 10854232, 11815939, 2480362, 783225, 384158, 32136]
# 请在此添加实现代码 #
# ********** Begin *********#
plt.figure(figsize=[14,5])
plt.subplot(121) # 绘图区域被分为 1 行 2 列,接下来绘制第一幅图
#图一
x = np.arange(len(labels))#x指的是横坐标间隔
plt.xticks(x,labels)
aver_livechild=[2.47,2.06,1.26,0.72,0.56,0.41,0.37]
plt.plot(x,aver_livechild,'r-')
#图二
plt.subplot(122)
x = np.arange(len(labels))
plt.xticks(x,labels)
y=(np.array(liveMen)+np.array(liveWomen))*100/(np.array(birthMen)+np.array(birthWomen))*1.0
plt.plot(x,y,'b-')
plt.savefig('picture/step4/fig4.png')
# ********** End **********#