Holoviews(散景)图的辅助轴

时间:2018-11-26 21:14:25

标签: python-3.x plot bokeh axis holoviews

我正在尝试绘制一组图表,这些图表被覆盖以显示它们之间的百分比差异。

我要绘制的代码:

%%output size = 200
%%opts Curve[height=200, width=400,show_grid=True,tools=['hover','box_select'], xrotation=90]
%%opts Curve(line_width=1)


from bokeh.models import Range1d, LinearAxis

new_df = new_df.astype('float')
percent_diff_df = percent_diff_df.astype('float')

def twinx(plot, element):
    # Setting the second y axis range name and range
    start, end = (element.range(1))
    label = element.dimensions()[1].pprint_label
    plot.state.extra_y_ranges = {"foo": Range1d(start=0, end=150)}
    # Adding the second axis to the plot. 
    linaxis = LinearAxis(axis_label='% Difference', y_range_name='foo')
    plot.state.add_layout(linaxis, 'right')

wavelength = hv.Dimension('wavelength', label = 'Wavelength', unit = 'nm')
radiance = hv.Dimension('radiance', label = 'Radiance', unit = 'W/m^2/sr/nm')
curve = hv.Curve((new_df['Wave'], new_df['Level_9']), wavelength, radiance,label = 'Level_9', group = 'Requirements')*\
    hv.Curve((new_df['gcal_wave'],new_df['gcal_9']),wavelength, radiance,label = 'GCAL_9', group = 'Requirements')
curve2 = hv.Curve((percent_diff_df['pdiff_wave'],percent_diff_df['pdiff_9']), label = '% Difference', group = 'Percentage Difference').opts(plot=dict(finalize_hooks=[twinx]), style=dict(color='purple'))
curve * curve2

结果如下所示: enter image description here

平坦的蓝色图形实际上应该看起来像这样: enter image description here

我需要在两个比例尺上绘制两个图形。我似乎能够添加一个比例,但无法将任何图添加到该比例。

1 个答案:

答案 0 :(得分:1)

您的解决方案即将完成。要实际使用在钩子中创建的范围和轴,您需要访问基础的散景字形并设置其y_range_name。

一般示例如下:

import pandas as pd
import holoviews as hv
from bokeh.models.renderers import GlyphRenderer

hv.extension('bokeh')

def apply_formatter(plot, element):

    p = plot.state

    # create secondary range and axis
    p.extra_y_ranges = {"twiny": Range1d(start=0, end=35)}
    p.add_layout(LinearAxis(y_range_name="twiny"), 'right')

    # set glyph y_range_name to the one we've just created
    glyph = p.select(dict(type=GlyphRenderer))[0]
    glyph.y_range_name = 'twiny'

dts = pd.date_range('2015-01-01', end='2015-01-10').values

c_def = hv.Curve((dts, np.arange(10)), name='default_axis').options(color='red', width=300)
c_sec = hv.Curve((dts, np.arange(10)), name='secondary_axis').options(color='blue',width=300, hooks=[apply_formatter])
c_def + c_def * c_sec + c_sec

有关更多详细信息,请参阅此处的原始github问题:https://github.com/pyviz/holoviews/issues/396