Python – Type Error : ‘Figure’ object is not iterable (itertools)

Type Error : ‘Figure’ object is not iterable (itertools)… here is a solution to the problem.

Type Error : ‘Figure’ object is not iterable (itertools)

import itertools

axs = plt.subplots(nrows=3, ncols=3, figsize=(15,15))

axs_list = list(itertools.chain.from_iterable(axs))
for ax in axs_list:

ax.plot(gen_stock_price_array2())

When I use itertools.chain.from_iterable, I get a type error. I’ve searched Google but can’t find an answer. I wonder if anyone else has the same problem, which is a bit strange to me.

Type error image:
enter image description here

Solution

plt.subplots(nrows=3, ncols=3, figsize=(15,15)) returns a two-element tuple: the first element is Figure and the second element is the axis.

You may need the following:

fig, axes = plt.subplots(nrows=3, ncols=3, figsize=(15,15))

for ax in axes.flat:
    ax.plot(gen_stock_price_array2())

I hope this will make some difference.

Related Problems and Solutions