I need to export a rotable 3D plot to HTML, just like WriteWebGL does in R, but from Python / matplotlib.
When ran in a Jupyter notebook, you can generate a in interactive plot like this:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
...
ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap='Greens');
(source code for this sample here)
As noted, 3D plots can be rotated by the user. How can I get this interactivity exported to HTML from Python?
Does it have to be matplotlib? You could use plotly for that.
Here is a simple example. This modifies an existing empty file called test.html which you then can open in web browser to use the interactive 3D-plot.
import plotly.graph_objects as go
import numpy as np
import plotly.express as px
# Helix equation
t = np.linspace(0, 20, 100)
x, y, z = np.cos(t), np.sin(t), t
fig = go.Figure(data=[go.Scatter3d(
x=x,
y=y,
z=z,
mode='markers',
marker=dict(
size=12,
color=z, # set color to an array/list of desired values
colorscale='Viridis', # choose a colorscale
opacity=0.8
)
)])
# tight layout
fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))
fig.write_html("test.html") #Modifiy the html file
fig.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With