根据列表的长度自动生成子图
我想根据我称之为dets的numpy阵列长度生成色情子图。我想使用数组长度来确定子图的正确数量的列和行。如果是正方形,请绘制子图的正方形矩阵,如果不是正方形,请添加另一个行。对于初学者,我编写了一些代码来检查数组的长度是否会创建子图的正方形矩阵,其中以下是:
data_f = np.random.rand(len(dets),2,5)
dets = np.arange(-5,-0.75,0.25)
x = np.array([1,5,6,3,8,9,2,3,10,12,3])
v = np.linspace(0,10,len(x))
square = np.sqrt(len(dets))
check_square = len(dets)%square
non_square = 1
print(len(data_f))
if check_square == 0:
nrows = int(np.sqrt(len(dets)))
ncols = int(np.sqrt(len(dets)))
else:
nrows = int(np.sqrt(len(dets)))+non_square
ncols = int(np.sqrt(len(dets)))
fig, ax = plt.subplots(nrows, ncols, sharex='col', sharey='row')
for i in range(nrows):
for j in range(ncols):
if i==0:
im = ax[i,j].imshow(data_f[j],extent=(x.min(), x.max(), v.min(), v.max()),origin='lower',aspect='auto')
else:
im = ax[i,j].imshow(data_f[j+ncols*i],extent=(x.min(), x.max(), v.min(), v.max()),origin='lower',aspect='auto')
输出图:
该图17个图,但由于错误
这绘制了我想要的所有内容,除了它总是以奇怪的方式将剧情击中,因为有以下错误:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
~\AppData\Local\Temp\1/ipykernel_4560/3817292743.py in <module>
6 im = ax[i,j].imshow(data_f[j],extent=(x.min(), x.max(), v.min(), v.max()),origin='lower',aspect='auto')
7 else:
----> 8 im = ax[i,j].imshow(data_f[j+ncols*i],extent=(x.min(), x.max(), v.min(), v.max()),origin='lower',aspect='auto')
9
IndexError: index 17 is out of bounds for axis 0 with size 17
I want to generate colorplot subplots based on a numpy array's length which I call dets. I want to use the array length to determine the right number of columns and rows for the subplots. If square, plot a square matrix of subplots, if not square, add another row. For starters, I have written some code to check if the array's length would create a square matrix of subplots with the following:
data_f = np.random.rand(len(dets),2,5)
dets = np.arange(-5,-0.75,0.25)
x = np.array([1,5,6,3,8,9,2,3,10,12,3])
v = np.linspace(0,10,len(x))
square = np.sqrt(len(dets))
check_square = len(dets)%square
non_square = 1
print(len(data_f))
if check_square == 0:
nrows = int(np.sqrt(len(dets)))
ncols = int(np.sqrt(len(dets)))
else:
nrows = int(np.sqrt(len(dets)))+non_square
ncols = int(np.sqrt(len(dets)))
fig, ax = plt.subplots(nrows, ncols, sharex='col', sharey='row')
for i in range(nrows):
for j in range(ncols):
if i==0:
im = ax[i,j].imshow(data_f[j],extent=(x.min(), x.max(), v.min(), v.max()),origin='lower',aspect='auto')
else:
im = ax[i,j].imshow(data_f[j+ncols*i],extent=(x.min(), x.max(), v.min(), v.max()),origin='lower',aspect='auto')
The output plot:
This plots 17 plots but the resulting plots I cannot adjust because of the error
This plots everything I want, except it always smushes the plots together in a weird way because of the following error:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
~\AppData\Local\Temp\1/ipykernel_4560/3817292743.py in <module>
6 im = ax[i,j].imshow(data_f[j],extent=(x.min(), x.max(), v.min(), v.max()),origin='lower',aspect='auto')
7 else:
----> 8 im = ax[i,j].imshow(data_f[j+ncols*i],extent=(x.min(), x.max(), v.min(), v.max()),origin='lower',aspect='auto')
9
IndexError: index 17 is out of bounds for axis 0 with size 17
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您总共有20个轴,并且当您循环穿越NCOL和NROWS时,您将获得20次迭代。 data_f)只有17
。
但是len (
It is because you have a total of 20 axes, and when you loop through ncols and nrows, you will get 20 iterations. But len(data_f) is only 17.
at the start of your iteration, add
I did this and it stopped the error