数据分析第四节课Matplotlib作业
2020-08-20 11:24
477 查看
练习一
绘制班级的身高分布图形
height = [160,163,175,180,176,177,168,189,188,177,174,170,173,181]
代码如下:
from matplotlib import pyplot as plt import matplotlib font = {'family':'SimHei', 'weight':'bold', 'size':12 } matplotlib.rc("font",**font) #设置字体格式 height = [160,163,175,180,176,177,168,189,188,177,174,170,173,181] cha = max(height) - min(height) #求极差 bins = round(cha)//5 #求组数 plt.hist(height,bins,rwidrh=0.8) plt.xlabel('身高') plt.ylabel('人数') plt.title('身高直方图') plt.savefig('身高分布直方图') plt.show()
绘图如下:
练习二
使用栅栏对象绘制子图长宽比例不同的综合图如下:
代码如下:
from matplotlib import pyplot as plt import matplotlib import random font = {'family':'SimHei', 'weight':'bold', 'size':12 } matplotlib.rc("font",**font) #设置全局字体格式 fig = plt.figure() width = (3,1) #设置第0列和第1列子图宽度比例 height = (1,2) #设置第0列和第1列子图高度比例 #创建2*2的栅栏对象 gs =fig.add_gridspec(2,2,width_ratios=width,height_ratios=height) #绘制条形图 ax1 = fig.add_subplot(gs[0,0]) #添加子图 x1 = ["变身特工","美丽人生","鲨海逃生","熊出没"] y1 = [2358,399,2358,362] ax1.bar(x1,y1) ax1.set_xlabel('电影') ax1.set_ylabel('票房') ax1.set_title('电影票房数据') #绘制直方图 ax2 = fig.add_subplot(gs[1,0]) x2 = [160,166,175,180,176,177,168,189,188,177,174,170,169,181] ax2.hist(x2,round(max(x2)-min(x2))//5) ax2.set_xlabel('身高') ax2.set_ylabel('人数') ax2.set_title('各身高区间的人数及其体重') #绘制散点图与直方图共用一个子图 ax3 = ax2.twinx() x3 = [random.randint(160,190) for i in range(10)] y3 = [random.randint(0,100) for i in range(10)] ax3.scatter(x3,y3,color='g') ax3.set_ylabel('体重') #绘制线形图 ax4 = fig.add_subplot(gs[1,1]) ax4.plot(range(10),range(10)) fig.subplots_adjust(wspace=0.5,hspace=0.5) #调整子图间距 plt.savefig('综合图') plt.show()
绘得图如下:
相关文章推荐
- 基于matplotlib对iris数据集进行数据分析
- Python seaborn matplotlib 对数据进行图表分析
- 学习Python数据分析(1)----numpy,Pandas,matplotlib,scipy 的安装
- 【python数据挖掘课程】十二.Pandas、Matplotlib结合SQL语句对比图分析
- 数据分析与挖掘入门——学习笔记(五)Matplotlib的简介、折线图与基础绘画功能
- 数据分析(使用matplotlib,seaborn,ploty进行可视化)——1
- 【利用python进行数据分析-学习记录】python-matplotlib中Basemap插件的安装
- Matplotlib临摹作业(西安交大国家艺术基金数据可视化培训第9天)
- matplotlib数据分析
- 爬虫数据分析------Pandas和Matplotlib
- Python数据分析与应用 第三章 Matplotlib数据可视化基础 (折线图) 上
- Python进阶(三十九)-数据可视化の使用matplotlib进行绘图分析数据
- 数据分析matplotlib
- 利用python中的pandas和matplotlib进行电影数据分析
- 动态可视化 数据可视化之魅D3,Processing,pandas数据分析,科学计算包Numpy,可视化包Matplotlib,Matlab语言可视化的工作,Matlab没有指针和引用是个大问题
- Python数据分析入门 matplotlib数据可视化基础【一】
- 数据分析基础之matplotlib绘制散点图
- 小白学 Python 数据分析(17):Matplotlib(二)基础操作
- python数据分析——matplotlib生成折线图,散点图和直方图
- Python数据分析四剑客:IPython、Numpy、pandas、Matplotlib