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

Nic*_*ini 4 python matplotlib iterable-unpacking

我正在 python 中创建一个函数,它允许我创建两个并行图,并且它们共享两个轴:

\n
def PlotManager(data1,data2,fig):\n    f, (ax1, ax2) = fig.subplots(2, 1, sharey=True,sharex=True)\n\n    #Plot1 sopra\n    x_axis = data1.index\n    #Plot and shade the area between the upperband and the lower band grey\n    ax1.fill_between(x_axis,data1[\'Upper\'],data1[\'Lower\'], color = \'grey\', alpha= 0.5)\n    #Plot the closing price and the moving average\n    ax1.plot(x_axis,data1[\'Close\'],color = \'gold\',lw = 3,label = \'Close Price\', alpha= 0.5)\n    ax1.plot(x_axis,data1[\'SMA\'],color = \'blue\',lw = 3,label = \'Simple Moving Average\', alpha= 0.5)\n    ax1.scatter(x_axis,data1[\'Buy\'],color="green", lw=3,label="Buy",marker = "^", alpha=1)\n    ax1.scatter(x_axis,data1[\'Sell\'],color="red", lw=3,label="Sell",marker = "v", alpha = 1)\n    #Set the title and show the image\n    ax1.set_title("Bollinger Band for Amazon")\n    plt.xticks(rotation = 45)\n\n    #Plot 2 Sotto\n    ax2.set_title(\'RSI_Plot\')\n    ax2.plot(x_axis,data2[\'RSI\'])\n    ax2.axhline(0,linestyle=\'--\',alpha=0.5, color="grey")\n    ax2.axhline(10,linestyle=\'--\',alpha=0.5, color="orange")\n    ax2.axhline(20,linestyle=\'--\',alpha=0.5, color="green")\n    ax2.axhline(30,linestyle=\'--\',alpha=0.5, color="red")\n    ax2.axhline(70,linestyle=\'--\',alpha=0.5, color="red")\n    ax2.axhline(80,linestyle=\'--\',alpha=0.5, color="green")\n    ax2.axhline(90,linestyle=\'--\',alpha=0.5, color="orange")\n    ax2.axhline(100,linestyle=\'--\',alpha=0.5, color="grey")\n
Run Code Online (Sandbox Code Playgroud)\n

但给了我cannot unpack non-iterable AxesSubplot object错误:

\n
def PlotManager(data1,data2,fig):\n    f, (ax1, ax2) = fig.subplots(2, 1, sharey=True,sharex=True)\n\n    #Plot1 sopra\n    x_axis = data1.index\n    #Plot and shade the area between the upperband and the lower band grey\n    ax1.fill_between(x_axis,data1[\'Upper\'],data1[\'Lower\'], color = \'grey\', alpha= 0.5)\n    #Plot the closing price and the moving average\n    ax1.plot(x_axis,data1[\'Close\'],color = \'gold\',lw = 3,label = \'Close Price\', alpha= 0.5)\n    ax1.plot(x_axis,data1[\'SMA\'],color = \'blue\',lw = 3,label = \'Simple Moving Average\', alpha= 0.5)\n    ax1.scatter(x_axis,data1[\'Buy\'],color="green", lw=3,label="Buy",marker = "^", alpha=1)\n    ax1.scatter(x_axis,data1[\'Sell\'],color="red", lw=3,label="Sell",marker = "v", alpha = 1)\n    #Set the title and show the image\n    ax1.set_title("Bollinger Band for Amazon")\n    plt.xticks(rotation = 45)\n\n    #Plot 2 Sotto\n    ax2.set_title(\'RSI_Plot\')\n    ax2.plot(x_axis,data2[\'RSI\'])\n    ax2.axhline(0,linestyle=\'--\',alpha=0.5, color="grey")\n    ax2.axhline(10,linestyle=\'--\',alpha=0.5, color="orange")\n    ax2.axhline(20,linestyle=\'--\',alpha=0.5, color="green")\n    ax2.axhline(30,linestyle=\'--\',alpha=0.5, color="red")\n    ax2.axhline(70,linestyle=\'--\',alpha=0.5, color="red")\n    ax2.axhline(80,linestyle=\'--\',alpha=0.5, color="green")\n    ax2.axhline(90,linestyle=\'--\',alpha=0.5, color="orange")\n    ax2.axhline(100,linestyle=\'--\',alpha=0.5, color="grey")\n
Run Code Online (Sandbox Code Playgroud)\n

我该如何处理这个错误?

\n

gbo*_*ffi 5

的值plt.subplots(2, 1, ...)是一个元组figure, array(subplot0, subplot1),以便您可以正确解包为一个图形和两个子图。

\n

相反, 的值fig.subplots(2, 1, ...)subplot0, subplot1(因为你已经有了数字\xe2\x80\xa6),当你尝试解压时,它相当于

\n
f = subplot0\nax0, ax1 = subplot1\n
Run Code Online (Sandbox Code Playgroud)\n

这导致TypeError: cannot unpack non-iterable AxesSubplot object

\n
\n

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

\n
ax1, ax2 = fig.subplots(2, 1, sharey=True,sharex=True)\n
Run Code Online (Sandbox Code Playgroud)\n