I am trying to re-work some jupyter notebooks using plotly instead of matplotlib. My original function is

def plot_power_spectrum(y):

ps = np.abs(np.fft.fft(y - np.mean(y)))**2

time_step = 1.0/6 # hours

freqs = np.fft.fftfreq(y.size, time_step)

idx = np.argsort(freqs)

plt.plot(freqs[idx], ps[idx])

plt.axvline(2*np.pi/168.0, color="magenta", alpha=0.4, lw=5)

plt.axvline(-2*np.pi/168.0, color="magenta", alpha=0.4, lw=5)

I can't see a simple way to add such vertical lines (or other markup) in plotly.

I found this on using the cufflinks pandas integration. Although the function name is the same (iplot) it doesn't seem to be any relation.

I also saw this similar question. All I could think was "surely there's a simpler way"... Is there?

解决方案

No problems doing it in plotly. Here is my notebook cell:

from plotly.graph_objs import *

from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot

init_notebook_mode(connected=True)

y=np.random.randint(30,size=100)

ps = np.abs(np.fft.fft(y - np.mean(y)))**2

time_step = 1.0/6 # hours

freqs = np.fft.fftfreq(y.size, time_step)

idx = np.argsort(freqs)

data = Scatter(x=freqs[idx], y=ps[idx])

layout = Layout(shapes=[dict({

'type': 'line',

'x0': 2*np.pi/168.0,

'y0': 0,

'x1': 2*np.pi/168.0,

'y1': 35000,

'line': {

'color': '#FF00FF',

'width': 5

}})])

iplot({'data':[data], 'layout':layout})

For more examples check the shapes section here: https://plot.ly/python/shapes/

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