Say, you’re me 👱♂️ five minutes ago, and you want to create a Matplotlib plot using the following (genius) code snippet:
import matplotlib.pyplot as plt fig, axes = plt.subplots() axes[0, 0].plot([1, 2, 3], [9, 8, 7]) plt.show()
If you run this code, instead of the desired plot, you get the following
TypeError: 'AxesSubplot' object is not subscriptable:
Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 4, in <module> axes[0, 0].plot([1, 2, 3], [5, 5, 5]) TypeError: 'AxesSubplot' object is not subscriptable
💬 Question: How to resolve the
TypeError: 'AxesSubplot' object is not subscriptable in your Python script?
Don’t panic! 📘 The solution is easier than you think…
Fix Not Subscriptable TypeError on ‘AxesSubplot’ Object
💡 Generally, Python raises the
TypeError XXX object is not subscriptable if you use indexing with the square bracket notation on an object that is not indexable. In this case, you tried to index an
Axes object because you thought it was an array of
Let’s go over the code to understand why the error happened!
First, you assign the result of the
plt.subplots() function to the two variables
fig, axes = plt.subplots()
If you don’t pass an argument in the
plt.subplots() function, it creates a
Figure with one
So if you try to subscript using
axes, or any other indexing scheme, Python will raise an error. It’s simple:
axes doesn’t hold a container type so it cannot be indexed using the square bracket notation!
So to fix the
TypeError: 'AxesSubplot' object is not subscriptable, simply remove the indexing notation on the axes object obtained by
plt.subplots() called without arguments.
import matplotlib.pyplot as plt fig, axes = plt.subplots() axes.plot([1, 2, 3], [9, 8, 7]) # not: axes[0, 0] plt.show()
Now it works — here’s the output:
What is the Reason for the Error?
However, this error is tough to spot because if you pass any other argument into the
plt.subplot() function, it creates a
Figure and a Numpy array of
Axes objects which you store in
For example, this creates a non-subscriptable axes because you don’t pass any argument:
fig, axes = plt.subplots()
For example, this creates a subscriptable array of axes that is a one-dimensional array of subplots because you pass an argument:
fig, axes = plt.subplots(3)
For example, this creates a subscriptable array of axes that is a two-dimensional array of subplots because you passed two arguments
fig, axes = plt.subplots(3, 2)
No wonder did you think that you can call
axes on the return value of the
plt.subplot() function! However, doing so is only possible if you didn’t pass an argument into it.
Make sure you never run into similar errors by spending a couple of minutes understanding the
plt.subplot() function once and for all!
Learn More about plt.subplot()
To further understand the
subplots() function, check out our detailed guide on the Finxter blog and the following video:
🌍 Full Tutorial: Matplotlib Subplots – A Helpful Illustrated Guide
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.