【问题标题】:Prob using axes for subplot an get AttributeError: 'numpy.ndarray' object has no attribute 'plot'Prob 使用轴进行子图获取 AttributeError:'numpy.ndarray' 对象没有属性'plot'
【发布时间】:2021-02-19 14:50:15
【问题描述】:

'''
matplotlib.use("Qt5Agg")

pas = [1,2,3,4,5,6,7,8,9,10]

# set up l'organisation de la figure
fig, axs = plt.subplots(nrows=3,ncols=len(pas))


manager = plt.get_current_fig_manager()
manager.window.showMaximized()
plt.show()
     
axs[1, 0+1].set(ylabel='fréquence (set test)')
for iii in range(0, len(pas)):
    
    bins = np.linspace(round(y[:,iii].min()), round(y[:,iii].max()), 20)
    axs[0, iii].hist(yhat_graph[:,iii], bins, color = 'steelblue', alpha = 0.5, label='simulation')
    axs[0, iii].hist(y[:,iii], bins, color = 'orange', alpha=0.5, label='ref. apport hist.')
    axs[0, iii].set(xlabel='apports m3/sec')

    axs[0, iii].legend(loc='upper right')
    axs[0, iii].set(title='prévision J+'  + str(iii+1) + ' (set test)' )
    

axs[1, 0].set(ylabel='Variations journalières Jn - Jn-1 (set test) \n observation')
for iii in range(0, len(pas)-1):
    axs[1, iii].plot()
    graph_scatter(ecart_valid_y[iii],
                  ecart_yhat[iii], True,'simulation','','var. j' + str(iii+1) +' - j ' + str(iii) ,'steelblue')
    axs[1, iii].set(xlabel='simulation')


if history == 'missing':
    print('pas de fichier history')
    axs[2,0:].plot()
else:
    axs[2, 0].plot()
    graph_loss(history)
    axs[2,1:].plot()

graph_sim_multiStep(y[-windowGraph[0]:-windowGraph[1]], yhat_graph[-windowGraph[0]:-windowGraph[1]], nash, kge, titre)

'''

用这一行“axs[2,1:].plot()”

我遇到了这个错误: AttributeError:“numpy.ndarray”对象没有属性“plot”

函数 'graph_loss' 和 'graph_scatter' 单独工作正常

【问题讨论】:

    标签: python matplotlib subplot axes


    【解决方案1】:

    正如你所说,问题出在这一行:

    axs[2,1:].plot()
    

    在您的代码中,axsAxesSubplot 对象的 3x10 numpy 数组。我假设您尝试做的是一次在其中几个对象上调用 plot() 方法,但是 numpy 数组不支持这样的方法调用。您可以查看 vectorize() 方法,但我认为使用 for 循环是最清楚的。这是一个小例子,它调用plot() 并在几个子图上提供一些数据,然后在其余子图上调用plot() 而没有参数。

    import matplotlib.pyplot as plt
    
    fig, axs = plt.subplots(nrows=2,ncols=3)
    
    axs[0, 0].plot([1, 4, 2])
    axs[0, 2].plot([4, 1, 2])
    
    # Could probably remove the next three lines.
    axs[0, 1].plot()
    for iii in range(3):
        axs[1, iii].plot()
    
    plt.show()
    

    我只是想知道你为什么要首先不带参数调用plot()。它似乎只是稍微改变了轴比例。尝试完全删除这些调用。

    【讨论】:

    • 我创建了一个脚本来创建复杂的自定义图,我希望生成子图并用我的脚本图填充它
    猜你喜欢
    • 2016-11-01
    • 2016-10-24
    • 2021-05-07
    • 2020-12-03
    • 2020-11-29
    • 2020-10-06
    • 2018-01-25
    • 2016-06-29
    相关资源
    最近更新 更多