任務描述
本關任務:請編寫代碼繪製各類商品房平均銷售價格柱狀圖。
相關知識
爲了完成本關任務,你需要掌握繪製堆積(並列)柱狀圖。
繪製堆積柱狀圖
bar
函數調用方式如下所示:matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)
要想繪製堆積柱狀圖,可通過設置第一個參數
x
的值來使得柱形錯位顯示,x
的每一個元素表示柱形的中間位置,示例代碼如下所示:import numpy as np import matplotlib.pyplot as plt # A班計算機程序設計課5個小組的平均成績柱狀圖 A_means_score = np.array([90, 85, 77, 82, 79]) # B班計算機程序設計課5個小組的平均成績柱狀圖 B_means_score = np.array([67, 82, 87, 92, 95]) index = np.arange(5) bar_width = 0.35 plt.bar(index, A_means_score, bar_width, # A班x軸數據起始位置爲index序列 alpha=0.4, color='b') plt.bar(index+bar_width, B_means_score, bar_width, #B班x軸起始位置與A班數據錯開 alpha=0.4, color='r') x_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4', 'Group 5'] plt.xticks(index+bar_width/2, x_labels) # index+bar_width/2 使得標籤居中顯示 plt.show()
輸出圖像如下所示:
若是有多組數據,則可通過結合列表以及循環結構進行繪圖控制。、、
編程要求
國家統計局統計的商品房的平均銷售價格如下表所示:
請編寫代碼繪製六類商品房平均銷售價格柱狀圖,具體編程要求如下:
- 柱狀圖柱形寬度設置爲
0.8
,顏色分別爲列表colors
中對應的RGB值;- 橫軸座標軸範圍爲
[-1,98]
,第一組數據的柱形起始位分別爲1,7,13,...91
,間隔爲6。- 橫軸標籤爲年份,從
2000
至2015
,旋轉角度爲45
度,標籤位置位於6組數據正中間
;- 縱軸座標軸範圍爲
[1450, 15300]
,軸刻度爲2000,4000,...14000
,刻度間隔爲2000
;- 添加圖例,圖例標籤如列表
legend_labels
所示,位置位於左上角
;- 添加標題
'Selling Prices of Six Types of Housing'
- 存儲輸出圖像,圖像名字爲
picture/step2/fig2.png
;完成圖應如下圖所示:
測試說明
平臺將運行用戶補全的代碼文件,並將存儲的
picture/step2/fig2.png
圖像與標準答案圖像比較,然後判斷用戶編寫代碼是否正確。若畫圖正確,測試集將輸出:
祝賀!圖片與預期輸出一致
; 否則,測試集將輸出:圖片與預期輸出不一致,請繼續努力!
。
# -*- coding: utf-8 -*-
import matplotlib
import re
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_labels=re.findall(r'\b\d+\b',xstring)[::-1]
ylist=[]
for y in ystring:
ylist.append(list(map(float,re.findall(r'[0-9]+\.?[0-9]*',y)))[::-1]) #或者使用y.split()
bar_width = 0.8
xindex=np.arange(1,92,6)
fig, ax = plt.subplots()
for i in range(6):
ax.bar(xindex+bar_width*i, ylist[i], bar_width ,color=colors[i])
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 **********#