Reputation: 133
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?
Upvotes: 7
Views: 21563
Reputation: 7232
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.
Upvotes: 10
Reputation: 103
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)
Upvotes: 4