在Bokeh v0.12.13中我可以用什么模块代替ColorBar

时间:2018-02-06 03:17:13

标签: python bokeh

我试图在official document.之后使用ColorBar模块编写脚本 但是,我在下面得到了错误。

ImportError: cannot import name 'ColorBar'  

另外,当我查看bokeh github页面时,不再提供模块ColorBar 有谁知道如何修改以下代码以与v0.12.13兼容?

def heatmap(df,colstr):
    from math import pi
    from bokeh.io import show
    from bokeh.models import (
        ColorBar,
        ColumnDataSource,
        HoverTool,
        LinearColorMapper,
        BasicTicker,
        PrintfTickFormatter,
    )
    from bokeh.plotting import figure

    def preprocess(df,colstr):
        #temp=pd.DataFrame(df[colstr])
        df["Hour"]=df.index.hour.astype(str)
        df["Date"]=df.index.date.astype(str)
        temp=df[["Hour","Date",colstr]]

        return temp

    #add hour column and day column
    df1=preprocess(df,colstr)

    colors = ["#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce", "#ddb7b1", "#cc7878", "#933b41", "#550b1d"]
    mapper = LinearColorMapper(palette=colors, low=df[colstr].min(), high=df[colstr].max())

    source = ColumnDataSource(df1)
    #hard cord. better to be flexiable
    hours = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23']
    #assuming  summerdesign day and winter design day is included
    #dates = list(df1["Date"])[48:]
    dates = list(df1["Date"].drop_duplicates())[4:]

    TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"

    p = figure(title=colstr,
               x_range=hours, y_range=list(reversed(dates)),
               x_axis_location="above", plot_width=1200, plot_height=1200,
              tools=TOOLS, toolbar_location='below')

    p.grid.grid_line_color = None
    p.axis.axis_line_color = None
    p.axis.major_tick_line_color = None
    p.axis.major_label_text_font_size = "5pt"
    p.axis.major_label_standoff = 0
    p.xaxis.major_label_orientation = pi / 3

    p.rect(x="Hour", y="Date", width=1, height=1,
           source=source,
           fill_color={'field': colstr, 'transform': mapper},
           line_color=None)


    color_bar = ColorBar(color_mapper=mapper, major_label_text_font_size="5pt",
                         ticker=BasicTicker(desired_num_ticks=len(colors)),
                         formatter=PrintfTickFormatter(format="%d%%"),
                         label_standoff=6, border_line_color=None, location=(0, 0))
    p.add_layout(color_bar, 'right')


    p.select_one(HoverTool).tooltips = [
         ('date', '@Date'),
         ('hour', '@Hour'),
    ]

    show(p)      # show the plot

1 个答案:

答案 0 :(得分:0)

ColorBar仍然存在:https://github.com/bokeh/bokeh/blob/0.12.13/bokeh/models/annotations.py#L197

通过bokeh.models.ColorBar导入它仍然适用于我在不同机器上的多次安装。

最可能的原因是你实际上没有使用Bokeh 0.12.13运行此代码(虽然我不确定Bokeh安装有多久不能有ColorBar型号),或者你的安装有点腐败。