在Python中使用散景:时间序列

时间:2015-12-21 22:45:21

标签: python bokeh

我正在尝试在散景中创建一个条形图,并在缩小x轴时进行缩放。我有点堆叠怎么做。我使用以下代码:

import datetime
import time
import numpy as np
import pandas as pd
from bokeh.plotting import *
from bokeh.models import Range1d

def GenerateTimeSeries(StartTime,points):
    valuesStart = [int(c) for c in StartTime.split('-')]
    startT = datetime.datetime(valuesStart[0], valuesStart[1], valuesStart[2], valuesStart[3], valuesStart[4])
    start = time.mktime(startT.timetuple())
    dt =24*3600
    Time = np.linspace(start,start + points*dt, points) * 1000  
    R= np.random.choice(range(0,255),len(Time))
    TimeSeries = pd.DataFrame({'R': R,'time':Time})
    return TimeSeries


timeSeries = GenerateTimeSeries('2015-12-21-0-0',100)
TOOLS = 'xpan,xwheel_zoom'#TOOLS = 'box_select,crosshair,resize,reset'
output_file('Figure.html',title = 'Title')        
p= figure(title = 'Title',x_axis_label='time',plot_width=800,plot_height=500,x_range=Range1d(timeSeries.time[0],timeSeries.time[10]),y_range=Range1d(0,300),tools=TOOLS,x_axis_type = "datetime")#

'灵感'如何使GenerateTimeSeries函数来自散文文档的以下链接:TimeSeriesPloting

现在,如果使用line图,结果似乎没问题:

p.line(timeSeries.time, timeSeries.R, color='#1F78B4', legend='ACME')
show(p)

但是在使用rect

p.rect(x=timeSeries.time[i],y=256/2,width=100,height=256,color='#1F78B4',alpha=0.4)# I want the bar to have a constant height, in case you are wondering why I have the `y` constant.
show(p)

rect中的结果是一些宽度非常小并且彼此距离很近的条形图。我如何按比例缩放以使条彼此靠近?

1 个答案:

答案 0 :(得分:0)

我认为错误在于以下几行:

p.rect(x=timeSeries.time[i],y=256/2,width=100,height=256,color='#1F78B4',alpha=0.4)

因为宽度为100(在x坐标系中)意味着100秒(不是像素)。所以这只是在整个x_range

的一小部分中重新开始
dt =24*3600
Time = np.linspace(start,start + points*dt, points) * 1000