Skip to content

Instantly share code, notes, and snippets.

@computron
Last active July 13, 2022 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save computron/7d1840ae04d67abaeda0 to your computer and use it in GitHub Desktop.
Save computron/7d1840ae04d67abaeda0 to your computer and use it in GitHub Desktop.
plot_bandstructure
"""
This example prints out a band structure object of a Materials Project entry.
To run this example, you should:
* have pymatgen (www.pymatgen.org) installed
* obtain a Materials Project API key (https://www.materialsproject.org/open)
* paste that API key in the MAPI_KEY variable below, e.g. MAPI_KEY = "foobar1234"
as well as:
* update MP_ID with the Materials Project id of the compound
For citation, see https://www.materialsproject.org/citing
"""
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)
print 'Band gap info: {}'.format(my_bs.get_band_gap())
BSPlotter(my_bs).show()
@kalaiarasus
Copy link

when i execute the above i get an error like this, help me to resolve the problem
sk@sk-System-Product-Name:examples$ python 123.py
False
{'energy': 1.7978000000000005, 'transition': '(0.591,0.409,0.000)-\Gamma', 'direct': False}
18.0201
2.6904
{'energy': 6.1023, 'band_index': defaultdict(<class 'list'>, {<Spin.up: 1>: [15]}), 'projections': {}, 'kpoint_index': [123], 'kpoint': <pymatgen.electronic_structure.bandstructure.Kpoint object at 0x7f611a70b550>}
GLib-GIO-Message: Using the 'memory' GSettings backend. Your settings will not be saved or shared with other applications.
/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/axis.py:1015: UserWarning: Unable to find pixel distance along axis for interval padding of ticks; assuming no interval padding needed.
warnings.warn("Unable to find pixel distance along axis "
/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/axis.py:1025: UserWarning: Unable to find pixel distance along axis for interval padding of ticks; assuming no interval padding needed.
warnings.warn("Unable to find pixel distance along axis "
Traceback (most recent call last):
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/backends/backend_qt5agg.py", line 176, in __draw_idle_agg
FigureCanvasAgg.draw(self)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py", line 474, in draw
self.figure.draw(self.renderer)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/figure.py", line 1159, in draw
func(*args)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 2324, in draw
a.draw(renderer)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/axis.py", line 1120, in draw
self.label.draw(renderer)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/text.py", line 757, in draw
raise ValueError("posx and posy should be finite values")
ValueError: posx and posy should be finite values
sk@sk-System-Product-Name:examples$ python plot_bs1.py
Band gap info: {'energy': 1.7978000000000005, 'direct': False, 'transition': '(0.591,0.409,0.000)-\Gamma'}
GLib-GIO-Message: Using the 'memory' GSettings backend. Your settings will not be saved or shared with other applications.
/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/axis.py:1015: UserWarning: Unable to find pixel distance along axis for interval padding of ticks; assuming no interval padding needed.
warnings.warn("Unable to find pixel distance along axis "
/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/axis.py:1025: UserWarning: Unable to find pixel distance along axis for interval padding of ticks; assuming no interval padding needed.
warnings.warn("Unable to find pixel distance along axis "
Traceback (most recent call last):
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/backends/backend_qt5agg.py", line 176, in __draw_idle_agg
FigureCanvasAgg.draw(self)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py", line 474, in draw
self.figure.draw(self.renderer)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/figure.py", line 1159, in draw
func(*args)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 2324, in draw
a.draw(renderer)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/axis.py", line 1120, in draw
self.label.draw(renderer)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/text.py", line 757, in draw
raise ValueError("posx and posy should be finite values")
ValueError: posx and posy should be finite values
sk@sk-System-Product-Name:examples$ python -i plot_bs1.py
Band gap info: {'transition': '(0.500,0.227,0.000)-\Gamma', 'energy': 3.9223999999999997, 'direct': False}
GLib-GIO-Message: Using the 'memory' GSettings backend. Your settings will not be saved or shared with other applications.
/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/axis.py:1015: UserWarning: Unable to find pixel distance along axis for interval padding of ticks; assuming no interval padding needed.
warnings.warn("Unable to find pixel distance along axis "
/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/axis.py:1025: UserWarning: Unable to find pixel distance along axis for interval padding of ticks; assuming no interval padding needed.
warnings.warn("Unable to find pixel distance along axis "
Traceback (most recent call last):
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/backends/backend_qt5agg.py", line 176, in __draw_idle_agg
FigureCanvasAgg.draw(self)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py", line 474, in draw
self.figure.draw(self.renderer)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/figure.py", line 1159, in draw
func(*args)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 2324, in draw
a.draw(renderer)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/axis.py", line 1120, in draw
self.label.draw(renderer)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/sk/anaconda3/lib/python3.5/site-packages/matplotlib/text.py", line 757, in draw
raise ValueError("posx and posy should be finite values")
ValueError: posx and posy should be finite values

@tafaltens
Copy link

tafaltens commented Jul 13, 2022

I just ran the above code using Jupyter notebooks in nanoHUB (a free resource that enables you to easily connect to The Materials Project, as pymatgen is already installed https://nanohub.org/tools/jupyter).

-- by the way, there are a lot of additional Python libraries already installed in nanoHUB, including matplotlib, so that may also help to make the code run flawlessly. The above jupyter notebook link uses Anaconda 6.0.

I only had to make one edit to make it work- I added parentheses around the argument of the print statement, as follows:

print ('Band gap info: {}'.format(my_bs.get_band_gap()))

Here is a screenshot of the results returned.
Screen Shot 2022-07-13 at 7 10 21 PM

I got here by following the instructions in the Materials Project YouTube video tutorial: https://www.youtube.com/watch?v=zhLwfuaBNiY&list=PLTjFYVNE7LTjHJwV994iQHS-bIkh3UXKJ&index=7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment