当前位置:   article > 正文

matplotlib之绘制子图中遇到的问题及解决方法_subplot() takes 1 or 3 positional arguments but 2

subplot() takes 1 or 3 positional arguments but 2 were given

将从下面这四步开始介绍整个过程(遇到问题,解决问题的过程)

1:将在一个窗口中绘制三个子图。转变为在一个窗口绘制一个图,并在另一个窗口绘制剩下两个子图。

2:在绘制第二个窗口时,子图的创建 和 函数的选择;

3:第二个窗口中子图的排列。

例子,在一个窗口,分别用三个子图绘制两种不同的数据。

1、绘制子图的代码。

其中“test_y_x”、“test_y_y”分别代表“横轴”“纵轴”,“rel_test_y_x”、“rel_test_y_y”同理。可用自己的数据代替,这里代表我数据集中(x,y)的坐标位置。

  1. # 创建横轴
  2. x = np.arange(1200)
  3. x.reshape(1200, 1)
  4. # 创建第一个子图,位置第一行第一列
  5. plt.subplot(3, 1, 1)
  6. plt.xlabel('X') # 设置横坐标标签
  7. plt.ylabel('Y') # 设置纵坐标标签
  8. plt.plot(test_y_x, test_y_y, '.', linewidth=1.0, color='red') # 根据数据横轴和纵轴的点进行绘制,每个点用“.”表示,“linewidth”设置点的粗细,“color”设置点的颜色。下面同理
  9. plt.plot(rel_test_y_x, rel_test_y_y, '.', linewidth=1.0, color='green')
  10. plt.legend(["predict_after_10", "true_after_10"], loc='upper right') #设置不同颜色点的标识,标识位置图片的右上角
  11. # 创建第二个子图
  12. plt.subplot(3, 1, 2)
  13. plt.xlabel('step_time')
  14. plt.ylabel('X')
  15. plt.plot(x, test_y_x, ',', color='red')
  16. plt.plot(x, rel_test_y_x, ',', color='green')
  17. plt.legend(["predict_after_10", "true_after_10"], loc='upper right')
  18. # 创建第三个子图
  19. plt.subplot(3, 1, 3)
  20. plt.xlabel('step_time')
  21. plt.ylabel('Y')
  22. plt.plot(x, test_y_y, ',', color='red')
  23. plt.plot(x, rel_test_y_y, ',', color='green')
  24. plt.legend(["predict_after_10", "true_after_10"], loc='lower right')
  25. # 自动调节子图之间的间距
  26. plt.tight_layout()
  27. # 将绘制窗口显示出来
  28. plt.show()

运行出绘制的结果如下图:

2、将上面的一个窗口中三个子图,绘制为两个窗口,一个窗口有一张图,另一个窗口有两个子图。

2.1、报错信息1:

将会遇到的报错信息:TypeError: subplots() takes from 1 to 3 positional arguments but 4 were given。

 报错位置:在绘制第二个画布中的第二行“ax2=fig2.subplots(2,1,1)”

解决方法:将“subplots”改为“subplot”。

自我理解:“fig.subplots()”是生成一个画布一个子图时用的,如果想要在同一个画布中生成多个子图,需要用“subplot()”函数。

其他地方的更改:使用“fig1= plt.figure(1)”,“ax1=fig1.subplots()”生成一个画布和一个坐标系。

下面是报错代码:

  1. # 创建横轴
  2. x = np.arange(1200)
  3. x.reshape(1200, 1)
  4. # 生成一个Figure画布和一个Axes坐标系
  5. fig1 = plt.figure(1)
  6. ax1 = fig1.subplots()
  7. plt.xlabel('X') # 设置横坐标标签
  8. plt.ylabel('Y') # 设置纵坐标标签
  9. ax1.plot(test_y_x, test_y_y, '.', linewidth=1.0, color='red') # 根据数据横轴和纵轴的点进行绘制,每个点用“.”表示,“linewidth”设置点的粗细,“color”设置点的颜色。下面同理
  10. ax1.plot(rel_test_y_x, rel_test_y_y, '.', linewidth=1.0, color='green')
  11. ax1.legend(["predict_after_10", "true_after_10"], loc='upper right') #设置不同颜色点的标识,标识位置图片的右上角
  12. # 生成第二个画布,创建第一个子图
  13. fig2 = plt.figure(2)
  14. ax2 = fig2.subplots(2, 1, 1)
  15. plt.xlabel('step_time')
  16. plt.ylabel('X')
  17. ax2.plot(x, test_y_x, ',', color='red')
  18. ax2.plot(x, rel_test_y_x, ',', color='green')
  19. ax2.legend(["predict_after_10", "true_after_10"], loc='upper right')
  20. # 创建第二个子图
  21. ax3 = fig2.add_subplots(2, 1, 2)
  22. plt.xlabel('step_time')
  23. plt.ylabel('Y')
  24. ax3.plot(x, test_y_y, ',', color='red')
  25. ax3.plot(x, rel_test_y_y, ',', color='green')
  26. ax3.legend(["predict_after_10", "true_after_10"], loc='lower right')
  27. # 自动调节子图之间的间距
  28. plt.tight_layout()
  29. # 将绘制窗口显示出来
  30. plt.show()

