python机器学习入门之matplotlib的使用(超详细,必看)

悠悠 2023-09-30 15:24 143阅读 0赞

matplotlib是python的一个基本2D绘图库 功能强大

比较常用的是里面的pyplot子模块

常见函数有figure()创建一个空白画布 add_subplot()创建子图 title()设置标题

xlabel()设置x轴名称 ylabel()设置y轴名称 legend()指定图例 show()显示图形等等

实例一如下

67652be690ee42d6adff73c595644a4e.png

代码如下

  1. import pandas as pd
  2. import numpy as np
  3. from pandas import Series
  4. from numpy import nan as NA
  5. import matplotlib.pyplot as plt
  6. from mpl_toolkits.mplot3d import Axes3D
  7. fig=plt.figure()
  8. ax1=fig.add_subplot(2,2,1)
  9. ax2=fig.add_subplot(2,2,2)
  10. ax3=fig.add_subplot(2,2,3)
  11. plt.show()

2:在子图上绘制图形 如三角形 椭圆 矩形

578adbcc15ab4a69b27a2ed2a76a7415.png

代码如下

  1. import pandas as pd
  2. import numpy as np
  3. from pandas import Series
  4. from numpy import nan as NA
  5. import matplotlib.pyplot as plt
  6. from mpl_toolkits.mplot3d import Axes3D
  7. ax=fig.add_subplot(1,1,1)
  8. rect=plt.Rectangle((0.2,0.75),0.4,0.15,color='r',alpha=0.3)
  9. cir=plt.Circle((0.7,0.2),0.15,color='b',alpha=0.3)
  10. pgon=plt.Polygon([[0.2,0.2],[0.35,0.25],[0.2,0.6]],color='g',alpha=0.9)
  11. ax.add_patch(rect)
  12. ax.add_patch(cir)
  13. ax.add_patch(pgon)
  14. plt.show()

3:直线图的绘画

f767ba2645814990b6fe4a4dd64c5178.png

  1. import pandas as pd
  2. import numpy as np
  3. from pandas import Series
  4. from numpy import nan as NA
  5. import matplotlib.pyplot as plt
  6. from mpl_toolkits.mplot3d import Axes3D
  7. a=np.arange(10)
  8. plt.xlabel('x')
  9. plt.ylabel('y')
  10. plt.plot(a,a*1.5,a,a*2.5,a,a*3.5,a,a*4.5)
  11. plt.legend(['1.5x','2.5x','3.5x','4.5x'])
  12. plt.title('simple lines')
  13. j=np.linspace(-10,10,100)
  14. k=np.sin(j)
  15. plt.plot(j,k,marker="o")
  16. plt.show()

4:为series数据绘制图形

e7f7b3780e684faf9716295d76fe3406.png

  1. import numpy as np
  2. from pandas import Series
  3. from numpy import nan as NA
  4. import matplotlib.pyplot as plt
  5. from mpl_toolkits.mplot3d import Axes3D
  6. s1=Series(np.random.randn(1000).cumsum())
  7. s2=Series(np.random.randn(1000).cumsum())
  8. plt.subplot(2,1,1)
  9. ax1=s1.plot(kind='line',label='S1',title="figures of Series",style='--')
  10. s2.plot(ax=ax1,kind='line',label='S2')
  11. plt.ylabel('value')
  12. plt.legend(loc=2)
  13. plt.subplot(2,1,2)
  14. s1[0:10].plot(kind='bar',grid=True,label='s1')
  15. plt.xlabel('index')
  16. plt.ylabel('value')
  17. plt.show()

5:三维图形的绘画 虽说matplotlib主要用于二维图形绘制 但是三维的也可以画

809c9e431b2f486397512da3a181855b.png

  1. import numpy as np
  2. from pandas import Series
  3. from numpy import nan as NA
  4. import matplotlib.pyplot as plt
  5. from mpl_toolkits.mplot3d import Axes3D
  6. def randrange(n,randFloor,randceil):
  7. rnd=np.random.rand(n)
  8. return (randceil-randFloor)*rnd+randFloor
  9. plt.rcParams['font.sans-serif']=['SimHei']
  10. fig=plt.figure(figsize=(10,8))
  11. ax=fig.add_subplot(111,projection="3d")
  12. n=100
  13. for zmin,zmax,c,m,l in[(4,15,'r','o','低值'),(13,40,'g','*','高值')]:
  14. x=randrange(n,0,20)
  15. y=randrange(n,0,20)
  16. z=randrange(n,zmin,zmax)
  17. ax.scatter(x,y,z,c=c,marker=m,label=l,s=z*6)
  18. ax.set_xlabel("x-value")
  19. ax.set_ylabel("y-value")
  20. ax.set_zlabel("z-value")
  21. ax.set_title("高低值 3D散点图",alpha=0.6,size=15,weight='bold')
  22. ax.legend(loc="upper left")
  23. plt.show()

觉得有帮助请点赞收藏喔

发表评论

表情:
评论列表 (有 0 条评论,143人围观)

还没有评论,来说两句吧...

相关阅读