{"id":127803,"date":"2022-09-03T14:44:26","date_gmt":"2022-09-03T14:44:26","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=641705"},"modified":"2022-09-03T14:44:26","modified_gmt":"2022-09-03T14:44:26","slug":"fixed-matplotlib-typeerror-axessubplot-object-is-not-subscriptable","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/09\/03\/fixed-matplotlib-typeerror-axessubplot-object-is-not-subscriptable\/","title":{"rendered":"[Fixed] Matplotlib: TypeError: \u2018AxesSubplot\u2019 object is not subscriptable"},"content":{"rendered":"\n<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;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<h2>Problem Formulation<\/h2>\n<p>Say, you&#8217;re me <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f471-200d-2642-fe0f.png\" alt=\"\ud83d\udc71\u200d\u2642\ufe0f\" 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>\n<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()\naxes[0, 0].plot([1, 2, 3], [9, 8, 7])\nplt.show()\n<\/pre>\n<p>If you run this code, instead of the desired plot, you get the following <code>TypeError: 'AxesSubplot' object is not subscriptable<\/code>:<\/p>\n<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])\nTypeError: 'AxesSubplot' object is not subscriptable<\/pre>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" 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>\n<p>Don&#8217;t panic! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4d8.png\" alt=\"\ud83d\udcd8\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> The solution is easier than you think&#8230;<\/p>\n<h2>Fix Not Subscriptable TypeError on &#8216;AxesSubplot&#8217; Object<\/h2>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" 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>\n<p>Let&#8217;s go over the code to understand why the error happened! <\/p>\n<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>\n<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>\n<p>If you don&#8217;t pass an argument in the <code>plt.subplots()<\/code> function, it creates a <code>Figure<\/code> with one <code>Axes<\/code> object. <\/p>\n<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&#8217;s simple: <code>axes<\/code> doesn&#8217;t hold a container type so it cannot be indexed using the square bracket notation!<\/p>\n<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>\n<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()\naxes.plot([1, 2, 3], [9, 8, 7]) # not: axes[0, 0]\nplt.show()\n<\/pre>\n<p>Now it works &#8212; here&#8217;s the output:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 598px) 100vw, 598px\" \/><\/figure>\n<\/div>\n<h2>What is the Reason for the Error?<\/h2>\n<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>\n<p>For example, this creates a <strong>non-subscriptable axes<\/strong> because you don&#8217;t pass any argument:<\/p>\n<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>\n<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>\n<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>\n<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>\n<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>\n<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&#8217;t pass an argument into it.<\/p>\n<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>\n<h2>Learn More about plt.subplot()<\/h2>\n<p>To further understand the <code>subplots()<\/code> function, check out our detailed guide on the Finxter blog and the following video:<\/p>\n<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 decoding=\"async\" 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>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" 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 \u2013 A Helpful Illustrated Guide<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Problem Formulation Say, you&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-127803","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127803","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=127803"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127803\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=127803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=127803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=127803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}