materialsproject / pymatgen

Python Materials Genomics (pymatgen) is a robust materials analysis code that defines classes for structures and molecules with support for many electronic structure codes. It powers the Materials Project.
https://pymatgen.org
Other
1.53k stars 867 forks source link

Having trouble with matplotlib and band structures #103

Closed computron closed 10 years ago

computron commented 10 years ago

I am trying to plot band structures using the following code:

from pymatgen import MPRester
from pymatgen.electronic_structure.plotter import BSPlotter

if __name__ == "__main__":
    MAPI_KEY = None  # You must change this to your Materials API key! (or set MAPI_KEY env variable)
    MP_ID = "mp-19017"  # You must change this to the mp-id of your compound of interest

    mpr = MPRester(MAPI_KEY)  # object for connecting to MP Rest interface

    my_bs = mpr.get_bandstructure_by_material_id(MP_ID)
    BSPlotter(my_bs).show()

Something seems to be wrong with my matplotlib configuration and the code trips over the tight_layout, giving me the following error:

tight_layout : falling back to Agg renderer
Traceback (most recent call last):
  File "/Users/ajain/Documents/code_testing/aj_scratch/aj_play/scratch.py", line 12, in <module>
    BSPlotter(my_bs).show()
  File "/Users/ajain/Documents/code_matgen/pymatgen_repo/pymatgen/electronic_structure/plotter.py", line 508, in show
    plt = self.get_plot(zero_to_efermi, ylim, smooth)
  File "/Users/ajain/Documents/code_matgen/pymatgen_repo/pymatgen/electronic_structure/plotter.py", line 492, in get_plot
    plt.tight_layout()
  File "/Library/Python/2.7/site-packages/matplotlib/pyplot.py", line 1055, in tight_layout
    fig.tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad)
  File "/Library/Python/2.7/site-packages/matplotlib/figure.py", line 1414, in tight_layout
    pad=pad, h_pad=h_pad, w_pad=w_pad)
  File "/Library/Python/2.7/site-packages/matplotlib/tight_layout.py", line 117, in auto_adjust_subplotpars
    tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots])
  File "/Library/Python/2.7/site-packages/matplotlib/axes.py", line 8362, in get_tightbbox
    bb_xaxis = self.xaxis.get_tightbbox(renderer)
  File "/Library/Python/2.7/site-packages/matplotlib/axis.py", line 1008, in get_tightbbox
    ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw, renderer)
  File "/Library/Python/2.7/site-packages/matplotlib/axis.py", line 993, in _get_tick_bboxes
    extent = tick.label1.get_window_extent(renderer)
  File "/Library/Python/2.7/site-packages/matplotlib/text.py", line 747, in get_window_extent
    bbox, info = self._get_layout(self._renderer)
  File "/Library/Python/2.7/site-packages/matplotlib/text.py", line 300, in _get_layout
    ismath=False)
  File "/Library/Python/2.7/site-packages/matplotlib/backends/backend_agg.py", line 188, in get_text_width_height_descent
    renderer=self)
  File "/Library/Python/2.7/site-packages/matplotlib/texmanager.py", line 608, in get_text_width_height_descent
    page = iter(dvi).next()
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 65, in __iter__
    have_page = self._read()
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 121, in _read
    self._dispatch(byte)
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 209, in _dispatch
    self._fnt_def(k, c, s, d, a, l, n)
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 362, in _fnt_def
    tfm = _tfmfile(n[-l:])
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 872, in _tfmfile
    return _fontfile(texname, Tfm, '.tfm', _tfmcache)
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 862, in _fontfile
    filename = find_tex_file(texname + suffix)
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 843, in find_tex_file
    stderr=subprocess.PIPE)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1228, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

