水平条形图和折线图与多个x轴在散景中缩放

时间:2017-01-06 20:20:40

标签: bokeh

我试图在一张图表上绘制直方图底层数据的水平直方图和时间序列。当然,这需要共享的y轴和两个x轴 - 一个用于直方图频率,另一个用于日期时间。但是,我似乎无法让日期时间轴发挥得很好。它最终结合了与直方图相关的0值,然后转换为1970-01-01(不是我想要的)。

import numpy as np
import pandas as pd
from bokeh.io import show 
from bokeh.plotting import figure
from bokeh.models import LinearAxis, DatetimeTickFormatter, Range1d

testData = pd.Series(np.random.rand(10), index=pd.date_range('2016-01-01', periods=10)
counts, bins = np.histogram(testData, bins=5)


histAndLine = figure(x_axis_type='datetime')

f = ['%Y-%m-%d']
histAndLine.xaxis.formatter = DatetimeTickFormatter(years=f, months=f, days=f)
histAndLine.extra_x_ranges = {'Counts': Range1d(0, max(counts)+1)}
histAndLine.add_layout(LinearAxis(x_range_name='Counts'), 'above')

histAndLine.line(x=testData.index, y=testData)
histAndLine.circle(x=testData.index, y=testData)

histAndLine.hbar(y=bins[:-1], height=np.diff(bins), left=0, right=counts, x_range_name='Counts')

show(histAndLine)

这个应该在一条线上产生一个条形图,线条和条形图使用大部分图表空间,但我的线条被压缩到右边(因为2016年的日期要大得多)比0 = 1970.

我在这里遗漏了什么吗?

1 个答案:

答案 0 :(得分:1)

数字使用的默认DataRange1d自动调整所有可用字形的范围。如果要排除字形的子集,可以是明确的:

l = histAndLine.line(x=testData.index, y=testData)
c = histAndLine.circle(x=testData.index, y=testData)

# only auto-range over the line and circle, not the bar
histAndLine.x_range.renderers = [l,c] 

enter image description here

相关问题