AttributeError: module ‘matplotlib.cbook’ has no attribute ‘iterable’ error typically occurs when the attribute iterable is not found in the matplotlib.cbook module, which you are trying to access.
To fix the error, downgrade the matplotlib version to 2.2.3 or use isinstance(x, collections.abc.Iterable) from the collections.abc.
Flowchart
Reproduce the error
import matplotlib.cbook
data = [11, 21, 19, 46]
# Try to use the `iterable` attribute, which does not exist
if matplotlib.cbook.iterable(data):
print("Data is iterable")
else:
print("Data is not iterable")
Output
AttributeError: module 'matplotlib.cbook' has no attribute 'iterable'
How to fix it?
Downgrade the matplotlib version using this command:
pip install matplotlib==2.2.3
You need to check the Matplotlib documentation to define the correct version that you should be using and ensure that you have installed the correct version.
If the attribute has been removed, you must modify your code to use a different method or attribute in the latest version of Matplotlib.
The iterable function from matplotlib.cbook might be deprecated in newer versions of Matplotlib.
Review your code to see if you are using matplotlib.cbook.iterable, and replace it with an alternative if needed.
Alternate solution
Modify your code to use the collections.abc module to check if an object is iterable.
import collections.abc
data = [11, 21, 19, 46]
if isinstance(data, collections.abc.Iterable):
print("Data is iterable")
else:
print("Data is not iterable")
Output
Data is iterable
In this example, we use the isinstance() function to check if data is an instance of the Iterable class from the collections.abc module.
If data is iterable, the code will print “Data is iterable”. Otherwise, it will print “Data is not iterable”.