Matplotlib

一、安装和导入
1.1安装Matplotlib库
pip install matplotlib
1.2导入Matplotlib库
import matplotlib.pyplot as test1
二、绘图基础必备
2.1figure对象创建画布
对应代码:

figure(num,figsize,dpi,facecolor,edgecolor,frameon)
num:图形编号或名称

figsize:绘图对象的宽和高

dpi:绘图对象的分辨率,默认80

facecolor:背景颜色

edgecolor:边框颜色

frameon:是否显示边框

演示代码:

import matplotlib.pyplot as test1
# 创建画布
test1.figure(num="商品房销售数据表",figsize=(3,2),facecolor="r")
# 绘制空白图形
test1.plot()
# 显示
test1.show()
实现结果:

 

2.2 subplot()函数划分子图
对应代码:

subplot(行数,列数,子图序号)
演示代码:

import matplotlib.pyplot as test1
# 创建画布
test1.figure(num="商品房销售数据表")
# 当subplot()函数中三个参数都小于10,逗号可以省略
test1.subplot(2,2,1)
test1.subplot(2,2,2)
test1.subplot(2,2,3)
test1.subplot(2,2,4)
# 显示
test1.show()
实验结果:

 

2.3 添加标题
对应代码:

#添加全局标题 x:标题位置x坐标 y:标题位置y坐标 color:标题颜色 backgroundcolor:标题背景颜色
#fontsize:标题字体大小 fontweight:字体粗细 fonstyle:设置字体类型
#horizontalalignment:标题水平对其方式 verticalalignment:标题垂直对其方式
suptitle(标题名)
#添加子标题 loc:标题位置 rotation:标题文字旋转角度 color:标题颜色 fontsize:标题字体大小
# fontweight:字体粗细 fonstyle:设置字体类型 fontdice:设置字典参数
#horizontalalignment:标题水平对其方式 verticalalignment:标题垂直对其方式
title(标题名)
演示代码:

import matplotlib.pyplot as test1
# 解决中文输出不了的问题
test1.rcParams['font.sans-serif'] = ['SimHei']
test1.figure("商品房销售数据表")
# 当subplot()函数中三个参数都小于10,逗号可以省略
test1.subplot(2,2,1)
test1.title("子表1")
test1.subplot(2,2,2)
test1.title("子表2")
test1.subplot(2,2,3)
test1.title("子表3")
test1.subplot(2,2,4)
test1.title("子表4")
# 显示
test1.suptitle("全局标题",color="r")
test1.show()
实验结果:

 

2.4 tight_layout()函数调整子图
对应代码:

tight_layout(rect=[left,bottom,right,top])
演示代码:

import matplotlib.pyplot as test1
# 解决中文输出不了的问题
test1.rcParams['font.sans-serif'] = ['SimHei']
test1.figure("商品房销售数据表")
# 当subplot()函数中三个参数都小于10,逗号可以省略
test1.subplot(2,2,1)
test1.title("子表1")
test1.subplot(2,2,2)
test1.title("子表2")
test1.subplot(2,2,3)
test1.title("子表3")
test1.subplot(2,2,4)
test1.title("子表4")
# 显示
test1.suptitle("全局标题",color="r")
test1.tight_layout(rect=[0,0,1,0.9])
test1.show()
实验结果:

 

三、绘制散点图
3.1scatter()函数
对应代码:

#x:数据点的x坐标 y:数据点的y坐标 scale:数据点大小 color:数据点颜色 marker:数据点样式
#laber:图例文字
scatter(x,y,scale,color,marker,laber)
演示代码:

import matplotlib.pyplot as test1
import numpy as num
#设置字体,解决中文输出不了的问题
test1.rcParams['font.sans-serif'] = ['SimHei']
n = 500
x=num.random.normal(0,1,n)
y=num.random.normal(0,1,n)
#k表示黑色
test1.scatter(x,y,color="k")
test1.title("商品房销售数据")
test1.show()
实验结果:

 

3.2、其他常用函数
对应代码:

#x:文字x坐标 y:文字y坐标 s:显示的文字 fontsize:文字大小 color:文字颜色
#添加文字
text(x,y,s,fontsize,color)
#正常显示负号
rcParams["axes.unicode_minus"]=False
#设置x轴标签
xlabel(x,y,s,fontsize,color)
#设置y轴标签
ylabel(x,y,s,fontsize,color)
#设置x轴坐标范围
xlim(xmin,xmax)
#设置y轴坐标范围
ylim(ymin,ymax)
#设置刻度文字符号
tick_params(labersize)

演示代码:

import matplotlib.pyplot as test1
import numpy as num
#设置字体,解决中文输出不了的问题
test1.rcParams['font.sans-serif'] = ['SimHei']
# 设置正常显示负号
test1.rcParams["axes.unicode_minus"]=False
x=num.array([137.97,104.50,100,124.32,79.20,99,124,114,106.69,138.05,53.75,46.91,68,63.02,81.26,82.61])
y=num.array([145,110,93,116,65.32,104,118,91,62,133,51,45,78.50,69.65,75.69,95.30])
#绘制数据点
test1.scatter(x,y,color="r")
test1.xlim(0,200)
test1.ylim(0,200)
test1.title("商品房销售数据",fontsize=16,color="b")
test1.xlabel("面积(平方米)",fontsize=16)
test1.ylabel("价格(万元)",fontsize=16)
test1.show()

实验结果:

 

四、折线图和柱形图
4.1plot()函数
对应代码:

#x:数据点的x坐标 y:数据点的y坐标 color:数据点颜色 marker:数据点样式 label:图例文字
#linewidth:折现的宽度 markersize:数据点的大小
plot(x,y,color,marker,label,linewidth,markersize)
演示代码:

import matplotlib.pyplot as test1
import numpy as num
#设置字体,解决中文输出不了的问题
test1.rcParams['font.sans-serif'] = ['SimHei']
# 设置正常显示负号
test1.rcParams["axes.unicode_minus"]=False
n=50
y1 = num.random.randint(10,30,n)
y2 = num.random.randint(30,40,n)
#绘制数据点
test1.plot(y1,color="r")
test1.plot(y2,color="r")
test1.xlim(0,50)
test1.ylim(0,50)
test1.show()
实验结果:

 

4.2 bar()函数
对应代码

#left:x坐标 height:高度 width:每条数据宽度 facecolor:条形颜色 edgecolor:条形边框颜色
#label:图例文字
bar(left,height,width,facecolor,edgecolor,label)
演示代码:

import matplotlib.pyplot as test1
import numpy as num
#设置字体,解决中文输出不了的问题
test1.rcParams['font.sans-serif'] = ['SimHei']
# 设置正常显示负号
test1.rcParams["axes.unicode_minus"]=False
n=50
y1 = num.random.randint(10,30,n)
#绘制数据点
test1.bar(range(len(y1)),y1,0.9,facecolor="b",edgecolor="r")
test1.xlim(0,50)
test1.ylim(0,50)
test1.show()
实验结果:

 

总结
Matplotlib数据可视化是python的典型库,在绘图领域有着方便的绘表功能,可以让你方便的设计二维和三维数据,也提供了强大的绘表辅助功能,在数据可视化和数据分析中具有重大的地位。

matplotlib数据可视化还有很多没有总结到的,总结不到位的后续补充

posted @   yltzz  阅读(38)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示