I can turn off tight layout (manually commenting it out), and the main portion of the band structure plot renders fine, but the x-labels and y-labels do not render and I get the following error:

Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/matplotlib/figure.py", line 898, in draw
    func(*args)
  File "/Library/Python/2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/matplotlib/axes.py", line 1997, in draw
    a.draw(renderer)
  File "/Library/Python/2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/matplotlib/axis.py", line 1042, in draw
    ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw, renderer)
  File "/Library/Python/2.7/site-packages/matplotlib/axis.py", line 993, in _get_tick_bboxes
    extent = tick.label1.get_window_extent(renderer)
  File "/Library/Python/2.7/site-packages/matplotlib/text.py", line 747, in get_window_extent
    bbox, info = self._get_layout(self._renderer)
  File "/Library/Python/2.7/site-packages/matplotlib/text.py", line 309, in _get_layout
    ismath=ismath)
  File "/Library/Python/2.7/site-packages/matplotlib/backends/backend_macosx.py", line 153, in get_text_width_height_descent
    renderer=self)
  File "/Library/Python/2.7/site-packages/matplotlib/texmanager.py", line 608, in get_text_width_height_descent
    page = iter(dvi).next()
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 65, in __iter__
    have_page = self._read()
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 121, in _read
    self._dispatch(byte)
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 209, in _dispatch
    self._fnt_def(k, c, s, d, a, l, n)
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 362, in _fnt_def
    tfm = _tfmfile(n[-l:])
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 872, in _tfmfile
    return _fontfile(texname, Tfm, '.tfm', _tfmcache)
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 862, in _fontfile
    filename = find_tex_file(texname + suffix)
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 843, in find_tex_file
    stderr=subprocess.PIPE)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1228, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/matplotlib/figure.py", line 898, in draw
    func(*args)
  File "/Library/Python/2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/matplotlib/axes.py", line 1997, in draw
    a.draw(renderer)
  File "/Library/Python/2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/matplotlib/axis.py", line 1042, in draw
    ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw, renderer)
  File "/Library/Python/2.7/site-packages/matplotlib/axis.py", line 993, in _get_tick_bboxes
    extent = tick.label1.get_window_extent(renderer)
  File "/Library/Python/2.7/site-packages/matplotlib/text.py", line 747, in get_window_extent
    bbox, info = self._get_layout(self._renderer)
  File "/Library/Python/2.7/site-packages/matplotlib/text.py", line 309, in _get_layout
    ismath=ismath)
  File "/Library/Python/2.7/site-packages/matplotlib/backends/backend_macosx.py", line 153, in get_text_width_height_descent
    renderer=self)
  File "/Library/Python/2.7/site-packages/matplotlib/texmanager.py", line 608, in get_text_width_height_descent
    page = iter(dvi).next()
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 65, in __iter__
    have_page = self._read()
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 121, in _read
    self._dispatch(byte)
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 209, in _dispatch
    self._fnt_def(k, c, s, d, a, l, n)
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 362, in _fnt_def
    tfm = _tfmfile(n[-l:])
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 872, in _tfmfile
    return _fontfile(texname, Tfm, '.tfm', _tfmcache)
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 862, in _fontfile
    filename = find_tex_file(texname + suffix)
  File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 843, in find_tex_file
    stderr=subprocess.PIPE)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1228, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
shyuep commented 10 years ago

Try reinstalling matplotlib?

Shyue Ping

On July 5, 2014 at 6:30:09 PM, Anubhav Jain (notifications@github.com) wrote:

I am trying to plot band structures using the following code:

from pymatgen import MPRester from pymatgen.electronic_structure.plotter import BSPlotter

if name == "main": MAPI_KEY = None # You must change this to your Materials API key! (or set MAPI_KEY env variable) MP_ID = "mp-19017" # You must change this to the mp-id of your compound of interest

mpr = MPRester(MAPI_KEY)  # object for connecting to MP Rest interface

my_bs = mpr.get_bandstructure_by_material_id(MP_ID)
BSPlotter(my_bs).show()

Something seems to be wrong with my matplotlib configuration and the code trips over the tight_layout, giving me the following error:

tight_layout : falling back to Agg renderer Traceback (most recent call last): File "/Users/ajain/Documents/code_testing/aj_scratch/aj_play/scratch.py", line 12, in BSPlotter(my_bs).show() File "/Users/ajain/Documents/code_matgen/pymatgen_repo/pymatgen/electronic_structure/plotter.py", line 508, in show plt = self.get_plot(zero_to_efermi, ylim, smooth) File "/Users/ajain/Documents/code_matgen/pymatgen_repo/pymatgen/electronic_structure/plotter.py", line 492, in get_plot plt.tight_layout() File "/Library/Python/2.7/site-packages/matplotlib/pyplot.py", line 1055, in tight_layout fig.tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad) File "/Library/Python/2.7/site-packages/matplotlib/figure.py", line 1414, in tight_layout pad=pad, h_pad=h_pad, w_pad=w_pad) File "/Library/Python/2.7/site-packages/matplotlib/tight_layout.py", line 117, in auto_adjust_subplotpars tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots]) File "/Library/Python/2.7/site-packages/matplotlib/axes.py", line 8362, in get_tightbbox bb_xaxis = self.xaxis.get_tightbbox(renderer) File "/Library/Python/2.7/site-packages/matplotlib/axis.py", line 1008, in get_tightbbox ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw, renderer) File "/Library/Python/2.7/site-packages/matplotlib/axis.py", line 993, in _get_tick_bboxes extent = tick.label1.get_window_extent(renderer) File "/Library/Python/2.7/site-packages/matplotlib/text.py", line 747, in get_window_extent bbox, info = self._get_layout(self._renderer) File "/Library/Python/2.7/site-packages/matplotlib/text.py", line 300, in _get_layout ismath=False) File "/Library/Python/2.7/site-packages/matplotlib/backends/backend_agg.py", line 188, in get_text_width_height_descent renderer=self) File "/Library/Python/2.7/site-packages/matplotlib/texmanager.py", line 608, in get_text_width_height_descent page = iter(dvi).next() File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 65, in iter have_page = self._read() File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 121, in _read self._dispatch(byte) File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 209, in _dispatch self._fnt_def(k, c, s, d, a, l, n) File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 362, in _fnt_def tfm = _tfmfile(n[-l:]) File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 872, in _tfmfile return _fontfile(texname, Tfm, '.tfm', _tfmcache) File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 862, in _fontfile filename = find_tex_file(texname + suffix) File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 843, in find_tex_file stderr=subprocess.PIPE) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 679, in init errread, errwrite) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1228, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory

