有没有一种简单的方法可以在散点图中绘制垂直线 [英] Is there a simple way to plot vertical lines on scatter plots in plotly

查看:127
本文介绍了有没有一种简单的方法可以在散点图中绘制垂直线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用plotly而不是matplotlib对某些jupyter笔记本进行返工.我原来的功能是

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)

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

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

在使用袖扣熊猫集成时,我发现了.尽管函数名称相同(iplot),但似乎没有任何关系.

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

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

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

这篇关于有没有一种简单的方法可以在散点图中绘制垂直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