title: PythonTeX (Python + Latex) error of savefig() in PythonteX Gallery file
body: <p>I am learning Python, Latex, and found out about existence of wonderfull package of PythonTeX, which allows to add Python code into TeX. There is a gallery file at this site: </p>
  
  <p><a href="https://ctan.org/pkg/pythontex" rel="nofollow noreferrer">https://ctan.org/pkg/pythontex</a></p>
  
  <p>If you download all package files, there is "gallery" tex source file, which I am compiling for testing. There is also simple plot generation code (short exerpt):</p>
  
  <pre><code>You may want to use matplotlib's PGF backend when creating plots.
  
  \begin{pylabblock}
  rc('text', usetex=True)
  rc('font', family='serif')
  rc('font', size=10.0)
  rc('legend', fontsize=10.0)
  rc('font', weight='normal')
  x = linspace(0, 10)
  figure(figsize=(4, 2.5))
  plot(x, sin(x), label='$\sin(x)$')
  xlabel(r'$x\mathrm{-axis}$')
  ylabel(r'$y\mathrm{-axis}$')
  legend(loc='lower right')
  savefig('myplot.pdf', bbox_inches='tight')
  \end{pylabblock}
  </code></pre>
  
  <p>while executing this part, specifically last line:</p>
  
  <pre><code>savefig('myplot.pdf', bbox_inches='tight')
  </code></pre>
  
  <p>I get this nasty set of errors (sorry for the size):</p>
  
  <pre><code>----  Messages for pylab:default:5  ----
    Traceback (most recent call last):
  * PythonTeX stderr - error on line 173:
      File "pythontex-files-pythontex_gallery_withiffileexistscondonplot\pylab_default_5.py", line 62, in &lt;module&gt;
        savefig('myplot.pdf', bbox_inches='tight')
      File "C:\python\lib\site-packages\matplotlib\pyplot.py", line 722, in savefig
        res = fig.savefig(*args, **kwargs)
      File "C:\python\lib\site-packages\matplotlib\figure.py", line 2180, in savefig
        self.canvas.print_figure(fname, **kwargs)
      File "C:\python\lib\site-packages\matplotlib\backend_bases.py", line 2056, in print_figure
        **kwargs)
      File "C:\python\lib\site-packages\matplotlib\backends\backend_pdf.py", line 2503, in print_pdf
        self.figure.draw(renderer)
      File "C:\python\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
        return draw(artist, renderer, *args, **kwargs)
      File "C:\python\lib\site-packages\matplotlib\figure.py", line 1709, in draw
        renderer, self, artists, self.suppressComposite)
      File "C:\python\lib\site-packages\matplotlib\image.py", line 135, in _draw_list_compositing_images
        a.draw(renderer)
      File "C:\python\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
        return draw(artist, renderer, *args, **kwargs)
      File "C:\python\lib\site-packages\matplotlib\axes\_base.py", line 2647, in draw
        mimage._draw_list_compositing_images(renderer, self, artists)
      File "C:\python\lib\site-packages\matplotlib\image.py", line 135, in _draw_list_compositing_images
        a.draw(renderer)
      File "C:\python\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
        return draw(artist, renderer, *args, **kwargs)
      File "C:\python\lib\site-packages\matplotlib\axis.py", line 1205, in draw
        renderer)
      File "C:\python\lib\site-packages\matplotlib\axis.py", line 1150, in _get_tick_bboxes
        for tick in ticks if tick.label1.get_visible()],
      File "C:\python\lib\site-packages\matplotlib\axis.py", line 1150, in &lt;listcomp&gt;
        for tick in ticks if tick.label1.get_visible()],
      File "C:\python\lib\site-packages\matplotlib\text.py", line 890, in get_window_extent
        bbox, info, descent = self._get_layout(self._renderer)
      File "C:\python\lib\site-packages\matplotlib\text.py", line 291, in _get_layout
        ismath="TeX" if self.get_usetex() else False)
      File "C:\python\lib\site-packages\matplotlib\backends\_backend_pdf_ps.py", line 47, in get_text_width_height_descent
        s, fontsize, renderer=self)
      File "C:\python\lib\site-packages\matplotlib\texmanager.py", line 450, in get_text_width_height_descent
        page, = dvi
      File "C:\python\lib\site-packages\matplotlib\dviread.py", line 243, in __iter__
        while self._read():
      File "C:\python\lib\site-packages\matplotlib\dviread.py", line 301, in _read
        self._dtable[byte](self, byte)
      File "C:\python\lib\site-packages\matplotlib\dviread.py", line 161, in wrapper
        return method(self, *[f(self, byte-min) for f in get_args])
      File "C:\python\lib\site-packages\matplotlib\dviread.py", line 441, in _fnt_def
        self._fnt_def_real(k, c, s, d, a, l)
      File "C:\python\lib\site-packages\matplotlib\dviread.py", line 446, in _fnt_def_real
        tfm = _tfmfile(fontname)
      File "C:\python\lib\site-packages\matplotlib\dviread.py", line 1063, in _fontfile
        filename = find_tex_file(texname + suffix)
      File "C:\python\lib\site-packages\matplotlib\dviread.py", line 1052, in find_tex_file
        result = cbook._check_and_log_subprocess(cmd, _log, **kwargs)
      File "C:\python\lib\site-packages\matplotlib\cbook\__init__.py", line 2127, in _check_and_log_subprocess
        command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
      File "C:\python\lib\subprocess.py", line 472, in run
        with Popen(*popenargs, **kwargs) as process:
      File "C:\python\lib\subprocess.py", line 775, in __init__
        restore_signals, start_new_session)
      File "C:\python\lib\subprocess.py", line 1178, in _execute_child
        startupinfo)
    FileNotFoundError: [WinError 2] Systém nemůže nalézt uvedený soubor
  
  --------------------------------------------------
  PythonTeX:  pythontex_gallery_withIfFileExistsCondOnPlot - 1 error(s), 0 warning(s)
  
  </code></pre>
  
  <p>Interesting is, that this problem is machine-specific. I have two other (lot older!) machines, which can process this code OK. One is on windows 10, other on windows 7. The issue is on another device also with windows 10.</p>
  
  <p>I have read somewhere that this error is generated by Python being in directories with non-ASCII symbols in its names. Seeing that, I have put python in C:\ root. Also, I have setup PATH variable accordingly (already in installer).</p>
  
  <p>How can this be fixed?</p>
  
  <p>Please, notify me if full (working) MWE is necessary instead of link to CTAN site. If so, I try to minimize to official file to as small amount of code as possible and edit the question.</p>
  
  <p>Thank you for any help,</p>
  
  <p>Regards, Tomas</p>
  
  <p>EDIT: Just while I got comment, found out, that a folder .matplotlib is generated in my windows 10 user directory (containing non-ACII letters in its path). Comparing .matplotlib content among machines I found out, that if the savefig() function is called successfully, there are a lot of files, otherwise, only two.</p>
  
  <p>Now I am sure, that the reason for this is that while using PythonTeX and matplotlib, a .matplotlib directory is created in user home directory, which (in my case) is a faulty path.</p>
  
  <p>Is there a way, how to change the location where is this directory created?</p>