【问题标题】:Is there a simple way to plot vertical lines on scatter plots in plotly有没有一种简单的方法可以在散点图上绘制垂直线 plotly
【发布时间】:2017-03-03 03:56:09
【问题描述】:

我正在尝试使用plotly 而不是matplotlib 重新处理一些jupyter 笔记本。我原来的功能是

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)

我看不到在plotly 中添加此类垂直线(或其他标记)的简单方法。

我发现 this 在使用袖扣 pandas 集成。虽然函数名称相同(iplot),但似乎没有任何关系。

我还看到了this similar question。我能想到的只是“肯定有更简单的方法”......有吗?

【问题讨论】:

    标签: python matplotlib jupyter-notebook plotly


    【解决方案1】:

    在情节上没有问题。这是我的笔记本单元格:

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

    有关更多示例,请查看此处的形状部分:https://plot.ly/python/shapes/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 2022-07-28
      • 2021-04-03
      • 2020-08-27
      • 1970-01-01
      相关资源
      最近更新 更多