【问题标题】:tight_layout throw error: ValueError: max() arg is an empty sequence紧密布局抛出错误:ValueError:max()arg是一个空序列
【发布时间】:2017-08-09 10:35:34
【问题描述】:

我正在尝试在 python 中使用 plt.ion() 启用更新数字:

import numpy as np
import numpy.matlib
import matplotlib.pyplot as plt

plt.ion()

plt.close('all')

tmax =30
W = np.random.rand(6,100)        

fig, ax = plt.subplots(2,3)     
plt.show()

for t in range (1, tmax):
    W = t * W        
    for ii in range(2):
        for jj in range(3):      
            output = W[3*ii+jj,:].reshape((10,10),order = 'F')
            ax[ii,jj].clear()
            ax[ii,jj].imshow(output, interpolation='nearest')                
    plt.tight_layout()
    plt.draw()
    plt.pause(0.1)


plt.waitforbuttonpress()

运行此命令将在控制台中显示空白数字并抛出错误:

Traceback(最近一次调用最后一次):

文件“”,第 1 行,在 runfile('/Users/Alessi/Documents/Spyder/3240ass/comp.py', wdir='/Users/Alessi/Documents/Spyder/3240ass')

文件 “/Users/Alessi/anaconda/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py”, 第 866 行,在运行文件中 execfile(文件名,命名空间)

文件 “/Users/Alessi/anaconda/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py”, 第 102 行,在 execfile 中 exec(编译(f.read(),文件名,'exec'),命名空间)

文件“/Users/Alessi/Documents/Spyder/3240ass/comp.py”,第 27 行,在 plt.tight_layout()

文件 “/Users/Alessi/anaconda/lib/python3.5/site-packages/matplotlib/pyplot.py”, 第 1387 行,在紧密布局中 fig.tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)

文件 “/Users/Alessi/anaconda/lib/python3.5/site-packages/matplotlib/figure.py”, 第 1752 行,在紧密布局中 rect=rect)

文件 “/Users/Alessi/anaconda/lib/python3.5/site-packages/matplotlib/tight_layout.py”, 第 322 行,在 get_tight_layout_figure 中 max_nrows = max(nrows_list)

ValueError: max() arg 是一个空序列

ps.使用 anaconda spyder

【问题讨论】:

    标签: python matplotlib spyder


    【解决方案1】:

    您不能在控制台中使用交互式更多 (ion)。不幸的是,问题不是很清楚你想要什么。但是让我们假设您想显示一个带有动画情节的窗口。一个简单的方法是在 IPython 控制台之外运行脚本。在 spyder 中,您将转到运行/配置(或按 F6)并选择“在新的专用 Python 控制台中执行”。

    现在脚本本身的问题是你首先调用plt.show(),它显示了空的子图。这必须被删除。

    会显示动画的版本是

    import numpy as np
    import matplotlib.pyplot as plt
    
    plt.close('all')
    plt.ion()
    
    tmax =30
    
    fig, ax = plt.subplots(2,3)     
    
    for t in range (1, tmax):
        W = np.random.rand(6,100)      
        for ii in range(2):
            for jj in range(3):      
                output = W[3*ii+jj,:].reshape((10,10),order = 'F')
                ax[ii,jj].clear()
                ax[ii,jj].imshow(output, interpolation='nearest')
        if t == 1:                
            plt.tight_layout()
        plt.draw()
        plt.pause(0.1)
    
    
    plt.waitforbuttonpress()
    

    【讨论】:

    • 对我来说重要的部分是:“现在脚本本身的问题是你首先调用 plt.show(),它显示了空的子图。这必须被删除。”
    猜你喜欢
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 2016-07-03
    • 2021-01-11
    相关资源
    最近更新 更多