You get AttributeError: module ‘matplotlib.cbook’ has no attribute ‘iterable’ error, usually when you tried to import networkx module and try to plot graph. Sometimes the error can be very frustrating but believe me it is very simple and easy to solve it. In this short article, we will learn how we can solve module matplotlib.cbook has no attribute iterable error using various methods. Moreover, we will also discuss how we can interpret and understand the errors in Python taking AttributeError: module ‘matplotlib.cbook’ has no attribute ‘iterable’ as an example.
AttributeError: module ‘matplotlib.cbook’ has no attribute ‘iterable’
Most of the time many people get matplotlib.cbook has no attribute iterable error when they tried to import the newtorkx module. Let us take an example and see why we get this error:
# importing the networkx import networkx as nx # creating graph G = nx.complete_graph(5) # drawing the network nx.draw_networkx(G)
Sometimes, importing networkx module causes AttributeError: module ‘matplotlib.cbook’ has no attribute ‘iterable’ error:
Now let us see, how we can solve the problem using various methods.
The error can be solved by the following methods:
- Degrading the matplotlib module
- Upgrading the networkx module
- Creating a virtual environment
Let us go through each of the methods in detail.
Solution-1: Degrading the matplotlib module
One of the reasons why we are getting AttributeError: module ‘matplotlib.cbook’ has no attribute ‘iterable’ error can be because of differences in the versions. So, first, try to degrade the version of matplotlib to 2.2.3.
Here is how you can degrade the matplotlib module using various commands:
# if you are using the pip command pip install matplotlib==2.2.3 # in case you are using pip3 pip3 install matplotlib==2.2.3 # if you are using jupyter notebook !pip install matplotlib==2.2.3 # for anaconda users conda install matplotlib==2.2.3 # for linux users sudo apt install matplotlib==2.2.3
Hopefully, this will help you to get rid of the error:
Solution-2: Upgrading the networkx module
Another method to get rid of AttributeError: module ‘matplotlib.cbook’ has no attribute ‘iterable’ error is to upgrade the networkx module. You can use the following commands to upgrade the networkx on your system.
# using the pip command pip install --upgrade networkx # using the pip3 command pip3 install --upgrade networkx # for jupyter notebook users !pip install --upgrade networkx # for anaconda envrionment conda install --upgrade networkx # for linux users sudo apt install --upgrade networkx
Hope this method will help you to get rid of the error matplotlib.cbook has no attribute iterable.
Solution-3: Create a virtual environment and install the modules
If the above methods still do not help you to get rid of the error, then you can use the virtual environment to install the modules there and use them.
Use the following commands to create a virtual environment on your system:
# use correct version of Python when creating VENV python3 -m venv venv # activate on Unix or MacOS source venv/bin/activate # activate on Windows venv\Scripts\activate.bat # activate on Windows (PowerShell) venv\Scripts\Activate.ps1
Once you have created the virtual environment, you can then install the modules on your system:
# installing the matplotlib pip install matplotlib # installing networkx pip install networkx
Now, you can use the installed modules on your system without getting any errors:
Understanding AttributeError: module ‘matplotlib.cbook’ has no attribute ‘iterable’
Let us understand errors in Python taking AttributeError: module ‘matplotlib.cbook’ has no attribute ‘iterable’ as an example. In Python, errors usually have two main parts. The first part of the error gives information about the category of the error which in our case is the AttributeError which means the attribute that we are using with the module or function is not correct.
The second part of the error gives more specific information about the error. In our example, the error clearly says matplotlib.cbook has no attribute iterable.
What is Matplotlib.cbook?
It is always better to understand what the error means. So, if you still facing issues and the above-mentioned methods didn’t work for you then it is better to understand the meaning of the error.
matplotlib.cbook is a module in the matplotlib library in Python. It contains a collection of utility functions that are used throughout the matplotlib library. This module is safe to import from anywhere within Matplotlib; it imports Matplotlib only at runtime.
Some of the functions available in matplotlib.cbook are listed here:
- matplotlib.cbook.CallbackRegistry: Handle registering, processing, blocking, and disconnecting for a set of signals and callbacks.
- matplotlib.cbook.blocked: Block callback signals from being processed.
- connect: Register func to be called when signal signal is generated.
- disconnect: Disconnect the callback registered with callback id cid.
- process: Process signal s.
- clean: Clean dead weak references from the dictionary.
- join: Join given arguments into the same set. Accepts one or more arguments.
- joined: Return whether a and b are members of the same set.
You can learn more about the matplotlib.cbook from their official documentation.
How to install networkx in Jupyter notebook?
To install the networkx module in Jupyter notebook you can use the following commands:
!pip install networkx
How to install networkx on ubuntu?
Similar to other modules, you can use the sudo to install the networkx on the ubuntu operating system.
sudo apt install networkx
What is AttributeError in Python?
Attribute error in Python is a type of error that occurs when we assigned the wrong attribute to the function.
How to check if an object has the attribute or not?
You can use the hasattr() function in order to check if an object has the given attribute or not:
if hasattr(a, 'property'): a.property
In this short article, we learned how we can handleAttributeError: module ‘matplotlib.cbook’ has no attribute ‘iterable’error using two different methods. This error is usually because of the difference in the versions so we can either upgrade the networkx or degrade the matplotlib module to fix this error.
- Learn how to visualize data in Matpltolib
- Learn how to visualize data using pandas
- Visualize data with interactive 3d plots