Python 无法解压缩不可编辑的AxeSubPlot对象-Matplotlib

Python 无法解压缩不可编辑的AxeSubPlot对象-Matplotlib,python,matplotlib,iterable-unpacking,Python,Matplotlib,Iterable Unpacking,我正在用python制作一个函数,它允许我创建两个平行图,它们共享两个轴: def PlotManager(data1,data2,fig): f, (ax1, ax2) = fig.subplots(2, 1, sharey=True,sharex=True) #Plot1 sopra x_axis = data1.index #Plot and shade the area between the upperband and the lower band g

我正在用python制作一个函数,它允许我创建两个平行图,它们共享两个轴:

def PlotManager(data1,data2,fig):
    f, (ax1, ax2) = fig.subplots(2, 1, sharey=True,sharex=True)

    #Plot1 sopra
    x_axis = data1.index
    #Plot and shade the area between the upperband and the lower band grey
    ax1.fill_between(x_axis,data1['Upper'],data1['Lower'], color = 'grey', alpha= 0.5)
    #Plot the closing price and the moving average
    ax1.plot(x_axis,data1['Close'],color = 'gold',lw = 3,label = 'Close Price', alpha= 0.5)
    ax1.plot(x_axis,data1['SMA'],color = 'blue',lw = 3,label = 'Simple Moving Average', alpha= 0.5)
    ax1.scatter(x_axis,data1['Buy'],color="green", lw=3,label="Buy",marker = "^", alpha=1)
    ax1.scatter(x_axis,data1['Sell'],color="red", lw=3,label="Sell",marker = "v", alpha = 1)
    #Set the title and show the image
    ax1.set_title("Bollinger Band for Amazon")
    plt.xticks(rotation = 45)

    #Plot 2 Sotto
    ax2.set_title('RSI_Plot')
    ax2.plot(x_axis,data2['RSI'])
    ax2.axhline(0,linestyle='--',alpha=0.5, color="grey")
    ax2.axhline(10,linestyle='--',alpha=0.5, color="orange")
    ax2.axhline(20,linestyle='--',alpha=0.5, color="green")
    ax2.axhline(30,linestyle='--',alpha=0.5, color="red")
    ax2.axhline(70,linestyle='--',alpha=0.5, color="red")
    ax2.axhline(80,linestyle='--',alpha=0.5, color="green")
    ax2.axhline(90,linestyle='--',alpha=0.5, color="orange")
    ax2.axhline(100,linestyle='--',alpha=0.5, color="grey")
但是给了我
无法解包不可编辑的AxeSubplot对象的错误:

[命令:python-uc:\Users\Nicolò\Documents\Git\ProgettoTradingBot\ProgettoTradeBot\GUIprova2.py]
C:\Users\Nicolò\Documents\Git\progetotradingbot\progetotradebot\bolingerbandsfinal.py:63:MatplotlibDeprecationWarning:使用与先前轴相同的参数添加轴当前将重用先前的实例。在将来的版本中,将始终创建并返回一个新实例。同时,通过向每个轴实例传递唯一的标签,可以抑制此警告,并确保将来的行为。
ax=f.add_子批次(111)
C:\Users\Nicolò\Documents\Git\progetotradingbot\progetotradebot\bolingerbandsfinal.py:63:MatplotlibDeprecationWarning:使用与先前轴相同的参数添加轴当前将重用先前的实例。在将来的版本中,将始终创建并返回一个新实例。同时,通过向每个轴实例传递唯一的标签,可以抑制此警告,并确保将来的行为。
ax=f.add_子批次(111)
回溯(最近一次呼叫最后一次):
文件“C:\Users\Nicolò\AppData\Local\Programs\Python\38\lib\site packages\matplotlib\cbook\\ uuuuuuu init_uuuuu.py”,第196行,正在处理中
func(*args,**kwargs)
文件“C:\Users\Nicol\AppData\Local\Programs\Python38\lib\site packages\matplotlib\animation.py”,第951行,在_start中
self._init_draw()
文件“C:\Users\Nicol\AppData\Local\Programs\Python38\lib\site packages\matplotlib\animation.py”,第1743行,在_init_draw中
self.\u draw\u frame(下一步(self.new\u frame\u seq()))
文件“C:\Users\Nicol\AppData\Local\Programs\Python38\lib\site packages\matplotlib\animation.py”,第1766行,在“绘图”框架中
self.\u drawing\u artists=self.\u func(框架数据,*self.\u参数)
文件“C:\Users\Nicolò\Documents\Git\progetotradingbot\progetotradebot\GUIprova2.py”,第48行,动画
绘图仪管理器(柏林湾(df,f)、RSI(df,f2)、f)
文件“C:\Users\Nicolò\Documents\Git\ProgettoTradingBot\ProgettoTradeBot\mostraGrafici.py”,第7行,在PlotManager中
f、 (ax1,ax2)=图子批次(2,1,sharey=True,sharex=True)
TypeError:无法解压缩不可编辑的AxeSubPlot对象

如何处理此错误?

plt.subplot(2,1,…)
的值是一个元组
图形、数组(subplot 0,subplot 1)
,这样您就可以正确地解包为一个图形和两个子地块

相反,
fig.subplot(2,1,…)
的值是
subplot 0,subplot 1
(因为您已经有了该图…),当您尝试解包时,它相当于

f = subplot0
ax0, ax1 = subplot1
这将导致
TypeError:无法解压缩不可编辑的AxesSubplot对象


因为您没有在下面使用标记为
f
的对象,所以应该编写

ax1, ax2 = fig.subplots(2, 1, sharey=True,sharex=True)