dhu 数据科学与技术 第6次作业

9 篇文章 5 订阅

一. 简答题(共2题,100分)

  1. (简答题)
    2012—2020年我国人均可支配收入(单位:万元)为[1.47, 1.62, 1.78, 1.94, 2.38, 2.60,2.82, 3.07, 3.21]。按照要求绘制以下图形。

(1)模仿例4-1和例4-3,绘制人均可支配收入折线图(效果如图4-6所示)。数据点用小矩形标记、黑色虚线,并用注解标出最高点,图标题为“Income”,设置坐标轴标题,最后将图形保存为jpg文件。
在这里插入图片描述
图4-6 2012—2020年人均可支配收入(单位:万元)

(2)模仿例4-2,使用多个子图分别绘制人均可支配收入的折线图、箱形图及柱状图(效果如图4-7所示)。

【提示】

(1)本实验准备数据时可以使用Series对象或DataFrame对象。

(2)创建的3个子图分别使用(2,2,1)、(2,2,2)和(2,1,2)作为参数。

(3)使用plt.subplots_adjust()调整子图间距离,以便添加图标题。

在这里插入图片描述
图4-7 多子图展示各年度人均可支配收入
High-speed rail.csv

import matplotlib.pyplot as plt
from pandas import DataFrame
import numpy as np
import matplotlib

# 1)
zhfont1 = matplotlib.font_manager.FontProperties(fname="C:\\python\\SourceHanSansSC-Bold.otf")
income=[1.47, 1.62, 1.78, 1.94, 2.38, 2.60, 2.82, 3.07, 3.21]
data=DataFrame({'income': income},index=['2012','2013','2014','2015','2016','2017','2018','2019','2020'])
plt.plot(income,marker = 's',linestyle = 'dotted',c='black')
plt.ylabel("Income(RMB Ten Thousand)")
plt.xlabel("Year")
plt.title("2012-2020 年人均可支配收入", fontproperties=zhfont1)
my_y_ticks = np.arange(0.0, 3.5, 0.5)
plt.yticks(my_y_ticks)
plt.annotate('Largest!',xy = (8,3.21),xytext = (6.1,2.6),arrowprops = dict(arrowstyle = '->',color = 'r'),color = 'r')
plt.grid()
plt.savefig(fname="C:\\python\\fig1",dpi=200,bbox_inches='tight')

# 2)
fig=plt.figure(figsize = (10,6))

ax1=fig.add_subplot(2,2,1)
data.plot(title = 'Line chart',legend = False,ax = ax1)
plt.xticks(range(0,9),['2012','2013','2014','2015','2016','2017','2018','2019','2020'])
plt.xlim(-0.5,8.5)
plt.xlabel('Year')
plt.ylabel('Income')

ax2=fig.add_subplot(2,2,2)
data.plot(kind='box',title = 'Box-whisker plot',xticks = [],ax = ax2)
plt.xlabel('2012-2020')
plt.ylabel('Income')

ax3=fig.add_subplot(2,1,2)
data.plot(kind='bar',title = 'Bar Chart',ax = ax3)
plt.xticks(range(0,9),['2012','2013','2014','2015','2016','2017','2018','2019','2020'])
plt.xlabel('Year')
plt.ylabel('Income')

plt.subplots_adjust(wspace = 0.5,hspace = 0.5)
plt.show()
  1. (简答题)文件high-speedrail.csv中存放着世界各国高铁的情况,数据格式如表4-6所示,请对世界各国高铁的数据进行绘图分析。
    表4-6 high-speedrail.csv文件的数据格式
    在这里插入图片描述

(1)各国运营里程对比柱状图,标注China为“Longest”,如图4-22所示。

(2)各国运营里程现状和发展堆叠柱状图,如图4-23所示。
在这里插入图片描述
图4-22 各国高铁运营里程柱状图
图4-23.png
图4-23 各国高铁发展情况堆叠图

(3)各国运营里程占比饼图,其中China为扇形离开中心点,如图4-24所示。
在这里插入图片描述

图4-24 各国高铁运营里程分布饼图
【提示】

(1)从文件中读取数据时,使用第一列数据作为index。
data = pd.read_csv(‘High-speed rail.csv’, index_col =‘Country’)
例如,获取中国对应的数据行时,使用data [‘China’]。

import matplotlib.pyplot as plt
import pandas as pd
from pandas import DataFrame
import numpy as np
import matplotlib

data = pd.read_csv('C:\\python\\High-speed rail.csv',index_col='Country')

# 1)
data['Operation'].plot(kind='bar',title = 'Operation Mileage',ylabel='Mileage(km)',rot = 45,xlabel='Country')
plt.annotate('Longest!',xy=(0,20000),xytext=(1,20000),arrowprops=dict(arrowstyle='->',color='r'),c='r')
plt.show()

# 2)
data[['Operation','Under-construction','Planning']].plot(kind='barh',title = 'Global trends of high-speed rail',stacked=True,ylabel='Mileage(km)',rot = 0,legend=False)
plt.ylabel('Country')
plt.xlabel('Mileage(km)')
plt.show()

# 3)
line=pd.read_csv('C:\\python\\High-speed rail.csv')
matplotlib.pyplot.pie(data['Operation'],labels=line['Country'],autopct='%0.1f%%',explode=(0.1,0,0,0,0,0),shadow=True,startangle = 60)
plt.show()
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值