Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] [Fixed] Matplotlib: TypeError: ‘AxesSubplot’ object is not subscriptable
1
[Fixed] Matplotlib: TypeError: ‘AxesSubplot’ object is not subscriptable

5/5 – (1 vote)

Problem Formulation

Say, you’re me [Image: 1f471-200d-2642-fe0f.png] 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 subscriptable
:

Code:
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

[Image: 1f4ac.png] Question: How to resolve the
Code:
TypeError: 'AxesSubplot' object is not subscriptable
in your Python script?

Don’t panic! [Image: 1f4d8.png] The solution is easier than you think…

Fix Not Subscriptable TypeError on ‘AxesSubplot’ Object

[Image: 1f4a1.png] Generally, Python raises the
Code:
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
Code:
Axes
object because you thought it was an array of
Code:
Axes
objects.

Let’s go over the code to understand why the error happened!

First, you assign the result of the
Code:
plt.subplots()
function to the two variables
Code:
fig
and
Code:
axes
.

Code:
fig, axes = plt.subplots()

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

So if you try to subscript using
Code:
axes[0,0]
,
Code:
axes[0]
, or any other indexing scheme, Python will raise an error. It’s simple:
Code:
axes
doesn’t hold a container type so it cannot be indexed using the square bracket notation!

So to fix the
Code:
TypeError: 'AxesSubplot' object is not subscriptable
, simply remove the indexing notation on the axes object obtained by
Code:
plt.subplots()
called without arguments.


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]

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()
function, it creates a
Code:
Figure
and a Numpy array of
Code:
Subplot
/
Code:
Axes
objects which you store in
Code:
fig
and
Code:
axes
respectively.


For 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]
or
Code:
axes[0]
on the return value of the
Code:
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
Code:
plt.subplot()
function once and for all!

Learn More about plt.subplot()

To further understand the
Code:
subplots()
function, check out our detailed guide on the Finxter blog and the following video:

[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]

[Image: 1f30d.png] Full Tutorial: Matplotlib Subplots – A Helpful Illustrated Guide
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: