• python--matplotlib库使用3


    解决问题:

    1 一张画布画多个子图

    2 matplotlib绘图中文不能显示问题

    3 线绘图函数plt.plot()与散点绘图函数plt.scatter()之间的区别

      上代码:

     1 #lwlr绘图比较3个yHat(k取1,0.01,0.003)
     2 def lwlrPlot3(xArr,yArr):               #输入:xArr是n×d矩阵/数组/列表;yArr是n×1
     3 
     4     xMat=mat(xArr)
     5     srtInd=xMat[:,1].argsort(0)         #等价于argsort(xMat[:,1],0)
     6     xSort=xMat[srtInd][:,0,:]           #等价于xMat[srtInd.flatten().A[0]] 
     7 
     8     yHat1=lwlrTest(xArr,xArr,yArr,1)    #调用局部加权回归(lwlr)主函数
     9     yHat2=lwlrTest(xArr,xArr,yArr,0.01)
    10     yHat3=lwlrTest(xArr,xArr,yArr,0.03)
    11 
    12     fig=plt.figure()
    13     ax1=fig.add_subplot(311)
    14     ax2=fig.add_subplot(312)
    15     ax3=fig.add_subplot(313)
    16 
    17     #画直线图需要排序
    18     #直线图plt.plot(),plot前要排序
    19     #ax1.plot(xMat[:,1],yHat[:].T)
    20     ax1.plot(xSort[:,1],yHat1[srtInd]) 
    21     ax2.plot(xSort[:,1],yHat2[srtInd])
    22     ax3.plot(xSort[:,1],yHat3[srtInd])
    23 
    24     #画散点图不需要排序,plt.scatter()
    25     ax1.scatter(xMat[:,1].flatten().A[0],mat(yArr).T.flatten().A    [0],s=2,c='r',label=u'欠拟合')  
    26     ax2.scatter(xMat[:,1].flatten().A[0],mat(yArr).T.flatten().A[0],s=2,c='r',label=u'最好')
    27     ax3.scatter(xMat[:,1].flatten().A[0],mat(yArr).T.flatten().A[0],s=2,c='r',label=u'过拟合')
    28 
    29     ax1.legend(loc='upper left')
    30     ax2.legend(loc='upper left')
    31     ax3.legend(loc='upper left')
    32 
    33     plt.show()

      注意:1 代码中调用了局部加权回归主函数"lwlrTest()",参见"python--局部加权回归"
              2 为了解决绘图时中文显示乱码问题,需在代码前端加入以下三行代码:

    1 '''
    2 解决python matplotlib画图无法显示中文的问题!
    3 '''
    4 from pylab import *
    5 mpl.rcParams['font.sans-serif']=['SimHei']
    6 mpl.rcParams['axes.unicode_minus']=False

      绘图结果:

  • 相关阅读:
    JS 获取网页内容高度 和 网页可视高度 支持IE6789 Firefox Chrome
    JS 回车快捷键登陆页面 兼容火狐和IE
    如何设置span宽度
    实现三行布局页面自适应不同分辨率下的屏幕高度
    电脑个别网站打不开 POSTMAN可以请求通
    克隆DataTable
    Microsoft Visual SourceSafe 6.0 关联VS
    asp.net 网站设置访问超时时长
    sql server management studio(ssms)连接多个数据库注意事项
    ASP.NET Core 开发中间件(StaticFiles)使用
  • 原文地址:https://www.cnblogs.com/cygalaxy/p/6810134.html
Copyright © 2020-2023  润新知