根据列表的长度自动生成子图

发布于 2025-02-09 22:09:57 字数 1738 浏览 2 评论 0原文

我想根据我称之为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

但可醉心 2025-02-16 22:09:57

这是因为您总共有20个轴,并且当您循环穿越NCOL和NROWS时,您将获得20次迭代。 data_f)只有17

if(j + ncols*i) == len(data_f): 
    break

但是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

if(j + ncols*i) == len(data_f): 
    break

I did this and it stopped the error

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文