kir*_*off 9 python graphics plot matplotlib subplot
我想用Python matplotlib创建包含许多(100)子图的绘图.我找不到合适的语法:
我想要的东西(这不起作用)
plt.subplot(10,10,i,X1, Y)
Run Code Online (Sandbox Code Playgroud)
在一个循环中,i从0到99,然后
plt.show()
Run Code Online (Sandbox Code Playgroud)
许多教程中都提供了语法,以便只有很少的子图.然后,语法可以
plt.close('all')
fig = plt.figure()
ax1 = plt.subplot(221)
ax2 = plt.subplot(223)
ax3 = plt.subplot(122)
example_plot(ax1)
example_plot(ax2)
example_plot(ax3)
plt.tight_layout()
Run Code Online (Sandbox Code Playgroud)
对于我的问题,我想我不能使用与我一样的语法plt.subplot(10101)
,等等,我不明白.
你有解决方案吗?
谢谢
jor*_*eca 14
试试这个:
fig, ax = plt.subplots(10, 10)
Run Code Online (Sandbox Code Playgroud)
其中ax将在列表(列表)中包含一百个轴.
这是一个非常方便的功能,来自以下文档:
Definition: plt.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, **fig_kw)
Create a figure with a set of subplots already made.
This utility wrapper makes it convenient to create common layouts of
subplots, including the enclosing figure object, in a single call.
Run Code Online (Sandbox Code Playgroud)
你的例子几乎是正确的.请用:
for i in range(100):
ax = plt.subplot(10,10,i)
ax.plot(...)
Run Code Online (Sandbox Code Playgroud)
这是一个完全有效的代码解决方案,可以显示事物的编号,因为看起来人们仍然会看到这里:
columns = 10
rows = 4
fig, ax_array = plt.subplots(rows, columns,squeeze=False)
for i,ax_row in enumerate(ax_array):
for j,axes in enumerate(ax_row):
axes.set_title('{},{}'.format(i,j))
axes.set_yticklabels([])
axes.set_xticklabels([])
# axes.plot(you_data_goes_here,'r-')
plt.show()
Run Code Online (Sandbox Code Playgroud)
哪个输出这个来向你展示编号是如何工作的(我只用了4行而不是10行来使图片变得更小,只需将"行"改为10即可得到10行子图):
编号显示您在每个位置将拥有的i和j值,以便您可以在matplotlib子图阵列中按照您想要的方式排列.这包含了数组中的子图,用于您想要的任何布局.