Create an account


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 ?‍♂️ 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:

YouTube Video

? Full Tutorial: Matplotlib Subplots – A Helpful Illustrated Guide



https://www.sickgaming.net/blog/2022/09/...criptable/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] [Fixed] Access Denied – OpenAI Error Reference Number 1020 xSicKxBot 0 1,419 08-10-2023, 09:24 PM
Last Post: xSicKxBot
  [Tut] [Fixed] Access Denied – OpenAI Error Reference Number 1020 xSicKxBot 0 1,389 05-25-2023, 04:45 PM
Last Post: xSicKxBot
  [Tut] Pandas Series Object – A Helpful Guide with Examples xSicKxBot 0 1,313 05-01-2023, 01:30 AM
Last Post: xSicKxBot
  [Tut] [Fixed] Sendy Loading Lists Not Working (Loads Forever) xSicKxBot 0 1,204 02-14-2023, 08:12 PM
Last Post: xSicKxBot
  [Tut] (Fixed) ModuleNotFoundError: No Module Named ‘git’ | Python xSicKxBot 0 1,214 11-22-2022, 07:46 PM
Last Post: xSicKxBot
  [Tut] ModuleNotFoundError: No Module Named ‘ffmpeg’ (Fixed) xSicKxBot 0 1,267 11-19-2022, 12:20 PM
Last Post: xSicKxBot
  [Tut] [Fixed] Python ModuleNotFoundError: No Module Named ‘readline’ xSicKxBot 0 1,281 11-11-2022, 10:01 PM
Last Post: xSicKxBot
  [Tut] Python TypeError: ‘dict_keys’ Not Subscriptable (Fix This Stupid Bug) xSicKxBot 0 1,220 10-17-2022, 03:24 AM
Last Post: xSicKxBot
  [Tut] (Fixed) Python TypeError ‘bool’ object is not subscriptable xSicKxBot 0 1,443 10-02-2022, 12:23 PM
Last Post: xSicKxBot
  [Tut] 3 Best Ways to Generate a Random Number with a Fixed Amount of Digits in Python xSicKxBot 0 1,222 09-27-2022, 10:20 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016