I can turn off tight layout (manually commenting it out), and the main portion of the band structure plot renders fine, but the x-labels and y-labels do not render and I get the following error:

Traceback (most recent call last): File "/Library/Python/2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper draw(artist, renderer, _args, _kwargs) File "/Library/Python/2.7/site-packages/matplotlib/figure.py", line 898, in draw func(_args) File "/Library/Python/2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper draw(artist, renderer, args, _kwargs) File "/Library/Python/2.7/site-packages/matplotlib/axes.py", line 1997, in draw a.draw(renderer) File "/Library/Python/2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper draw(artist, renderer, _args, _kwargs) File "/Library/Python/2.7/site-packages/matplotlib/axis.py", line 1042, in draw ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw, renderer) File "/Library/Python/2.7/site-packages/matplotlib/axis.py", line 993, in _get_tick_bboxes extent = tick.label1.get_window_extent(renderer) File "/Library/Python/2.7/site-packages/matplotlib/text.py", line 747, in get_window_extent bbox, info = self._get_layout(self._renderer) File "/Library/Python/2.7/site-packages/matplotlib/text.py", line 309, in _get_layout ismath=ismath) File "/Library/Python/2.7/site-packages/matplotlib/backends/backend_macosx.py", line 153, in get_text_width_height_descent renderer=self) File "/Library/Python/2.7/site-packages/matplotlib/texmanager.py", line 608, in get_text_width_height_descent page = iter(dvi).next() File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 65, in iter have_page = self._read() File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 121, in _read self._dispatch(byte) File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 209, in _dispatch self._fnt_def(k, c, s, d, a, l, n) File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 362, in _fnt_def tfm = _tfmfile(n[-l:]) File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 872, in _tfmfile return _fontfile(texname, Tfm, '.tfm', _tfmcache) File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 862, in _fontfile filename = find_tex_file(texname + suffix) File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 843, in find_tex_file stderr=subprocess.PIPE) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 679, in init errread, errwrite) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1228, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory Traceback (most recent call last): File "/Library/Python/2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper draw(artist, renderer, _args, _kwargs) File "/Library/Python/2.7/site-packages/matplotlib/figure.py", line 898, in draw func(_args) File "/Library/Python/2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper draw(artist, renderer, args, _kwargs) File "/Library/Python/2.7/site-packages/matplotlib/axes.py", line 1997, in draw a.draw(renderer) File "/Library/Python/2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper draw(artist, renderer, _args, _kwargs) File "/Library/Python/2.7/site-packages/matplotlib/axis.py", line 1042, in draw ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw, renderer) File "/Library/Python/2.7/site-packages/matplotlib/axis.py", line 993, in _get_tick_bboxes extent = tick.label1.get_window_extent(renderer) File "/Library/Python/2.7/site-packages/matplotlib/text.py", line 747, in get_window_extent bbox, info = self._get_layout(self._renderer) File "/Library/Python/2.7/site-packages/matplotlib/text.py", line 309, in _get_layout ismath=ismath) File "/Library/Python/2.7/site-packages/matplotlib/backends/backend_macosx.py", line 153, in get_text_width_height_descent renderer=self) File "/Library/Python/2.7/site-packages/matplotlib/texmanager.py", line 608, in get_text_width_height_descent page = iter(dvi).next() File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 65, in iter have_page = self._read() File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 121, in _read self._dispatch(byte) File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 209, in _dispatch self._fnt_def(k, c, s, d, a, l, n) File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 362, in _fnt_def tfm = _tfmfile(n[-l:]) File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 872, in _tfmfile return _fontfile(texname, Tfm, '.tfm', _tfmcache) File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 862, in _fontfile filename = find_tex_file(texname + suffix) File "/Library/Python/2.7/site-packages/matplotlib/dviread.py", line 843, in find_tex_file stderr=subprocess.PIPE) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 679, in init errread, errwrite) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1228, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory

— Reply to this email directly or view it on GitHub.

computron commented 10 years ago

Ok I didn't get this working (e.g. even after reinstalling matplotlib) but I'm pretty sure this is something in my local matplotlib installation rather than pymatgen. Closing the issue

computron commented 10 years ago

Btw I finally solved this - the issue has to do with matplotlib not finding LaTeX. The package installer for Mac LaTeX (MacTeX) installs the latex executables in /usr/texbin, which gets added to the PATH. However PyCharm doesn't see this as part of its path, so it can't find LaTeX and can't plot any chart involving LaTeX. If you set your PyCharm run environment to include /usr/texbin all is well