机器学习之Matplotlib 基础
2019-07-22 10:29
369 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_40242609/article/details/96832211
Python机器学习工具包之 matplotlib
1 散点图 plt.sctter
import numpy as np import matplotlib.pyplot as plt # 散点图 用来描述变量之间的相关性 # height = [161, 170, 182, 175, 173, 165] # weight = [50, 58, 80, 70, 69, 55] # plt.scatter(height, weight) # plt.show() # 不相关 # N = 1000 # x = np.random.randn(N) # y = np.random.randn(N) # plt.scatter(x,y) # plt.show() # 正相关 y 是由x 生成的 # N = 1000 # x = np.random.randn(N) # y = np.random.randn(N)+x # 负相关只需在x前面加上-号 # plt.scatter(x,y) # plt.show() # 总结 # 散点图的参数 plt.scatter(x,y,s,c,marker,alpha) x,y表示两种数据,s表示点的面积(大小),marker点的形状 # alpha 透明度
2 折线图 plt.plot
import numpy as np import matplotlib.pyplot as plt # 折线图 x = np.linspace(-10,10,10) y = x**2 plt.plot(x,y) plt.show() # plt.plot_date() 横坐标是时间的 # 折线图1 plt.plot_date(x1,y1,linestyle,color,marker) # linestyle 是线型,color 颜色 # marker点的形状 # 折线图2 plt.plot_date(x2,y2) #plt.plot() # 可以绘制 图1 图2 在同一张图片上
3 条形图 plt.bar()
# 条形图 plt.bar import numpy as np import matplotlib.pyplot as plt N = 5 y = [10, 20, 30,< 3ff7 /span> 40, 50] index = np.arange(N) # 参数 plt.bar(x,height,color,width) x 横坐标,height 柱状图高度,color颜色 # width宽度 # p1 = plt.bar(x=index, height=y) # 水平条形图 p2 = plt.barh(y=index, width=y) plt.show()
4 饼形图 plt.pie()
import numpy as np import matplotlib.pyplot as plt labels = list('abcd') fraces = [15,50,30,5] plt.pie(x = fraces,labels = labels,autopct='%.0f%%',explode=[0.05,0.05,0,0],shadow= True) # 参数 x 数值数组,labels 每个数组对应的标签,autopct 饼图的每一块距离圆心的距离,shadow 阴影(体现立体感) plt.show()
5 箱型图 plt.box()
import numpy as np import matplotlib.pyplot as plt # 箱型图 是一种显示一组数据分散情况资料的统计图 # 上边缘、上四分位数、中位数、下四分位数、下边缘、异常值。 np.random.seed(100) data = np.random.normal(size=(1000,4),loc = 0,scale= 1 ) labels = list('ABCD') plt.boxplot(x = data,labels = labels) # size = 1000时候 plt.boxplot(data,sym= 'o',whis = 1.5)# sym 调整异常值的形状, # whis 是上边缘或者是下边缘到上四分位数或者下四分位数的距离,默认是1.5 plt.show()
6 matplotlib风格
# 颜色调整 # 内置颜色缩写 # b = blue # g = green # r = red # c = cyan # m = magenta # y = yellow # k = black # w = white # 其他调整颜色表示方法 # 灰色阴影 # RGB # 十六进制 # 点、线的样式 点型 marker/线型 linestyle '--' 虚线,'-.'点线,':' 点划线,'-' 实线 # 样式字符串 '颜色点型线型' import numpy as np import matplotlib.pyplot as plt y = np.arange(1, 5) plt.plot(y,'ro--') # 样式字符串 plt.plot(y+1,'o-') # plt.plot(y, color='g',marker = 'o',linestyle = '--') # 内置颜色 # plt.plot(y + 1, color='0.5',marker = 'D',linestyle = '-.') # 灰度 plt.plot(y + 2, color=(0.5, 0.15, 0.90),marker = '^',linestyle = ':') # RGB元组 plt.plot(y + 3, color='#FF00FF',marker = 'p',linestyle = '-') # 十六进制 plt.show()
7 子图
# 子图 # matplotlib 对象简介 # FigureCanvas 画布 # Figure 图 # Axes 坐标轴 # 子图画法 # fig = plt.figure() # figure 实例 # 可以添加Axes实例 # ax = fig.add_subplot(111) # 返回Axes实例 # 111 # 参数1 子图总行数 # 参数2 子图总列数 # 参数3 子图位置 # 实例 import matplotlib.pyplot as plt import numpy as np x = np.arange(1,100) fig = plt.figure() # ax1 = fig.add_subplot(221) plt.subplot(221) plt.plot(x) ax2 = fig.add_subplot(222) ax3 = fig.add_subplot(223) ax4 = fig.add_subplot(224) # ax1.plot(x,x) ax2.plot(x,-x) ax3.plot(x,x**x) ax4.plot(x,np.log(x)) plt.show() # figure 就相当于一块画布,axes对象就是画布上由坐标轴确认的一块快小的画图区域
7 多图
# 多图 import matplotlib.pyplot as plt fig1 = plt.figure() ax1 = fig1.add_subplot(221) ax1.plot([1,2,3],[3,2,1]) fig2 = plt.figure() ax2 = fig2.add_subplot(222) ax2.plot([2,2,4],[4,5,6]) plt.show()
8 网格
# 网格 import numpy as np import matplotlib.pyplot as plt # plt 封装好的 # y = np.arange(1,5) # plt.plot(y,y*2) # plt.grid(True,color = 'r',linewidth = 5,linestyle = '--') # 网格定制化 颜色,线宽度 # plt.show() # 面向对象的方式绘制 网格 x = np.arange(0,10,1) fig = plt.figure() ax = fig.add_subplot(221) ax.plot(x,x*2) ax.grid(True,linewidth = 2,color = 'g',linestyle = '--') plt.show()
9 图例
# 图例 import matplotlib.pyplot as plt import numpy as np # plt 方式 # x = np.arange(1,11,1) # plt.plot(x,x*2,label = 'normal') # label 线名 # plt.plot(x,x*3,label = 'fast') # label 线名 # plt.plot(x,x*4,label = 'faster') # label 线名 # plt.legend(loc = 4,ncol = 3) #常用参数 位置参数loc = 0 best/自适应位置(默认) loc = 1 右上角,loc = 2 左上角,loc = 3 左下角,loc = 4 右下角 # # ncol 几列 # plt.show() # 面向对象方式 x = np.arange(1,11,1) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(x,x*2,label = 'normal') # label 线名 ax.plot(x,x*3,label = 'fast') # label 线名 ax.plot(x,x*4,label = 'faster') # label 线名 ax.legend(loc = 4,ncol = 3) #常用参数 位置参数loc = 0 best/自适应位置(默认) loc = 1 右上角,loc = 2 左上角,loc = 3 左下角,loc = 4 右下角 plt.show()
10 坐标轴
import matplotlib.pyplot as plt import numpy as np # 坐标轴的范围 x = np.arange(-10, 11, 1) # plt.axis([-10,-10, 0, 100]) # 也可以用plt.xlim 和 plt.ylim 分别调整x和y plt.xlim([-5,10]) # plt.ylim([0,60]) plt.plot(x, x * x) plt.show() # 坐标轴的刻度 import numpy as np import matplotlib.pyplot as plt from datetime import datetime import matplotlib as mp x = np.arange(10) plt.locator_params('x',nbins = 20) # 横纵坐标分别有多少格 'x'只调整x轴 ax = plt.plot(x,x) plt.show() # 添加坐标轴 import numpy as np import matplotlib.pyplot as plt # plt 方式 # x = np.arange(2,20,1) # y1 = x*x # y2 = np.log(x) # plt.plot(x,y1) # plt.twinx() # 新 添加一条坐标轴 # plt.plot(x,y2,'r--') # 在新添加的坐标轴上画出x,y2 # plt.show() # 面向对象方式 x = np.arange(2,20,1) y1 = x*x y2 = np.log(x) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(x,y1) ax.set_ylabel('y1title') ax2 = ax.twinx() # 返回一个坐标轴 ax2.plot(x,y2,'go--') ax2.set_ylabel('y2title') # 设置y标签 plt.show()
11 注释
# 注释 import matplotlib.pyplot as plt import numpy as np x = np.arange(-10,11,1) y = x*x # plt.plot(x,y) fig = plt.figure() ax = fig.add_subplot(111) ax.annotate('this is the bottom',xy = (0,1),xytext = (0,20),arrowprops = dict(facecolor = 'r',frac = 0.5,headwidth = 20,width = 10)) ax.plot(x,y) # plt.annotate('this is the bottom',xy = (0,1),xytext = (0 ,20),arrowprops = dict(facecolor = 'r',frac = 0.5,headwidth = 20,width = 10)) # 参数1 注释文本,参数2 xy 箭头起始坐标,参数3 xytext 文本起始坐标 # 参数4 arrowprops = dict(facecolor = '箭头颜色',frac = 箭头比例,headwidth = 箭头宽度,width = 箭身宽度) plt.show() # 公式 # TeX公式 # 通过TeX 系统处理公式 import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) ax.set_xlim = ([1, 7]) ax.set_ylim = ([1, 5]) ax.text(2, 4, r'$\alpha_i \beta_j \lambda \ omega $', fontsize=25) # 公式的斜字体需要在字符串前后加上$将公式包括起来,至于公式的内容需要查阅matplotlib官方文档,公式字符串前面r的作用是防止字符串发生转义 plt.show() # 文字 import matplotlib.pyplot as plt import numpy as np x = np.arange(-10, 11) y = x * x plt.plot(x, y) plt.text(-2, 20, 'function y = x^2', family='serif', size=10, color='r',style = 'italic',weight = 20,bbox = dict(facecolor = 'r',alpha = 0.5)) # 参数1 文字起始点的横坐标,参数2 文字起始点的纵坐标,参数3 文本 参数4 family 字体 # 参数5 size 字体大小 参数6 color 颜色 参数7 sytle = 'italic'斜体 参数8 weight 字体粗度 参数9 bbox = dict(facecolor = '颜色缩写',alpha = 透明度) plt.text(-2, 40, 'function y = x^2', family='fantasy', size=10, color='g') # 参数1 文字起始点的横坐标,参数2 文字起始点的纵坐标,参数3 文本 # fig = plt.figure() # ax = fig.add_subplot(111) # ax.text(0, 40, 'function y = x^2', family='serif') # ax.plot(x,y) plt.show()
12 区域填充
# 区域填充 fill, fill_between import numpy as np import matplotlib.pyplot as plt x = np.linspace(0,5*np.pi,1000) y1 = np.sin(x) y2 = np.sin(2*x) # plt.fill(x,y1,'b',alpha = 0.3) # plt.fill(x,y2,'r',alpha = 0.3) # fill 填充的是曲线和x轴之间区域 # # plt.show() # 面向对象的方式使用fill_between fig = plt.figure() ax = plt.gca() ax.plot(x,y1,color = 'black') ax.plot(x,y2,color = 'y') ax.fill_between(x,y1,y2,where = y1>y2,facecolor= 'blue',alpha = 0.5,interpolate=True) # fill_between() where 条件表达式 # interpolate = True 交叉值附近精确填充 ax.fill_between(x,y1,y2,where = y2>= y1,facecolor= 'g',alpha = 0.5) plt.show()
13 区域
# 形状 需要用到matplotlib patches类 import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as mpatches # fig, ax = plt.subplots() # xy1 = np.array([0.2, 0.2]) # xy2 = np.array([0.2, 0.8]) # xy3 = np.array([0.8, 0.2]) # xy4 = np.array([0.8,0.8]) # circle = mpatches.Circle(xy1, radius=0.05) # 参数1 圆心位置,参数2 半径 # rect = mpatches.Rectangle(xy2, 0.2, 0.1, color='r') # 参数1 矩形左下角位置,参数2 矩形的宽,参数3 矩形的长 # ploygon = mpatches.RegularPolygon(xy3, 5, 0.1, color='g') # 参数1 多边形外接圆圆心位置,参数2 多边形边数 # # 参数3 多边形外接圆半径 # ellipse = mpatches.Ellipse(xy4,0.4,0.2,color = 'y')# 参数1 椭圆的圆心,参数2 长半径,参数3 短半径 # ax.add_patch(rect) # 将图像加到ax上 # ax.add_patch(circle) # ax.add_patch(ploygon) # ax.add_patch(ellipse) # ax.axis('equal') # ax.grid()
14 极坐标
# 极坐标 import numpy as np import matplotlib.pyplot as plt # i = np.arange(1, 6) i = np.empty(5) i.fill(5) theta = [0, np.pi / 2, np.pi, 3 * np.pi / 2, 2 * np.pi] ax = plt.subplot(111, projection='polar') # projection = 'polar'定义极坐标 ax.plot(theta, i, color='r', linewidth=3) ax.grid(True) plt.show()
相关文章推荐
- 【备忘】2018年最新北风网零基础学习机器学习(Python语言、算法、Numpy库、MatplotLib)视频教程)
- 机器学习(一)- 基础库matplotlib的使用
- 【备忘】2017年最新北风网零基础学习机器学习(Python语言、算法、Numpy库、MatplotLib)视频教程)
- 机器学习基础工具 Numpy / Pandas / Matplotlib总结
- CS231n-机器学习中matplotlib绘图基础.md
- 【备忘】2017年最新北风网零基础学习机器学习(Python语言、算法、Numpy库、MatplotLib)视频教程)
- matplotlib基础学习
- matplotlib.pyplot基础(一)
- matplotlib学习之绘图基础
- Python笔记——Python基础、numpy基础、SciPy、Matplotlib
- Windows下Python机器学习环境搭建(pip numpy scipy matplotlib scikit-learn nltk)
- Seaborn学习以及Matplotlib基础
- 案例带你学python——matplotlib基础作图
- Matplotlib画图基础
- Matplotlib基础
- Matplotlib基础知识
- python包matplotlib基础入门
- python 机器学习的开发环境搭建(numpy,scipy,matplotlib,scikit-learn)
- 数据分析与展示——Matplotlib基础绘图函数示例
- numpy、scipy、matplotlib基础介绍