0

I'm trying to save pyplot figures using matplotlib pyplot.savefig in a Jupyter notebook. This works for png files but as of today, this no longer works for pdf and svg. Before today I regularly saved figures as pdf and svg. I'm running python3.6 in anaconda3 and I just updated today with conda update conda and conda update --all. Not sure whether or how an update might have broken the savefig function.

Executed command:

plt.savefig(filename, bbox_inches='tight', format='svg')

Full error:

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-149-9adcbb84294a> in <module>
     28 
     29 filename =  'fig.svg'
---> 30 plt.savefig(filename, format='svg') #
     31 

~/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py in savefig(*args, **kwargs)
~/anaconda3/lib/python3.6/site-packages/matplotlib/figure.py in savefig(self, fname, transparent, **kwargs)
~/anaconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, **kwargs)
~/anaconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py in _get_output_canvas(self, fmt)
~/anaconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py in get_registered_canvas_class(format)
~/anaconda3/lib/python3.6/importlib/__init__.py in import_module(name, package)
    124                 break
    125             level += 1
--> 126     return _bootstrap._gcd_import(name[level:], package, level)
    127 
    128 

~/anaconda3/lib/python3.6/importlib/_bootstrap.py in _gcd_import(name, package, level)
~/anaconda3/lib/python3.6/importlib/_bootstrap.py in _find_and_load(name, import_)
~/anaconda3/lib/python3.6/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

ModuleNotFoundError: No module named 'matplotlib.backends.backend_svg'

Modules imported:

import numpy as np 
import scipy.integrate 
import scipy.signal 
import scipy.stats as st 
import matplotlib.pyplot as plt 
import seaborn as sns 

# Set matplotlib rc params. 
rc = {'lines.linewidth' : 2, 'axes.labelsize' : 20, 'legend.fontsize': 12, 'axes.titlesize' : 25, 'xtick.labelsize': 15, 'ytick.labelsize': 15} sns.set(rc=rc) sns.set_style("whitegrid") 

%matplotlib inline
kuintzlr
  • 1
  • 1
  • Please share the contents of the environment. Are you using the base environment for this? – AMC Apr 15 '20 at 21:41
  • I am not 100% sure what you're asking, but here are the packages I imported in this notebook (sorry, new to Stackoverflow and formatting is terrible): `import numpy as np import scipy.integrate import scipy.signal import scipy.stats as st import matplotlib.pyplot as plt import seaborn as sns # Set matplotlib rc params. rc = {'lines.linewidth' : 2, 'axes.labelsize' : 20, 'legend.fontsize': 12, 'axes.titlesize' : 25, 'xtick.labelsize': 15, 'ytick.labelsize': 15} sns.set(rc=rc) sns.set_style("whitegrid") %matplotlib inline` – kuintzlr Apr 15 '20 at 22:51
  • _I am not 100% sure what you're asking_ Can you share the output of `conda list -n env_name` ? _sorry, new to Stackoverflow and formatting is terrible_ It should go in your post, it will be much easier to read. – AMC Apr 15 '20 at 23:35
  • ```conda env list``` returns: # conda environments: # base * /Users/[my name]/anaconda3 – kuintzlr Apr 16 '20 at 01:30
  • I was hoping for the contents of the environment itself, but this will actually be quite useful. You're using the base environment, don't! It defeats the point of using virtual environments, and quickly turns into a complete nightmare. Reset the environment, or even better, remove all traces of Anaconda and Conda, and give it another try, odds are it will work. – AMC Apr 16 '20 at 01:34
  • Can you recommend a link that describes how to do this safely? Thanks! – kuintzlr Apr 16 '20 at 01:43
  • That depends on what you meany by _safely_. Take a look at [this](https://stackoverflow.com/questions/41914139/how-to-reset-anaconda-root-environment) for information on resetting the base environment. You can find instructions to uninstall Conda/Anaconda [here](https://docs.anaconda.com/anaconda/install/uninstall/). – AMC Apr 16 '20 at 01:51

1 Answers1

0

Solution: According to AMC's suggestion, I clean uninstalled anaconda and reinstalled, then created a new environment. I can now export figures properly in pdf. Still not sure what caused my problem after updating conda before.

kuintzlr
  • 1
  • 1