Matplotlib to plotly offline

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

Answers (3)

Jarno
Jarno

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

A_plus
A_plus

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

Endogen
Endogen

Reputation: 651

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')

Upvotes: 1

Related Questions