<h3 dir="auto">Bug report</h3>
<p dir="auto"><strong>Bug summary</strong></p>
<p dir="auto">Details copied from <a href="https://stackoverflow.com/q/62363069/5472354" rel="nofollow">here</a>, as I believe this is a bug:</p>
<p dir="auto">I understand that <code class="notranslate">constrained_layout</code> is still experimental in <code class="notranslate">matplotlib</code>, nevertheless, I found this odd behaviour, which I guess is a bug and I wonder how to circumvent it.</p>
<p dir="auto">If you run <code class="notranslate">re_add_colorbar_axes_as_subplot</code> with <code class="notranslate">constrained_layout=False</code>, there are no complaints, but if you set <code class="notranslate">constrained_layout=True</code>, it raises <code class="notranslate">TypeError: int() argument must be a string, a bytes-like object or a number, not 'Axes'</code>, which is traced back to the following line in <a href="https://matplotlib.org/3.2.1/_modules/matplotlib/axes/_subplots.html#SubplotBase" rel="nofollow"><code class="notranslate">matplotlib.axes.SubplotBase</code></a>:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="s = str(int(args[0]))"><pre class="notranslate"><span class="pl-s1">s</span> <span class="pl-c1">=</span> <span class="pl-en">str</span>(<span class="pl-en">int</span>(<span class="pl-s1">args</span>[<span class="pl-c1">0</span>]))</pre></div>
<p dir="auto">Per the documentation, you can see that <code class="notranslate">SubplotBase</code> expected something completely different:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="    *args : tuple (*nrows*, *ncols*, *index*) or int
    The array of subplots in the figure has dimensions ``(nrows,
    ncols)``, and *index* is the index of the subplot being created.
    *index* starts at 1 in the upper left corner and increases to the
    right."><pre class="notranslate">    <span class="pl-c1">*</span><span class="pl-s1">args</span> : <span class="pl-en">tuple</span> (<span class="pl-c1">*</span><span class="pl-s1">nrows</span><span class="pl-c1">*</span>, <span class="pl-c1">*</span><span class="pl-s1">ncols</span><span class="pl-c1">*</span>, <span class="pl-c1">*</span><span class="pl-s1">index</span><span class="pl-c1">*</span>) <span class="pl-c1">or</span> <span class="pl-s1">int</span>
    <span class="pl-v">The</span> <span class="pl-s1">array</span> <span class="pl-s1">of</span> <span class="pl-s1">subplots</span> <span class="pl-c1">in</span> <span class="pl-s1">the</span> <span class="pl-s1">figure</span> <span class="pl-s1">has</span> <span class="pl-s1">dimensions</span> <span class="pl-s">``</span>(<span class="pl-s1">nrows</span>,
    <span class="pl-s1">ncols</span>)<span class="pl-s">``</span>, <span class="pl-s1">and</span> <span class="pl-c1">*</span><span class="pl-s1">index</span><span class="pl-c1">*</span> <span class="pl-s1">is</span> <span class="pl-s1">the</span> <span class="pl-s1">index</span> <span class="pl-s1">of</span> <span class="pl-s1">the</span> <span class="pl-s1">subplot</span> <span class="pl-s1">being</span> <span class="pl-s1">created</span>.
    <span class="pl-c1">*</span><span class="pl-s1">index</span><span class="pl-c1">*</span> <span class="pl-s1">starts</span> <span class="pl-s1">at</span> <span class="pl-c1">1</span> <span class="pl-c1">in</span> <span class="pl-s1">the</span> <span class="pl-s1">upper</span> <span class="pl-s1">left</span> <span class="pl-s1">corner</span> <span class="pl-c1">and</span> <span class="pl-s1">increases</span> <span class="pl-s1">to</span> <span class="pl-s1">the</span>
    <span class="pl-s1">right</span>.</pre></div>
