Hi, I'm a material scientist and part of my work includes creating crystal structures. I'm looking two write a python script that can visualize the crystal structures I create. So the crystal structure file looks something like this
#line number atom type X-cordinate Y-cordinate Z-cordinate 1 A 0.0 0.0 0.0 2 A 1.0 0.0 0.0 3 B 0.0 1.0 0.0 4 B 0.0 0.0 1.0 ... ... ... ... ... 100 B 100.0 0.0 0.0
I'm looking for a python library to visualize these structures in 3D and in an interactive way. The atoms should be represented with spheres of different colors based on their type. Is there a Python library which lets us do this in an easy way?
Interactive animated 3D visualization of movement and orientation data?
python - Make 3D plot interactive in Jupyter Notebook - Stack Overflow
python 3D visualization and graphics - Stack Overflow
Seeking library recommendation for 3D visualization of crystal structure
try:
%matplotlib notebook
see jakevdp reply here
EDIT for JupyterLab users:
Follow the instructions to install jupyter-matplotlib
Then the magic command above is no longer needed, as in the example:
# Enabling the `widget` backend.
# This requires jupyter-matplotlib a.k.a. ipympl.
# ipympl can be install via pip or conda.
%matplotlib widget
# aka import ipympl
import matplotlib.pyplot as plt
plt.plot([0, 1, 2, 2])
plt.show()
Finally, note Maarten Breddels' reply; IMHO ipyvolume is indeed very impressive (and useful!).
You may go with Plotly library. It can render interactive 3D plots directly in Jupyter Notebooks.
To do so you first need to install Plotly by running:
pip install plotly
You might also want to upgrade the library by running:
pip install plotly --upgrade
After that in you Jupyter Notebook you may write something like:
# Import dependencies
import plotly
import plotly.graph_objs as go
# Configure Plotly to be rendered inline in the notebook.
plotly.offline.init_notebook_mode()
# Configure the trace.
trace = go.Scatter3d(
x=[1, 2, 3], # <-- Put your data instead
y=[4, 5, 6], # <-- Put your data instead
z=[7, 8, 9], # <-- Put your data instead
mode='markers',
marker={
'size': 10,
'opacity': 0.8,
}
)
# Configure the layout.
layout = go.Layout(
margin={'l': 0, 'r': 0, 'b': 0, 't': 0}
)
data = [trace]
plot_figure = go.Figure(data=data, layout=layout)
# Render the plot.
plotly.offline.iplot(plot_figure)
As a result the following chart will be plotted for you in Jupyter Notebook and you'll be able to interact with it. Of course you will need to provide your specific data instead of suggeseted one.

The easiest way for 3D visualization in Python is VPython.
For example, to draw a curve in the shape of a square, it's just:
square = curve(pos=[(0,0),(0,1),(1,1),(1,0),(0,0)])
This is vastly easier than MayaVI, VTK, OpenGL, etc.
Another easy option is matplotlib's 3D graphics. It's better on the quantitative details, but not as simple, fast, and interactive for the 3D part.
For 3D charts and graphs (Edit)
If you just need to draw a 3D graph and periodically update it, gnuplot may be appropriate. Using Python's subprocess library you can spawn it in a process to produce graphs, with a variety of output formats, such as SVG or PNG.
Also see Tom's answer below for an alternative library recommendation.
For Realtime 3D (Original)
This answer is for those who are interested in realtime graphics.
The standard way to render 3D graphics is with the OpenGL library which interfaces with the GPU. OpenGL is a big topic, but focusing on a specific application, like drawing 3D lines should not be too difficult.
Bindings exist for python http://pyopengl.sourceforge.net/
GLUT - is probably the best for providing X platform windowing and context setup, and is included in the mentioned bindings.
OpenGL - there are two main ways to use Opengl, a "legacy" style, and a "modern" one. For simple applications legacy is often easier to get started, although learning modern is a better long-term investment.
Here are some links for learning - Don't feel obligated to read them all - pick the topics that are relevant to you.
- http://greendalecs.wordpress.com/2012/04/21/3d-programming-in-python-part-1/
- http://www.informit.com/articles/article.aspx?p=328646&seqNum=6
- http://www.videotutorialsrock.com/
- http://nehe.gamedev.net/ (legacy)