✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Matplotlib to Plotly conversion

Dear All,
I am really impressed (and motivated) by Plotly. Obviously being new to it I have a learning curve. I followed an example to convert a matplotlib graph to plotly but got irremediably stuck. I am wondering if you can give some guidance. I have the following code for a bode plot. It plots on my end. I am using Python 2.7. I also added pip installed plotly successfully as I have ran some examples already. Anyhow the code I have for a Matplotlib bode plot is:

import numpy as np
from scipy import signal
import matplotlib.pyplot as plt

myrange=np.logspace(-3, 10, 50, endpoint=True)
s1 = signal.ZerosPolesGain([],[100],1e8)
w, mag, phase = signal.bode(s1, myrange)
plt.semilogx(w, mag,‘red’)
plt.show()

I understand from examples at plotly website that if I wrap this code inside of the code below I should get a plot. Could give me some direction, not happening for me. Thanks a bunch.

import plotly
#print plotly.version # version >1.9.4 required
from plotly.graph_objs import Scatter, Layout
plotly.offline.plot({
INSERT HERE
)
})

I was able to make some headway. I wrapped the following code around my code. See below:

import matplotlib.pyplot as plt
import plotly.tools as tls

mpl_fig = plt.figure()
py.sign_in(‘login’, ‘API KEY’)

#INSERT MATPLOTLIB HERE…

plotly_fig = tls.mpl_to_plotly(mpl_fig)
unique_url = py.plot(plotly_fig)

In the end I had the code below and it worked for me. I am trying to understand now what "plotly_fig = tls.mpl_to_plotly(mpl_fig)"
this line is doing behind the scenes. Also how to repeat this using offline browser. Any ideas?

import matplotlib.pyplot as plt
import plotly.tools as tls

mpl_fig = plt.figure()
py.sign_in(‘LOGIN’, ‘API KEY’)

import numpy as np
from scipy import signal
import matplotlib.pyplot as plt

myrange=np.logspace(-3, 10, 50, endpoint=True)
s1 = signal.ZerosPolesGain([],[100],1e8)
w, mag, phase = signal.bode(s1, myrange)
plt.semilogx(w, mag,‘red’)
plt.show()

plotly_fig = tls.mpl_to_plotly(mpl_fig)

unique_url = py.plot(plotly_fig)