下面是改正后的代码:以第二张画布的第一个子图为例。将“fig2.subplots(2,1,1)”改为“fig2.subplot(2,1,1)”

  1. # 生成第二个画布,创建第一个子图
  2. fig2 = plt.figure(2)
  3. ax2 = fig2.subplot(2, 1, 1)
  4. plt.xlabel('step_time')
  5. plt.ylabel('X')
  6. ax2.plot(x, test_y_x, ',', color='red')
  7. ax2.plot(x, rel_test_y_x, ',', color='green')
  8. ax2.legend(["predict_after_10", "true_after_10"], loc='upper right')

2.2、报错信息2:

将会遇到的报错信息:ax2 = fig2.subplot(2, 1, 1)
AttributeError: 'Figure' object has no attribute 'subplot'

解决方法:将“fig2.subplot(2, 1, 1)”改为“fig2.add_subplot(2, 1, 1)”

自我理解:fig中没有“subplot()”函数

下面是改正后的代码:以第二张画布的第一个子图为例。将“fig2.subplots(2,1,1)”改为“fig2.subplot(2,1,1)”

  1. # 生成第二个画布,创建第一个子图
  2. fig2 = plt.figure(2)
  3. ax2 = fig2.add_subplot(2, 1, 1)
  4. plt.xlabel('step_time')
  5. plt.ylabel('X')
  6. ax2.plot(x, test_y_x, ',', color='red')
  7. ax2.plot(x, rel_test_y_x, ',', color='green')
  8. ax2.legend(["predict_after_10", "true_after_10"], loc='upper right')

3、将一张画布中绘制三张子图,转变到一张画布中绘制一张子图 + 另一张画布中绘制两张子图的完整正确代码。

代码如下所示:上面提到的问题在下面代码中也进行了标识。

  1. x = np.arange(1200)
  2. x.reshape(1200, 1)
  3. # 生成一个Figure画布和一个Axes坐标系
  4. fig1 = plt.figure(1)
  5. ax1 = fig1.subplots()
  6. plt.xlabel('X')
  7. plt.ylabel('Y')
  8. ax1.plot(test_y_x, test_y_y, '.', linewidth=1.0, color='red')
  9. ax1.plot(rel_test_y_x, rel_test_y_y, '.', linewidth=1.0, color='green')
  10. ax1.legend(["predict_after_10", "true_after_10"], loc='upper right')
  11. # 创建图形
  12. fig2 = plt.figure(2)
  13. # 【问题2.1、在用多图的时候,函数名称是“subplot”,而不是“subplots”】
  14. # ax2 = fig2.subplot(2, 1, 1)
  15. # 【问题2.2、不能直接像上面那样使用,要改为下面这种】
  16. ax2 = fig2.add_subplot(2, 1, 1)
  17. plt.xlabel('step_time')
  18. plt.ylabel('X')
  19. ax2.plot(x, test_y_x, ',', color='red')
  20. ax2.plot(x, rel_test_y_x, ',', color='green')
  21. ax2.legend(["predict_after_10", "true_after_10"], loc='upper right')
  22. ax3 = fig2.add_subplot(2, 1, 2)
  23. plt.xlabel('step_time')
  24. plt.ylabel('Y')
  25. ax3.plot(x, test_y_y, ',', color='red')
  26. ax3.plot(x, rel_test_y_y, ',', color='green')
  27. ax3.legend(["predict_after_10", "true_after_10"], loc='lower right')
  28. # 【自动调节子图之间的间距】
  29. plt.tight_layout()
  30. plt.show()

最后运行结果图,如下所示。

 

参考链接:

matplotlib 绘制多个图形,如何同时独立显示? - 知乎 (zhihu.com)https://www.zhihu.com/question/280931066

plt.subplot()用法_plt.subplots()的用法_杰伦的大眼睛的博客-CSDN博客icon-default.png?t=N5F7https://blog.csdn.net/qq_39328344/article/details/107340747

下面链接中的回答是解决方法

TypeError: subplots() takes from 0 to 2 positional arguments but 3 were given_subplots()takes from_端己的博客-CSDN博客​​​​​​https://blog.csdn.net/wanchl980/article/details/126613784

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/612682
推荐阅读
相关标签
  

闽ICP备14008679号