09-05-2022, 09:33 AM
[Fixed] Matplotlib: TypeError: ‘AxesSubplot’ object is not subscriptable
5/5 – (1 vote)
Problem Formulation
Say, you’re me
five minutes ago, and you want to create a Matplotlib plot using the following (genius) code snippet:
If you run this code, instead of the desired plot, you get the following:
Question: How to resolve the 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 if you use indexing with the square bracket notation on an object that is not indexable. In this case, you tried to index an object because you thought it was an array of objects.
Let’s go over the code to understand why the error happened!
First, you assign the result of the function to the two variables and .
If you don’t pass an argument in the function, it creates a with one object.
So if you try to subscript using, , or any other indexing scheme, Python will raise an error. It’s simple: doesn’t hold a container type so it cannot be indexed using the square bracket notation!
So to fix the, simply remove the indexing notation on the axes object obtained by called without arguments.
Now it works — here’s the output:
![[Image: image-1.png]](https://blog.finxter.com/wp-content/uploads/2022/09/image-1.png)
What is the Reason for the Error?
However, this error is tough to spot because if you pass any other argument into the function, it creates a and a Numpy array of / objects which you store in and respectively.
For example, this creates a non-subscriptable axes because you don’t pass any argument:
For example, this creates a subscriptable array of axes that is a one-dimensional array of subplots because you pass an argument:
For example, this creates a subscriptable array of axes that is a two-dimensional array of subplots because you passed two arguments
No wonder did you think that you can call or on the return value of the 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 function once and for all!
Learn More about plt.subplot()
To further understand the function, check out our detailed guide on the Finxter blog and the following video:
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FB9NTkxrOXjk%2Fhqdefault.jpg)
Full Tutorial: Matplotlib Subplots – A Helpful Illustrated Guide
5/5 – (1 vote)
Problem Formulation
Say, you’re me
five minutes ago, and you want to create a Matplotlib plot using the following (genius) code snippet:Code:
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
Code:
TypeError: 'AxesSubplot' object is not subscriptableCode:
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 Code:
TypeError: 'AxesSubplot' object is not subscriptableDon’t panic!
The solution is easier than you think…Fix Not Subscriptable TypeError on ‘AxesSubplot’ Object
Generally, Python raises the Code:
TypeError XXX object is not subscriptableCode:
AxesCode:
AxesLet’s go over the code to understand why the error happened!
First, you assign the result of the
Code:
plt.subplots()Code:
figCode:
axesCode:
fig, axes = plt.subplots()If you don’t pass an argument in the
Code:
plt.subplots()Code:
FigureCode:
AxesSo if you try to subscript using
Code:
axes[0,0]Code:
axes[0]Code:
axesSo to fix the
Code:
TypeError: 'AxesSubplot' object is not subscriptableCode:
plt.subplots()Code:
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:
![[Image: image-1.png]](https://blog.finxter.com/wp-content/uploads/2022/09/image-1.png)
What is the Reason for the Error?
However, this error is tough to spot because if you pass any other argument into the
Code:
plt.subplot()Code:
FigureCode:
SubplotCode:
AxesCode:
figCode:
axesFor example, this creates a non-subscriptable axes because you don’t pass any argument:
Code:
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:
Code:
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
Code:
fig, axes = plt.subplots(3, 2)No wonder did you think that you can call
Code:
axes[0,0]Code:
axes[0]Code:
plt.subplot()Make sure you never run into similar errors by spending a couple of minutes understanding the
Code:
plt.subplot()Learn More about plt.subplot()
To further understand the
Code:
subplots()
Full Tutorial: Matplotlib Subplots – A Helpful Illustrated Guide

