且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Python 中使用 matplotlib 创建子图

更新时间:2022-03-04 06:50:55

正如错误明确指出的那样,您向 pyplot.subplot() 传递了一个非法参数.如果您查看该功能的文档,您会看到它需要 3 个参数(可以浓缩为一个):ax = plt.subplot(2, 1, 1)ax = plt.subplot(211).

As the error clearly states, you passed an illegal argument to pyplot.subplot(). If you look at the documentation for that function, you'll see that it takes 3 arguments (which can be condensed in one): ax = plt.subplot(2, 1, 1) or ax = plt.subplot(211).

但是,您要查找的功能是 plt.subplots()(请注意最后的 s ),

However, the function that you are looking for is plt.subplots() (note the s at the end), which generates both a figure and an array of subplots:

f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)