Skip to content

Instantly share code, notes, and snippets.

@computron
Last active July 13, 2022 23:19
Show Gist options
  • 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()
@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