<p dir="auto">So I wonder what went wrong here by using constrained layout and the axes auto-generated by the <code class="notranslate">colorbar</code>-method, because if you re-add a subplot the same way, there is no issue.</p>
<p dir="auto">The reason why I'm going through this trouble is because I would like the colorbar axes to be added to the figure as a separate <code class="notranslate">AxesSubplot</code> instance, and not just as a 'regular' <code class="notranslate">Axes</code> instance. I know you can pass the optional <code class="notranslate">cax</code> argument to the <code class="notranslate">colorbar</code>-method which allows you to determine which axes the colorbar is plotted in, as demonstrated in <code class="notranslate">add_another_subplot_fist</code>, but I don't know how I can get this to look properly.</p>
<p dir="auto"><strong>Code for reproduction</strong></p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import matplotlib.pyplot as plt
import numpy as np


def re_add_colorbar_axes_as_subplot(image, constrained_layout=False):
    fig, ax = plt.subplots(constrained_layout=constrained_layout)
    image_artist = ax.imshow(image)

    colorbar = fig.colorbar(image_artist)
    colorbar.ax.remove()
    colorbar.ax.figure = fig  # not sure why this is actually necessary either
    fig.add_subplot(colorbar.ax)

    fig.show()


def use_gridspec_keyword(image, constrained_layout=False):
    fig, ax = plt.subplots(constrained_layout=constrained_layout)
    image_artist = ax.imshow(image)

    colorbar = fig.colorbar(image_artist, ax=ax, use_gridspec=True)
    print(colorbar.ax)  # AxesSubplot instance if constained_layout=False,
    # Axes instance if constrained_layout=True
    fig.show()


def add_another_subplot_fist(image, constrained_layout=False):
    fig = plt.figure(constrained_layout=constrained_layout)
    ax = fig.add_subplot(121)
    image_artist = ax.imshow(image)

    ax2 = fig.add_subplot(122)
    fig.colorbar(image_artist, cax=ax2)
    # conversely, there is no problem when using constrained layout
    # and re-adding this subplot
    # ax2.remove()
    # ax2.figure = fig
    # fig.add_subplot(ax2)
    fig.show()


if __name__ == '__main__':
    image = np.random.uniform(0, 1, (100, 100))

    # re_add_colorbar_axes_as_subplot(image, True)
    # add_another_subplot_fist(image, True)
    use_gridspec_keyword(image, True)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</span>
<span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>


<span class="pl-k">def</span> <span class="pl-en">re_add_colorbar_axes_as_subplot</span>(<span class="pl-s1">image</span>, <span class="pl-s1">constrained_layout</span><span class="pl-c1">=</span><span class="pl-c1">False</span>):
    <span class="pl-s1">fig</span>, <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">subplots</span>(<span class="pl-s1">constrained_layout</span><span class="pl-c1">=</span><span class="pl-s1">constrained_layout</span>)
    <span class="pl-s1">image_artist</span> <span class="pl-c1">=</span> <span class="pl-s1">ax</span>.<span class="pl-en">imshow</span>(<span class="pl-s1">image</span>)

    <span class="pl-s1">colorbar</span> <span class="pl-c1">=</span> <span class="pl-s1">fig</span>.<span class="pl-en">colorbar</span>(<span class="pl-s1">image_artist</span>)
    <span class="pl-s1">colorbar</span>.<span class="pl-s1">ax</span>.<span class="pl-en">remove</span>()
    <span class="pl-s1">colorbar</span>.<span class="pl-s1">ax</span>.<span class="pl-s1">figure</span> <span class="pl-c1">=</span> <span class="pl-s1">fig</span>  <span class="pl-c"># not sure why this is actually necessary either</span>
    <span class="pl-s1">fig</span>.<span class="pl-en">add_subplot</span>(<span class="pl-s1">colorbar</span>.<span class="pl-s1">ax</span>)

    <span class="pl-s1">fig</span>.<span class="pl-en">show</span>()


