简体   繁体   English

无法解包不可迭代的 AxesSubplot 对象-Matplotlib

[英]Cannot unpack non-iterable AxesSubplot object- Matplotlib

I am making a function in python which allows me to create two parallel graphs and they share their 2 axes:我正在 python 中创建一个函数,它允许我创建两个平行图并且它们共享它们的 2 个轴:

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")

But gives me the cannot unpack non-iterable AxesSubplot object error:但是给了我cannot unpack non-iterable AxesSubplot object错误:

[Command: python -u C:\Users\Nicolò\Documents\Git\ProgettoTradingBot\ProgettoTradeBot\GUIprova2.py]
C:\Users\Nicolò\Documents\Git\ProgettoTradingBot\ProgettoTradeBot\BollingerBandsFinal.py:63: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
  ax = f.add_subplot(111)
C:\Users\Nicolò\Documents\Git\ProgettoTradingBot\ProgettoTradeBot\BollingerBandsFinal.py:63: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
  ax = f.add_subplot(111)
Traceback (most recent call last):
  File "C:\Users\Nicolò\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\cbook\__init__.py", line 196, in process
    func(*args, **kwargs)
  File "C:\Users\Nicolò\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\animation.py", line 951, in _start
    self._init_draw()
  File "C:\Users\Nicolò\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\animation.py", line 1743, in _init_draw
    self._draw_frame(next(self.new_frame_seq()))
  File "C:\Users\Nicolò\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\animation.py", line 1766, in _draw_frame
    self._drawn_artists = self._func(framedata, *self._args)
  File "C:\Users\Nicolò\Documents\Git\ProgettoTradingBot\ProgettoTradeBot\GUIprova2.py", line 48, in animate
    PlotManager(BollingerBands(df,f),RSI(df,f2),f)
  File "C:\Users\Nicolò\Documents\Git\ProgettoTradingBot\ProgettoTradeBot\mostraGrafici.py", line 7, in PlotManager
    f, (ax1, ax2) = fig.subplots(2, 1, sharey=True,sharex=True)
TypeError: cannot unpack non-iterable AxesSubplot object

How can i handle to this error?我该如何处理这个错误?

The value of plt.subplots(2, 1, ...) is a tuple figure, array(subplot0, subplot1) so that you can unpack correctly to a figure and two subplots. plt.subplots(2, 1, ...)是一个元figure, array(subplot0, subplot1)以便您可以正确解包为一个图和两个子图。

On the contrary the value of fig.subplots(2, 1, ...) is subplot0, subplot1 (because you ALREADY have the figure…) and when you try to unpack it's equivalent to相反, fig.subplots(2, 1, ...)subplot0, subplot1 (因为你已经有了这个图......),当你尝试解压它时,它相当于

f = subplot0
ax0, ax1 = subplot1

and this leads to TypeError: cannot unpack non-iterable AxesSubplot object这导致TypeError: cannot unpack non-iterable AxesSubplot object


Because you are not using the object labeled as f in the following, you should write因为你没有使用下面标记为f的对象,所以你应该写

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM