Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 674
Miguel Ángel Gárate Fox Avatar asked Jul 17 '26 07:07

Miguel Ángel Gárate Fox


2 Answers

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.

like image 163
Jarno Avatar answered Jul 18 '26 21:07

Jarno


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)
like image 29
alien_plutone Avatar answered Jul 18 '26 21:07

alien_plutone