Matplotlib to plotly offline
10,429
Solution 1
A minimal example of converting a matplotlib figure to plotly would look like this.
import matplotlib.pyplot as plt
import plotly
import plotly.plotly as py
import plotly.tools as tls
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 4, 9], "o")
plotly_fig = tls.mpl_to_plotly(fig)
plotly.offline.plot(plotly_fig, filename="plotly version of an mpl figure")
Just posting this as the documentation was somewhat hard to follow.
Solution 2
How about this page at Section Offline Use
BTW: You can also write a static image file as described here
import plotly.io as pio
import plotly.graph_objs as go
fig = go.Figure()
# Do some fig.add_scatter() stuff here
pio.write_image(fig, 'fig1.png')
Solution 3
import plotly.tools as tls
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
x = np.random.random(100) ### toy data
y = np.random.random(100) ### toy data
## matplotlib fig
fig, axes = plt.subplots(2,1, figsize = (10,6))
axes[0].plot(x, label = 'x')
axes[1].scatter(x,y)
## convert and plot in plotly
plotly_fig = tls.mpl_to_plotly(fig) ## convert
iplot(plotly_fig)
Related videos on Youtube
Author by
Miguel Ángel Gárate Fox
Updated on June 04, 2022Comments
-
Miguel Ángel Gárate Fox almost 2 years
I have some matplotlib graphs that need to be viewed offline in a browser, I was using MPLD3 to render them before, but given the need to view the plots without an internet connection, I'm considering using plotly. Is there a way to view matplotlib plotly graphs offline?
-
django almost 5 yearsinstead of
plotly.offline.plot
, can i use something likeplotly.offline.json
so that i can export json with valid plotly js options so i can plot this json as per my need instaed of html -
Aragon over 2 yearsCan we do the reverse? i.e. convert plotly fig to matlibplot fig ?