[Fixed] Matplotlib: TypeError: ‘AxesSubplot’ object is not subscriptable

Problem Formulation

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 Axes objects.

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 and axes.

fig, axes = plt.subplots()

If you don’t pass an argument in the plt.subplots() function, it creates a Figure with one Axes object.

So if you try to subscript using axes[0,0], axes[0], 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 Subplot/Axes objects which you store in fig and axes respectively.

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[0,0] or axes[0] 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