<span class="pl-k">def</span> <span class="pl-en">use_gridspec_keyword</span>(<span class="pl-s1">image</span>, <span class="pl-s1">constrained_layout</span><span class="pl-c1">=</span><span class="pl-c1">False</span>):
    <span class="pl-s1">fig</span>, <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">subplots</span>(<span class="pl-s1">constrained_layout</span><span class="pl-c1">=</span><span class="pl-s1">constrained_layout</span>)
    <span class="pl-s1">image_artist</span> <span class="pl-c1">=</span> <span class="pl-s1">ax</span>.<span class="pl-en">imshow</span>(<span class="pl-s1">image</span>)

    <span class="pl-s1">colorbar</span> <span class="pl-c1">=</span> <span class="pl-s1">fig</span>.<span class="pl-en">colorbar</span>(<span class="pl-s1">image_artist</span>, <span class="pl-s1">ax</span><span class="pl-c1">=</span><span class="pl-s1">ax</span>, <span class="pl-s1">use_gridspec</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
    <span class="pl-en">print</span>(<span class="pl-s1">colorbar</span>.<span class="pl-s1">ax</span>)  <span class="pl-c"># AxesSubplot instance if constained_layout=False,</span>
    <span class="pl-c"># Axes instance if constrained_layout=True</span>
    <span class="pl-s1">fig</span>.<span class="pl-en">show</span>()


<span class="pl-k">def</span> <span class="pl-en">add_another_subplot_fist</span>(<span class="pl-s1">image</span>, <span class="pl-s1">constrained_layout</span><span class="pl-c1">=</span><span class="pl-c1">False</span>):
    <span class="pl-s1">fig</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">figure</span>(<span class="pl-s1">constrained_layout</span><span class="pl-c1">=</span><span class="pl-s1">constrained_layout</span>)
    <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">fig</span>.<span class="pl-en">add_subplot</span>(<span class="pl-c1">121</span>)
    <span class="pl-s1">image_artist</span> <span class="pl-c1">=</span> <span class="pl-s1">ax</span>.<span class="pl-en">imshow</span>(<span class="pl-s1">image</span>)

    <span class="pl-s1">ax2</span> <span class="pl-c1">=</span> <span class="pl-s1">fig</span>.<span class="pl-en">add_subplot</span>(<span class="pl-c1">122</span>)
    <span class="pl-s1">fig</span>.<span class="pl-en">colorbar</span>(<span class="pl-s1">image_artist</span>, <span class="pl-s1">cax</span><span class="pl-c1">=</span><span class="pl-s1">ax2</span>)
    <span class="pl-c"># conversely, there is no problem when using constrained layout</span>
    <span class="pl-c"># and re-adding this subplot</span>
    <span class="pl-c"># ax2.remove()</span>
    <span class="pl-c"># ax2.figure = fig</span>
    <span class="pl-c"># fig.add_subplot(ax2)</span>
    <span class="pl-s1">fig</span>.<span class="pl-en">show</span>()


<span class="pl-k">if</span> <span class="pl-s1">__name__</span> <span class="pl-c1">==</span> <span class="pl-s">'__main__'</span>:
    <span class="pl-s1">image</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">uniform</span>(<span class="pl-c1">0</span>, <span class="pl-c1">1</span>, (<span class="pl-c1">100</span>, <span class="pl-c1">100</span>))

    <span class="pl-c"># re_add_colorbar_axes_as_subplot(image, True)</span>
    <span class="pl-c"># add_another_subplot_fist(image, True)</span>
    <span class="pl-en">use_gridspec_keyword</span>(<span class="pl-s1">image</span>, <span class="pl-c1">True</span>)</pre></div>
<p dir="auto"><strong>EDIT</strong></p>
<p dir="auto">Ok, I just realized that you can pass the <code class="notranslate">use_gridspec</code> keyword to the <code class="notranslate">colorbar</code>-method. From the <a href="https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.colorbar.html" rel="nofollow">docs</a>:</p>
<blockquote>
<p dir="auto">If cax is None, a new cax is created as an instance of Axes. If ax is an instance of Subplot and use_gridspec is True, cax is created as an instance of Subplot using the gridspec module.</p>
</blockquote>
<p dir="auto">But for some reason, this keyword seems to be ignored when using constrained layout.</p>
<p dir="auto"><strong>Matplotlib version</strong></p>

<ul dir="auto">
<li>Operating system: Windows 10 Home 64bit</li>
<li>Matplotlib version: 3.1.3 (via Anaconda)</li>
<li>Matplotlib backend: backend_interagg</li>
<li>Python version: 3.7.7</li>
</ul>
<p dir="auto">Ahoy!<br>
I have recently updated matplotlib through conda to version <code class="notranslate">14.0</code> (<code class="notranslate">np19py27_0</code>).<br>
I am using windows 8, python 2.7.7 (Anaconda distribution 2.0.2 32-bit)<br>
Unfortunatelly my windows user name contains non-ascii caracter (<code class="notranslate">ś</code>) and importing matplotlib gives errors. <g-emoji class="g-emoji" alias="cry" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f622.png">😢</g-emoji> (previous matplotlib versions I used did not have any problem with this)</p>
<p dir="auto">The error comes in two flavors:<br>
<g-emoji class="g-emoji" alias="black_circle" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/26ab.png">⚫</g-emoji> If my current working directory contains non-ascii character then:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
&lt;ipython-input-1-82be63b7783c&gt; in &lt;module&gt;()
----&gt; 1 import matplotlib

D:\Python\Anaconda\lib\site-packages\matplotlib\__init__.py in &lt;module&gt;()
   1046 
   1047 # this is the instance used by the matplotlib classes
-&gt; 1048 rcParams = rc_params()
   1049 
   1050 if rcParams['examples.directory']:

D:\Python\Anaconda\lib\site-packages\matplotlib\__init__.py in rc_params(fail_on_error)
    895     default matplotlib rc file.
    896     &quot;&quot;&quot;
--&gt; 897     fname = matplotlib_fname()
    898     if not os.path.exists(fname):
    899         # this should never happen, default in mpl-data should always be found

D:\Python\Anaconda\lib\site-packages\matplotlib\__init__.py in matplotlib_fname()
    746       system-defined copy.
    747     &quot;&quot;&quot;
--&gt; 748     fname = os.path.join(os.getcwd(), 'matplotlibrc')
    749     if os.path.exists(fname):
    750         return fname

D:\Python\Anaconda\lib\ntpath.pyc in join(path, *paths)
     82         if result_path and result_path[-1] not in '\\/':
     83             result_path = result_path + '\\'
---&gt; 84         result_path = result_path + p_path
     85     return result_drive + result_path
     86 

UnicodeDecodeError: 'ascii' codec can't decode byte 0x9c in position 11: ordinal not in range(128)"><pre class="notranslate"><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span>
<span class="pl-v">UnicodeDecodeError</span>                        <span class="pl-v">Traceback</span> (<span class="pl-s1">most</span> <span class="pl-s1">recent</span> <span class="pl-s1">call</span> <span class="pl-s1">last</span>)
<span class="pl-c1">&lt;</span><span class="pl-s1">ipython</span><span class="pl-c1">-</span><span class="pl-s1">input</span><span class="pl-c1">-</span><span class="pl-c1">1</span><span class="pl-c1">-</span><span class="pl-c1">82</span><span class="pl-s1">be63b7783c</span><span class="pl-c1">&gt;</span> <span class="pl-s1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>()
<span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">&gt;</span> <span class="pl-c1">1</span> <span class="pl-s1">import</span> <span class="pl-s1">matplotlib</span>

<span class="pl-v">D</span>:\P<span class="pl-s1">ython</span>\A<span class="pl-s1">naconda</span>\l<span class="pl-s1">ib</span>\s<span class="pl-s1">ite</span><span class="pl-c1">-</span><span class="pl-s1">packages</span>\m<span class="pl-s1">atplotlib</span>\_<span class="pl-s1">_init__</span>.<span class="pl-s1">py</span> <span class="pl-c1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>()
   <span class="pl-c1">1046</span> 
   <span class="pl-c1">1047</span> <span class="pl-c"># this is the instance used by the matplotlib classes</span>
<span class="pl-c1">-</span><span class="pl-c1">&gt;</span> <span class="pl-c1">1048</span> <span class="pl-s1">rcParams</span> <span class="pl-c1">=</span> <span class="pl-en">rc_params</span>()
   <span class="pl-c1">1049</span> 
   <span class="pl-c1">1050</span> <span class="pl-k">if</span> <span class="pl-s1">rcParams</span>[<span class="pl-s">'examples.directory'</span>]:

<span class="pl-v">D</span>:\P<span class="pl-s1">ython</span>\A<span class="pl-s1">naconda</span>\l<span class="pl-s1">ib</span>\s<span class="pl-s1">ite</span><span class="pl-c1">-</span><span class="pl-s1">packages</span>\m<span class="pl-s1">atplotlib</span>\_<span class="pl-s1">_init__</span>.<span class="pl-s1">py</span> <span class="pl-c1">in</span> <span class="pl-en">rc_params</span>(<span class="pl-s1">fail_on_error</span>)
    <span class="pl-c1">895</span>     <span class="pl-s1">default</span> <span class="pl-s1">matplotlib</span> <span class="pl-s1">rc</span> <span class="pl-s1">file</span>.
    <span class="pl-c1">896</span>     <span class="pl-s">"""</span>
<span class="pl-s">--&gt; 897     fname = matplotlib_fname()</span>
<span class="pl-s">    898     if not os.path.exists(fname):</span>
<span class="pl-s">    899         # this should never happen, default in mpl-data should always be found</span>
<span class="pl-s"></span>
<span class="pl-s">D:\Python\Anaconda\lib\site-packages\matplotlib\__init__.py in matplotlib_fname()</span>
<span class="pl-s">    746       system-defined copy.</span>
<span class="pl-s">    747     """</span>
<span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">&gt;</span> <span class="pl-c1">748</span>     <span class="pl-s1">fname</span> <span class="pl-c1">=</span> <span class="pl-s1">os</span>.<span class="pl-s1">path</span>.<span class="pl-en">join</span>(<span class="pl-s1">os</span>.<span class="pl-en">getcwd</span>(), <span class="pl-s">'matplotlibrc'</span>)
    <span class="pl-c1">749</span>     <span class="pl-k">if</span> <span class="pl-s1">os</span>.<span class="pl-s1">path</span>.<span class="pl-en">exists</span>(<span class="pl-s1">fname</span>):
    <span class="pl-c1">750</span>         <span class="pl-s1">return</span> <span class="pl-s1">fname</span>

<span class="pl-v">D</span>:\P<span class="pl-s1">ython</span>\A<span class="pl-s1">naconda</span>\l<span class="pl-s1">ib</span>\n<span class="pl-s1">tpath</span>.<span class="pl-s1">pyc</span> <span class="pl-c1">in</span> <span class="pl-en">join</span>(<span class="pl-s1">path</span>, <span class="pl-c1">*</span><span class="pl-s1">paths</span>)
     <span class="pl-c1">82</span>         <span class="pl-k">if</span> <span class="pl-s1">result_path</span> <span class="pl-c1">and</span> <span class="pl-s1">result_path</span>[<span class="pl-c1">-</span><span class="pl-c1">1</span>] <span class="pl-c1">not</span> <span class="pl-c1">in</span> <span class="pl-s">'<span class="pl-cce">\\</span>/'</span>:
     <span class="pl-c1">83</span>             <span class="pl-s1">result_path</span> <span class="pl-c1">=</span> <span class="pl-s1">result_path</span> <span class="pl-c1">+</span> <span class="pl-s">'<span class="pl-cce">\\</span>'</span>
<span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">&gt;</span> <span class="pl-c1">84</span>         <span class="pl-s1">result_path</span> <span class="pl-c1">=</span> <span class="pl-s1">result_path</span> <span class="pl-c1">+</span> <span class="pl-s1">p_path</span>
     <span class="pl-c1">85</span>     <span class="pl-k">return</span> <span class="pl-s1">result_drive</span> <span class="pl-c1">+</span> <span class="pl-s1">result_path</span>
     <span class="pl-c1">86</span> 

<span class="pl-v">UnicodeDecodeError</span>: <span class="pl-s">'ascii'</span> <span class="pl-s1">codec</span> <span class="pl-s1">can</span>'<span class="pl-s1">t</span> <span class="pl-s1">decode</span> <span class="pl-s1">byte</span> <span class="pl-c1">0x9c</span> <span class="pl-c1">in</span> <span class="pl-s1">position</span> <span class="pl-c1">11</span>: <span class="pl-s1">ordinal</span> <span class="pl-c1">not</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-c1">128</span>)</pre></div>
<p dir="auto"><g-emoji class="g-emoji" alias="black_circle" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/26ab.png">⚫</g-emoji> If I change my current directory so that it does not contain any ascii characters the error transforms itself a little:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
&lt;ipython-input-6-1d1446f6fa64&gt; in &lt;module&gt;()
----&gt; 1 from matplotlib import pyplot as plt

D:\Python\Anaconda\lib\site-packages\matplotlib\__init__.py in &lt;module&gt;()
   1046 
   1047 # this is the instance used by the matplotlib classes
-&gt; 1048 rcParams = rc_params()
   1049 
   1050 if rcParams['examples.directory']:

D:\Python\Anaconda\lib\site-packages\matplotlib\__init__.py in rc_params(fail_on_error)
    895     default matplotlib rc file.
    896     &quot;&quot;&quot;
--&gt; 897     fname = matplotlib_fname()
    898     if not os.path.exists(fname):
    899         # this should never happen, default in mpl-data should always be found

D:\Python\Anaconda\lib\site-packages\matplotlib\__init__.py in matplotlib_fname()
    757                 return fname
    758 
--&gt; 759     configdir = _get_configdir()
    760     if configdir is not None:
    761         fname = os.path.join(configdir, 'matplotlibrc')

D:\Python\Anaconda\lib\site-packages\matplotlib\__init__.py in _get_configdir()
    628     5. A writable directory could not be found or created; return None.
    629     &quot;&quot;&quot;
--&gt; 630     return _get_config_or_cache_dir(_get_xdg_config_dir())
    631 
    632 get_configdir = verbose.wrap('CONFIGDIR=%s', _get_configdir, always=False)

D:\Python\Anaconda\lib\site-packages\matplotlib\__init__.py in _get_xdg_config_dir()
    553     path = os.environ.get('XDG_CONFIG_HOME')
    554     if path is None:
--&gt; 555         path = get_home()
    556         if path is not None:
    557             path = os.path.join(path, '.config')

D:\Python\Anaconda\lib\site-packages\matplotlib\__init__.py in wrapper(*args, **kwargs)
    321         assert six.callable(func)
    322         def wrapper(*args, **kwargs):
--&gt; 323             ret = func(*args, **kwargs)
    324 
    325             if (always or not wrapper._spoke):

D:\Python\Anaconda\lib\site-packages\matplotlib\__init__.py in _get_home()
    507     &quot;&quot;&quot;
    508     try:
--&gt; 509         path = os.path.expanduser(&quot;~&quot;)
    510     except ImportError:
    511         # This happens on Google App Engine (pwd module is not present).

D:\Python\Anaconda\lib\ntpath.pyc in expanduser(path)
    271         userhome = join(dirname(userhome), path[1:i])
    272 
--&gt; 273     return userhome + path[i:]
    274 
    275 

UnicodeDecodeError: 'ascii' codec can't decode byte 0x9c in position 13: ordinal not in range(128)"><pre class="notranslate"><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span>
<span class="pl-v">UnicodeDecodeError</span>                        <span class="pl-v">Traceback</span> (<span class="pl-s1">most</span> <span class="pl-s1">recent</span> <span class="pl-s1">call</span> <span class="pl-s1">last</span>)
<span class="pl-c1">&lt;</span><span class="pl-s1">ipython</span><span class="pl-c1">-</span><span class="pl-s1">input</span><span class="pl-c1">-</span><span class="pl-c1">6</span><span class="pl-c1">-</span><span class="pl-c1">1</span><span class="pl-s1">d1446f6fa64</span><span class="pl-c1">&gt;</span> <span class="pl-s1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>()
<span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">&gt;</span> <span class="pl-c1">1</span> <span class="pl-k">from</span> <span class="pl-s1">matplotlib</span> <span class="pl-k">import</span> <span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</span>

<span class="pl-v">D</span>:\P<span class="pl-s1">ython</span>\A<span class="pl-s1">naconda</span>\l<span class="pl-s1">ib</span>\s<span class="pl-s1">ite</span><span class="pl-c1">-</span><span class="pl-s1">packages</span>\m<span class="pl-s1">atplotlib</span>\_<span class="pl-s1">_init__</span>.<span class="pl-s1">py</span> <span class="pl-c1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>()
   <span class="pl-c1">1046</span> 
   <span class="pl-c1">1047</span> <span class="pl-c"># this is the instance used by the matplotlib classes</span>
<span class="pl-c1">-</span><span class="pl-c1">&gt;</span> <span class="pl-c1">1048</span> <span class="pl-s1">rcParams</span> <span class="pl-c1">=</span> <span class="pl-en">rc_params</span>()
   <span class="pl-c1">1049</span> 
   <span class="pl-c1">1050</span> <span class="pl-k">if</span> <span class="pl-s1">rcParams</span>[<span class="pl-s">'examples.directory'</span>]:

<span class="pl-v">D</span>:\P<span class="pl-s1">ython</span>\A<span class="pl-s1">naconda</span>\l<span class="pl-s1">ib</span>\s<span class="pl-s1">ite</span><span class="pl-c1">-</span><span class="pl-s1">packages</span>\m<span class="pl-s1">atplotlib</span>\_<span class="pl-s1">_init__</span>.<span class="pl-s1">py</span> <span class="pl-c1">in</span> <span class="pl-en">rc_params</span>(<span class="pl-s1">fail_on_error</span>)
    <span class="pl-c1">895</span>     <span class="pl-s1">default</span> <span class="pl-s1">matplotlib</span> <span class="pl-s1">rc</span> <span class="pl-s1">file</span>.
    <span class="pl-c1">896</span>     <span class="pl-s">"""</span>
<span class="pl-s">--&gt; 897     fname = matplotlib_fname()</span>
<span class="pl-s">    898     if not os.path.exists(fname):</span>
<span class="pl-s">    899         # this should never happen, default in mpl-data should always be found</span>
<span class="pl-s"></span>
<span class="pl-s">D:\Python\Anaconda\lib\site-packages\matplotlib\__init__.py in matplotlib_fname()</span>
<span class="pl-s">    757                 return fname</span>
<span class="pl-s">    758 </span>
<span class="pl-s">--&gt; 759     configdir = _get_configdir()</span>
<span class="pl-s">    760     if configdir is not None:</span>
<span class="pl-s">    761         fname = os.path.join(configdir, 'matplotlibrc')</span>
<span class="pl-s"></span>
<span class="pl-s">D:\Python\Anaconda\lib\site-packages\matplotlib\__init__.py in _get_configdir()</span>
<span class="pl-s">    628     5. A writable directory could not be found or created; return None.</span>
<span class="pl-s">    629     """</span>
<span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">&gt;</span> <span class="pl-c1">630</span>     <span class="pl-s1">return</span> <span class="pl-en">_get_config_or_cache_dir</span>(<span class="pl-en">_get_xdg_config_dir</span>())
    <span class="pl-c1">631</span> 
    <span class="pl-c1">632</span> <span class="pl-s1">get_configdir</span> <span class="pl-c1">=</span> <span class="pl-s1">verbose</span>.<span class="pl-en">wrap</span>(<span class="pl-s">'CONFIGDIR=%s'</span>, <span class="pl-s1">_get_configdir</span>, <span class="pl-s1">always</span><span class="pl-c1">=</span><span class="pl-c1">False</span>)

<span class="pl-v">D</span>:\P<span class="pl-s1">ython</span>\A<span class="pl-s1">naconda</span>\l<span class="pl-s1">ib</span>\s<span class="pl-s1">ite</span><span class="pl-c1">-</span><span class="pl-s1">packages</span>\m<span class="pl-s1">atplotlib</span>\_<span class="pl-s1">_init__</span>.<span class="pl-s1">py</span> <span class="pl-c1">in</span> <span class="pl-en">_get_xdg_config_dir</span>()
    <span class="pl-c1">553</span>     <span class="pl-s1">path</span> <span class="pl-c1">=</span> <span class="pl-s1">os</span>.<span class="pl-s1">environ</span>.<span class="pl-en">get</span>(<span class="pl-s">'XDG_CONFIG_HOME'</span>)
    <span class="pl-c1">554</span>     <span class="pl-k">if</span> <span class="pl-s1">path</span> <span class="pl-c1">is</span> <span class="pl-c1">None</span>:
<span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">&gt;</span> <span class="pl-c1">555</span>         <span class="pl-s1">path</span> <span class="pl-c1">=</span> <span class="pl-en">get_home</span>()
    <span class="pl-c1">556</span>         <span class="pl-k">if</span> <span class="pl-s1">path</span> <span class="pl-c1">is</span> <span class="pl-c1">not</span> <span class="pl-c1">None</span>:
    <span class="pl-c1">557</span>             <span class="pl-s1">path</span> <span class="pl-c1">=</span> <span class="pl-s1">os</span>.<span class="pl-s1">path</span>.<span class="pl-en">join</span>(<span class="pl-s1">path</span>, <span class="pl-s">'.config'</span>)

<span class="pl-v">D</span>:\P<span class="pl-s1">ython</span>\A<span class="pl-s1">naconda</span>\l<span class="pl-s1">ib</span>\s<span class="pl-s1">ite</span><span class="pl-c1">-</span><span class="pl-s1">packages</span>\m<span class="pl-s1">atplotlib</span>\_<span class="pl-s1">_init__</span>.<span class="pl-s1">py</span> <span class="pl-c1">in</span> <span class="pl-en">wrapper</span>(<span class="pl-c1">*</span><span class="pl-s1">args</span>, <span class="pl-c1">**</span><span class="pl-s1">kwargs</span>)
    <span class="pl-c1">321</span>         <span class="pl-k">assert</span> <span class="pl-s1">six</span>.<span class="pl-en">callable</span>(<span class="pl-s1">func</span>)
    <span class="pl-c1">322</span>         <span class="pl-k">def</span> <span class="pl-s1">wrapper</span>(<span class="pl-c1">*</span><span class="pl-s1">args</span>, <span class="pl-c1">**</span><span class="pl-s1">kwargs</span>):
<span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">&gt;</span> <span class="pl-c1">323</span>             <span class="pl-s1">ret</span> <span class="pl-c1">=</span> <span class="pl-en">func</span>(<span class="pl-c1">*</span><span class="pl-s1">args</span>, <span class="pl-c1">**</span><span class="pl-s1">kwargs</span>)
    <span class="pl-c1">324</span> 
    <span class="pl-c1">325</span>             <span class="pl-k">if</span> (<span class="pl-s1">always</span> <span class="pl-c1">or</span> <span class="pl-c1">not</span> <span class="pl-s1">wrapper</span>.<span class="pl-s1">_spoke</span>):

<span class="pl-v">D</span>:\P<span class="pl-s1">ython</span>\A<span class="pl-s1">naconda</span>\l<span class="pl-s1">ib</span>\s<span class="pl-s1">ite</span><span class="pl-c1">-</span><span class="pl-s1">packages</span>\m<span class="pl-s1">atplotlib</span>\_<span class="pl-s1">_init__</span>.<span class="pl-s1">py</span> <span class="pl-c1">in</span> <span class="pl-en">_get_home</span>()
    <span class="pl-c1">507</span>     """
    508     try:
--&gt; 509         path = os.path.expanduser("~")
    510     except ImportError:
    511         # This happens on Google App Engine (pwd module is not present).

D:\Python\Anaconda\lib<span class="pl-cce">\n</span><span class="pl-s1">tpath</span>.<span class="pl-s1">pyc</span> <span class="pl-c1">in</span> <span class="pl-en">expanduser</span>(<span class="pl-s1">path</span>)
    <span class="pl-c1">271</span>         <span class="pl-s1">userhome</span> <span class="pl-c1">=</span> <span class="pl-s1">join</span>(<span class="pl-en">dirname</span>(<span class="pl-s1">userhome</span>), <span class="pl-s1">path</span>[<span class="pl-c1">1</span>:<span class="pl-s1">i</span>])
    <span class="pl-c1">272</span> 
<span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">&gt;</span> <span class="pl-c1">273</span>     <span class="pl-s1">return</span> <span class="pl-s1">userhome</span> <span class="pl-c1">+</span> <span class="pl-s1">path</span>[<span class="pl-s1">i</span>:]
    <span class="pl-c1">274</span> 
    <span class="pl-c1">275</span> 

<span class="pl-v">UnicodeDecodeError</span>: <span class="pl-s">'ascii'</span> <span class="pl-s1">codec</span> <span class="pl-s1">can</span>'<span class="pl-s1">t</span> <span class="pl-s1">decode</span> <span class="pl-s1">byte</span> <span class="pl-c1">0x9c</span> <span class="pl-c1">in</span> <span class="pl-s1">position</span> <span class="pl-c1">13</span>: <span class="pl-s1">ordinal</span> <span class="pl-c1">not</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-c1">128</span>)</pre></div>