Plotly for interactive 3D plotting

An recently wrote a post on how to use the seaborn library. I really like seaborn and use it a lot for 2D plots. However, recently I have been dealing with 3D data and have found plotly to be best. When used in a jupyter notebook, it allows you to easily generate 3D interactive plots. This is extremely useful to visualize structural data.

Plotly is best used in combination with the pandas library, most of the tutorials in its documentations use this library. In the following example they plot data from the iris flower data set.

import plotly.express as px
df = px.data.iris()
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
              color='species')
fig.show()

And this will return the following plot (I tried to make it interactive but I am not sure if that worked. If it didn’t, an interactive version can be found by clicking on the image)

Iris

Personally, I don’t always use pandas data frames for everything, so here is how to plot the position of residues in the H3 loop stored as a numpy array.

import plotly.graph_objects as go
trueH3 = go.Scatter3d(x=truth[0], y=truth[1], z=truth[2], name = "Truth", mode = "lines")
prediction = go.Scatter3d(x=pred[0], y=pred[1], z=pred[2], name = "Prediction", mode = "lines")
fig  = go.Figure(data=[trueH3, prediction])
fig.update_layout(title = "CDRH3 Truth Vs Prediction")
fig.show()

And that would return the following plot (interactive version here):

Plotly can be used for a large number of plotting tasks, so I hope I have convinced you that it is at least worth looking at. If you have read this far I hope this was not a complete waste of your time.

Author