Sick Gaming
[Tut] [Fixed] Matplotlib: TypeError: ‘AxesSubplot’ object is not subscriptable - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.sickgaming.net/forum-83.html)
+--- Thread: [Tut] [Fixed] Matplotlib: TypeError: ‘AxesSubplot’ object is not subscriptable (/thread-99906.html)



[Tut] [Fixed] Matplotlib: TypeError: ‘AxesSubplot’ object is not subscriptable - xSicKxBot - 09-05-2022

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

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;641705&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<h2>Problem Formulation</h2>
<p>Say, you’re me <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f471-200d-2642-fe0f.png" alt="?‍♂️" class="wp-smiley" style="height: 1em; max-height: 1em;" /> five minutes ago, and you want to create a <a href="https://blog.finxter.com/matplotlib-full-guide/" data-type="post" data-id="20151" target="_blank" rel="noreferrer noopener">Matplotlib</a> plot using the following (genius) code snippet:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3,4" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import matplotlib.pyplot as plt fig, axes = plt.subplots()
axes[0, 0].plot([1, 2, 3], [9, 8, 7])
plt.show()
</pre>
<p>If you run this code, instead of the desired plot, you get the following <code>TypeError: 'AxesSubplot' object is not subscriptable</code>:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="4" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 4, in &lt;module> axes[0, 0].plot([1, 2, 3], [5, 5, 5])
TypeError: 'AxesSubplot' object is not subscriptable</pre>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: How to resolve the <code>TypeError: 'AxesSubplot' object is not subscriptable</code> in your Python script?</p>
<p>Don’t panic! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4d8.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> The solution is easier than you think…</p>
<h2>Fix Not Subscriptable TypeError on ‘AxesSubplot’ Object</h2>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Generally, Python raises the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-typeerror-nonetype-object-is-not-subscriptable/" data-type="URL" data-id="https://blog.finxter.com/python-typeerror-nonetype-object-is-not-subscriptable/" target="_blank">TypeError XXX object is not subscriptable</a></code> if you use indexing with the <a rel="noreferrer noopener" href="https://blog.finxter.com/slice-notation-a-simple-illustrated-guide/" data-type="post" data-id="14152" target="_blank">square bracket notation</a> on an object that is not indexable. In this case, you tried to index an <code>Axes</code> object because you thought it was an array of <code>Axes</code> objects.</p>
<p>Let’s go over the code to understand why the error happened! </p>
<p>First, you assign the result of the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/matplotlib-subplots/" data-type="post" data-id="6103" target="_blank">plt.subplots()</a></code> function to the two variables <code>fig</code> and <code>axes</code>. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">fig, axes = plt.subplots()</pre>
<p>If you don’t pass an argument in the <code>plt.subplots()</code> function, it creates a <code>Figure</code> with one <code>Axes</code> object. </p>
<p class="has-base-background-color has-background">So if you try to subscript using <code>axes[0,0]</code>, <code>axes[0]</code>, or any other <a href="https://blog.finxter.com/daily-python-puzzle-list-indexing/" data-type="post" data-id="84">inde</a><a rel="noreferrer noopener" href="https://blog.finxter.com/daily-python-puzzle-list-indexing/" data-type="post" data-id="84" target="_blank">x</a><a href="https://blog.finxter.com/daily-python-puzzle-list-indexing/" data-type="post" data-id="84">ing scheme</a>, Python will raise an error. It’s simple: <code>axes</code> doesn’t hold a container type so it cannot be indexed using the square bracket notation!</p>
<p><strong>So to fix the <code>TypeError: 'AxesSubplot' object is not subscriptable</code>, simply remove the indexing notation on the axes object obtained by <code>plt.subplots()</code> called without arguments.</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="4" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import matplotlib.pyplot as plt fig, axes = plt.subplots()
axes.plot([1, 2, 3], [9, 8, 7]) # not: axes[0, 0]
plt.show()
</pre>
<p>Now it works — here’s the output:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="598" height="446" src="https://blog.finxter.com/wp-content/uploads/2022/09/image-1.png" alt="" class="wp-image-642110" srcset="https://blog.finxter.com/wp-content/uploads/2022/09/image-1.png 598w, https://blog.finxter.com/wp-content/uploads/2022/09/image-1-300x224.png 300w" sizes="(max-width: 598px) 100vw, 598px" /></figure>
</div>
<h2>What is the Reason for the Error?</h2>
<p class="has-global-color-8-background-color has-background"><strong>However, this error is tough to spot because if you pass any other argument into the <code>plt.subplot()</code> function, it creates a <code>Figure</code> and a <a href="https://blog.finxter.com/what-are-advantages-of-numpy-over-regular-python-lists/" target="_blank" rel="noreferrer noopener">Numpy array</a> of <code>Subplot</code>/<code>Axes</code> objects which you store in <code>fig</code> and <code>axes</code> respectively.</strong></p>
<p>For example, this creates a <strong>non-subscriptable axes</strong> because you don’t pass any argument:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">fig, axes = plt.subplots()</pre>
<p>For example, this creates a <strong>subscriptable array of axes</strong> that is a one-dimensional array of subplots because you pass an argument:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">fig, axes = plt.subplots(3)</pre>
<p>For example, this creates a <strong>subscriptable array of axes</strong> that is a two-dimensional array of subplots because you passed two arguments</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">fig, axes = plt.subplots(3, 2)</pre>
<p>No wonder did you think that you can call <code>axes[0,0]</code> or <code>axes[0]</code> on the return value of the <code>plt.subplot()</code> function! However, doing so is only possible if you didn’t pass an argument into it.</p>
<p>Make sure you never run into similar errors by spending a couple of minutes understanding the <code>plt.subplot()</code> function once and for all!</p>
<h2>Learn More about plt.subplot()</h2>
<p>To further understand the <code>subplots()</code> function, check out our detailed guide on the Finxter blog and the following video:</p>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/fixed-matplotlib-typeerror-axessubplot-object-is-not-subscriptable/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FB9NTkxrOXjk%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Full Tutorial</strong>: <a href="https://blog.finxter.com/matplotlib-subplots/" data-type="post" data-id="6103">Matplotlib Subplots – A Helpful Illustrated Guide</a></p>
</div>


https://www.sickgaming.net/blog/2022/09/03/fixed-matplotlib-typeerror-axessubplot-object-is-not-subscriptable/