使用Matplotlib模块进行数据可视化
第一关
python
# 请编写代码绘制住宅商品房平均销售价格柱状图
import matplotlib
matplotlib.use("Agg")
# 请在此添加实现代码 #
# ********** Begin *********#
import matplotlib.pyplot as plt
from numpy import *
xstring = '2015 2014 2013 2012 2011 \
2010 2009 2008 2007 2006 \
2005 2004 2003 2002 2001 2000'
ystring = '12914 11826 12997 12306.41 12327.28 \
11406 10608 8378 8667.02 8052.78 \
6922.52 5744 4196 4336 4588 4751'
y = ystring.split()
y.reverse()
y = [float(e) for e in y]
xlabels = xstring.split()
xlabels.reverse()
x = range(len(xlabels))
plt.xticks(x, xlabels, rotation = 45)
plt.yticks(range(4000,13500,1000))
plt.ylim(4000,13500)
plt.bar(x, y, color = '#800080')
plt.savefig('picture/step1/fig1.png')
# ********** End **********#
第二关
python
# 请编写代码绘制住宅商品房平均销售价格柱状图
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'
legend_labels = ['Commercial housing', 'Residential commercial housing',
'high-end apartments', 'Office Building', 'Business housing', 'Others'] #图例标签
colors = ['#ff7f50', '#87cefa', '#DA70D6', '#32CD32', '#6495ED', '#FF69B4'] #指定颜色
# 请在此添加实现代码 #
# ********** Begin *********#
xlabels = xstring.split() # 年份切分
xlabels.reverse() # 年份序列倒序排列,从小到大
x = np.arange(1, n*len(xlabels), n) #x轴条形起始位置
w = 0.8 #条形宽度设置
for i in range(n):
y = ystring[i].split()
y.reverse()
y = [float(e) for e in y] #将划分好的字符串转为float类型
plt.bar(x+i*w, y, width = w, color = colors[i]) #以指定颜色绘制柱状图
plt.ylim([1450, 15300]) #指定y轴范围
plt.yticks(range(2000,15000,2000)) #指定y轴刻度
plt.xlim([-1,98])
plt.xticks(x+w*2.5, xlabels, rotation = 45) #添加x轴标签,旋转40度
plt.legend(legend_labels, loc = 'upper left') #添加图例,位置为左上角
plt.title('Selling Prices of Six Types of Housing')
plt.savefig('picture/step2/fig2.png') #存储图像
# ********** End **********#
第三关
python
# 请绘制育龄妇女的受教育程度分布饼图
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'] #指定楔形颜色
womenCount = [2052380, 11315444, 20435242, 7456627, 3014264, 1972395, 185028]
explode = [0,0,0.1,0,0,0,0] # 确定突出部分
plt.pie(womenCount, explode=explode, labels=labels, shadow=True,colors=colors)
plt.axis('equal') # 用于显示为一个长宽相等的饼图
plt.savefig('picture/step3/fig3.png')
# ********** End **********#
第四关
python
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 *********#
x = np.arange(len(labels))
birth = np.array(birthMen) + np.array(birthWomen)
live = np.array(liveMen) + np.array(liveWomen)
plt.figure(figsize=[14,5]) #设置画布大小
plt.subplot(121)
birthrate = (1.0*live) / (1.0*np.array(womenCount))
plt.plot(x, birthrate, 'r')
plt.xticks(x, labels)
plt.subplot(122)
liverate = (1.0*live) / (1.0*birth) * 100
plt.plot(x, liverate, 'b')
plt.xticks(x, labels)
plt.savefig('picture/step4/fig4.png')
# ********** End **********#
网络机器人相关法律责任
第一关
python
from urllib import request
import sys
def Evidence(url):
try:
response = request.urlopen(url)
status_code = response.getcode()
print(f"Status: {status_code} OK ")
except Exception as e:
print(e)
第二关
python
import requests
def Evidence(url):
try:
response = requests.get(url)
if response.status_code == 200:
print("Status: 200")
else:
print(f"Status: {response.status_code}")
except requests.RequestException as e:
print("url请求失败")
第三关
python
import re
def Evidence(text):
# 定义匹配Email地址的正则表达式
email_pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
# 使用re.match进行匹配
match = re.match(email_pattern, text)
# 输出匹配结果,如果匹配不到输出None
if match:
print(match)
else:
print(None)
后面三关
后面三关域名过期做不了
网页抓取及信息提取
域名过期做不了