Example #1
0
def edge_labels(graph_render):
    source = graph_render.edge_renderer.data_source
    xcoord = bkm.CustomJSTransform(v_func=EDGE_JS_CODE % "0",
                                   args=dict(
                                       provider=graph_render.layout_provider,
                                       source=source))
    ycoord = bkm.CustomJSTransform(v_func=EDGE_JS_CODE % "1",
                                   args=dict(
                                       provider=graph_render.layout_provider,
                                       source=source))

    source.data['labels'] = [
        str(datum) if not pd.isnull(datum) else ''
        for datum in source.data['relative_allocation']
    ]
    # Use the transforms to supply coords to a LabelSet
    labels = bkm.LabelSet(x=bkt.transform('start', xcoord),
                          y=bkt.transform('start', ycoord),
                          text='labels',
                          text_font_size="12px",
                          x_offset=0,
                          y_offset=0,
                          source=source,
                          render_mode='canvas')
    return labels
Example #2
0
def make_plot(source, df, title, title2):
    plot = figure(x_axis_type="datetime",
                  plot_width=1500,
                  tools="",
                  toolbar_location="above")
    plot.title.text = title

    #plot.line("Date","ConfirmedCases",source=source)
    plot.line("Date",
              "PredictedDailyNewCases",
              source=source,
              line_width=3,
              color="orange")
    plot.line("Date", "DailyChangeConfirmedCases", source=source, line_width=3)
    #plot.line(x=[current_time,current_time], y=[-10,50000], color="#FB8072", line_width=4, line_alpha =0.6, line_dash="dashed")

    ################### INTERVENTIONS #########################

    colors = list(brewer['RdYlGn'][4])
    colors[1] = '#FDE724'
    colors[2] = '#FBA40A'
    mapper = LinearColorMapper(palette=colors,
                               low=df["C1_School closing"].min(),
                               high=df["C2_Workplace closing"].max())

    graph = figure(plot_width=1500,
                   plot_height=600,
                   title="Interventions",
                   x_range=dates,
                   y_range=interventions,
                   toolbar_location=None,
                   tools="",
                   x_axis_location=None)

    for i, j in enumerate(interventions):
        graph.rect(x="Date2",
                   y=i,
                   width=1.01,
                   height=0.8,
                   source=source,
                   line_color=transform(j, mapper),
                   fill_color=transform(j, mapper))

    color_bar = ColorBar(color_mapper=mapper,
                         location=(0, 0),
                         ticker=BasicTicker(desired_num_ticks=len(colors)))

    graph.add_layout(color_bar, 'right')

    graph.axis.axis_line_color = None
    graph.axis.major_tick_line_color = None
    graph.axis.major_label_text_font_size = "10px"
    graph.axis.major_label_standoff = 0
    #graph.xaxis.major_label_orientation = 1.2
    graph.outline_line_color = None
    graph.xgrid.grid_line_color = None
    #graph.line(x=[df["Date2"].iloc[-2],df["Date2"].iloc[-2]], y=["C1_School closing", "H6_Facial Coverings"], color="#FB8072", line_width=4, line_alpha =0.9, line_dash="dashed")
    graph.title.text = title2

    return plot, graph
Example #3
0
def make_earning_plot(data, symbol=''):
    num_of_year = len(data['year'].unique())
    colors = magma(num_of_year)
    mapper = LinearColorMapper(palette=colors,
                               low=data.year.max(),
                               high=data.year.min())

    source = ColumnDataSource(data)
    plot = figure(
        x_range=Range1d(0, 5),
        plot_height=500,
        plot_width=1000,
    )
    reported = plot.circle(
        x='quarter',
        y='reported',
        source=source,
        name='reported',
        legend='Reported',
        size=20,
        color=transform('year', mapper),
        fill_color=transform('year', mapper),
    )
    estimated = plot.circle(
        x='quarter',
        y='estimate',
        source=source,
        name='estimated',
        legend='Estimated',
        size=15,
        color=transform('year', mapper),
        fill_alpha=0.4,
        muted_alpha=0.1,
    )
    price_hover = HoverTool(renderers=[reported],
                            tooltips=[
                                ("year: ", "@year"),
                                ("reported: ", "@reported"),
                                ("estimate: ", "@estimate"),
                                ("surprise: ", "@surprise{0.2f}"),
                                ("surprise_per: ", "@surprise_per"),
                            ])

    plot.add_tools(price_hover)
    plot.title.text = f"{symbol.upper()} Earning" if symbol else "Earning"
    plot.legend.location = "top_left"
    plot.xaxis.axis_label = 'Quarter'
    plot.yaxis.axis_label = 'Price'
    plot.ygrid.band_fill_color = "olive"
    plot.ygrid.band_fill_alpha = 0.1
    plot.legend.click_policy = "mute"
    plot.xaxis.ticker = [1, 2, 3, 4]
    return plot
Example #4
0
    def setup_plot(src):
        """
        Create the heatmap plot.

        src: ColumnDataSource
        """

        # Prepare a color pallete and color mapper
        mapper = LinearColorMapper(
            # force 0 to be mapped with white color
            palette=tuple(
                itertools.chain(["#FFFFFF"], tuple(reversed(Viridis256[1:])))),
            low=0,
            high=1000,
        )

        # Actual figure setup
        p = figure(
            plot_width=800,
            plot_height=400,
            x_range=(src.data["Miete"].min(), src.data["Miete"].max()),
            y_range=(src.data["Einkommen"].min(), src.data["Einkommen"].max()),
            tools="hover",
            tooltips="Housing Benefit: @Wohngeld{0.0f}€",
        )

        p.rect(
            x="Miete",
            y="Einkommen",
            width=25,
            height=src.data["Einkommen"][1] - src.data["Einkommen"][0],
            source=src,
            line_color=transform("Wohngeld", mapper),
            fill_color=transform("Wohngeld", mapper),
        )

        color_bar = ColorBar(
            color_mapper=mapper,
            location=(0, 0),
            ticker=BasicTicker(desired_num_ticks=20),
            formatter=NumeralTickFormatter(format="0€"),
            label_standoff=12,
        )
        p.add_layout(color_bar, "right")

        plot = plotstyle(p, plot_dict)

        return plot
Example #5
0
def render(pkg):
    global current_rpm, level, hover, source
    current_rpm = pkg
    hist.append([pkg, level])
    newG = Graph()
    nrn, nre = graphe(pkg, newG)
    if nrn == 0:
        return Div(text="This package is unknown")
    newgraph = from_networkx(newG, spring_layout, scale=4, center=(0, 0))
    newplot = figure(title="RPM network",
                     sizing_mode="scale_width",
                     aspect_ratio=2,
                     x_range=(-2.2, 2.2),
                     y_range=(-2.1, 2.1),
                     tools="tap",
                     toolbar_location=None)
    newplot.axis.visible = False
    newplot.grid.visible = False
    newgraph.node_renderer.glyph = Rect(height=0.07,
                                        width=0.1,
                                        fill_color="color",
                                        fill_alpha=0.0,
                                        line_alpha=0.0)
    if nre != 0:
        newgraph.edge_renderer.glyph = MultiLine(line_color="color",
                                                 line_alpha=0.8)
    newplot.renderers.append(newgraph)
    source = newgraph.node_renderer.data_source
    xcoord = CustomJSTransform(v_func=code % "0",
                               args=dict(provider=newgraph.layout_provider))
    ycoord = CustomJSTransform(v_func=code % "1",
                               args=dict(provider=newgraph.layout_provider))
    source.selected.on_change('indices', selected)
    labels = LabelSet(x=transform('index', xcoord),
                      y=transform('index', ycoord),
                      text='name',
                      text_font_size="12px",
                      y_offset=-6,
                      x_offset="offset",
                      background_fill_color='color',
                      background_fill_alpha=0.85,
                      border_line_color='color',
                      border_line_alpha=1.0,
                      source=source,
                      render_mode='canvas')
    newplot.add_tools(hover)
    newplot.add_layout(labels)
    return newplot
Example #6
0
def update_plot(attr, old, new):
    length = slider.value
    width = slider.value
    new_df = get_dataset(length, width)
    source.data.update(new_df.data)
    plot.x_range.factors = [str(x) for x in list(range(1, length + 1))]
    plot.y_range.factors = [str(x) for x in list(range(1, width + 1))]
    plot.image_url(url=['myapp/static/images/sh1.jpg'],
                   x=0,
                   y=0,
                   w=length,
                   h=length,
                   anchor="bottom_left")

    colors = [
        "#550b1d", "#933b41", "#cc7878", "#ddb7b1", "#dfccce", "#e2e2e2",
        "#c9d9d3", "#a5bab7", "#75968f"
    ]
    mapper = LinearColorMapper(palette=colors, low=0, high=100)
    plot.rect(x="length",
              y="width",
              width=1,
              height=1,
              source=source,
              line_color=None,
              fill_color=transform('score', mapper),
              alpha=0.4)

    hover = HoverTool(tooltips=[("Potential Score for District Energy System",
                                 "@score")])
    plot.add_tools(hover)

    p.text = "<h><b>The Shanghai city has been evaluated by %d districts for tessellation</b></h>" % (
        length * width)
    def make_correlation_plot(df, param_list):
        df_corr = df[param_list].corr().fillna(0)
        df_corr = df_corr.stack().rename("value").reset_index()

        print(df_corr)

        colors = RdBu[11]

        # Had a specific mapper to map color with value
        mapper = LinearColorMapper(palette=colors, low=-1, high=1)

        corr_figure.x_range.factors = list(df_corr.level_0.drop_duplicates())
        corr_figure.y_range.factors = list(df_corr.level_1.drop_duplicates())

        hover = HoverTool(tooltips=[
            ("Corr", "@value"),
        ])

        # Create rectangle for heatmap
        corr_figure.rect(x="level_0",
                         y="level_1",
                         width=1,
                         height=1,
                         source=ColumnDataSource(df_corr),
                         line_color=None,
                         fill_color=transform('value', mapper))
        corr_figure.add_tools(hover)

        # Add legend
        color_bar = ColorBar(color_mapper=mapper, location=(0, 0))

        corr_figure.add_layout(color_bar, 'left')
Example #8
0
def heatmap():
  data=pd.DataFrame(
    [ [1,0.2,0.3],
      [0.3,1,0.5],
      [-0.5,0.8,1]],
    columns=['a','b','c'])
  value=[]
  for x in data.apply(tuple):
    value.extend(x)
  absValue=[abs(number) for number in value]
  print(data)
  source={
    'x': [i for i in list(data.columns) for j in list(data.columns)],
    'y': list(data.columns)*len(data.columns),
    'value':value,
    'abs':absValue
  }
  p=figure(title = "heatmap", sizing_mode="fixed", plot_width=600, plot_height=400,tools='pan,wheel_zoom,box_zoom,save,reset',x_range=list(data.columns),y_range=list(reversed(data.columns)))
  p.toolbar.logo=None
  from bokeh.models import LinearColorMapper
  from bokeh.palettes import inferno,YlOrRd,Magma,PuBu,Greys
  from bokeh.transform import transform
  crs=PuBu[9]
  crs.reverse()
  mapper = LinearColorMapper(palette=crs, low=min(absValue), high=max(absValue))
  fontMapper=LinearColorMapper(palette=[Greys[5][0],Greys[5][4]], low=min(absValue), high=max(absValue))
  p.rect(x="x", y="y", width=1, height=1, source=source,
       line_color=None, fill_color=transform('abs', mapper))
  p.text(x="x", y="y",text='value', source=source,text_font_size='2em',text_font_style='bold',text_align='center',text_baseline='middle',text_color=transform('abs',fontMapper))
  p.add_tools(
    HoverTool(tooltips = [('Value', '@value')])
  )
  return json.dumps(json_item(p))
    def draw(self, size=400):
        index_label = 'Predicted'
        column_label = 'Actual'

        matrix = self.generate_data()
        min_val, max_val = matrix.Value.min(), matrix.Value.max()
        source = ColumnDataSource(matrix)
        mapper = LinearColorMapper(palette=COLORS, low=min_val, high=max_val)

        hover = HoverTool(tooltips=[
            ('Number', f"@Value")
        ])

        p = figure(plot_width=size,
                   plot_height=size,
                   title='Confusion Matrix',
                   tools=[hover],
                   toolbar_location=None,
                   x_range=self.labels,
                   y_range=list(reversed(self.labels)))

        p.yaxis.axis_label = index_label
        p.xaxis.axis_label = column_label

        p.rect(x=column_label,
               y=index_label,
               width=1,
               height=1,
               source=source,
               fill_color=transform('Value', mapper))
        self.plot = p
        return p
Example #10
0
def make_plot(mapper_type, palette):
    mapper_opts = dict(palette=palette, low=1, high=1000)
    if mapper_type == "linear":
        mapper = LinearColorMapper(**mapper_opts)
    else:
        mapper = LogColorMapper(**mapper_opts)

    p = figure(toolbar_location=None,
               tools='',
               title="",
               x_axis_type=mapper_type,
               x_range=(1, 1000))
    p.title.text = f"{palette} with {mapper_type} mapping"
    p.circle(x='x',
             y='y',
             alpha=0.8,
             source=source,
             size=6,
             fill_color=transform('x', mapper),
             line_color=None)

    color_bar = ColorBar(color_mapper=mapper,
                         ticker=p.xaxis.ticker,
                         formatter=p.xaxis.formatter,
                         padding=0)
    p.add_layout(color_bar, 'below')

    return p
Example #11
0
def createMatrix(width, height, column_list_original, index_list_original, mapper, color_bar, source_normal):
    #Color mapper
    
    #Hover information
    hover = HoverTool()
    hover.tooltips = [
        ("(Author1, Author2)", "(@AuthorX, @AuthorY)"),
        ("Similarity rate", "@sim"),
        ("Index", "@index")
    ]
    #Basic interaction tools
    TOOLS = "save,pan,box_zoom,reset,wheel_zoom"
    #Graph grid and properties
    figure_matrix = figure(plot_width = width, plot_height=height,
            x_range=column_list_original, y_range=index_list_original, #feeding columns and range
            toolbar_location="right", tools=TOOLS, x_axis_location="above")

    r = figure_matrix.rect(x='AuthorX', y='AuthorY', width=1, height=1, source=source_normal,#feeding x,y for each square
        line_color=None, fill_color=transform('sim', mapper))
    figure_matrix.tools.append(hover)
    figure_matrix.add_layout(color_bar, 'right')
    figure_matrix.axis.axis_line_color = None
    figure_matrix.axis.major_tick_line_color = None
    figure_matrix.axis.major_label_text_font_size = "7pt"
    figure_matrix.axis.major_label_standoff = 0
    figure_matrix.xaxis.major_label_orientation = np.pi/2
    return figure_matrix
Example #12
0
def create_corrplot(df, timeframe, mpr, wd):
    df = df.pct_change()
    df = df.corr()

    df.index.name = 'allcurrenciesX'
    df.columns.name = 'allcurrenciesY'

    #formatting the dataframe
    df = df.stack().rename("value").reset_index()

    #mapper = LinearColorMapper( palette=PuBu[9], low=0, high=1)

    Hover = HoverTool(tooltips=[
        ('corr-coefficient', '@value'),
    ], )

    toolstoadd = "box_zoom", "reset"
    p = figure(tools=toolstoadd,
               plot_width=wd,
               plot_height=400,
               title="Correlation plot for " + timeframe + " period",
               x_range=list(df.allcurrenciesX.drop_duplicates()),
               y_range=list(df.allcurrenciesY.drop_duplicates()),
               toolbar_location="above",
               x_axis_location="below")
    p.add_tools(Hover)

    p.rect(x="allcurrenciesX",
           y="allcurrenciesY",
           width=1,
           height=1,
           source=ColumnDataSource(df),
           line_color=None,
           fill_color=transform('value', mpr))
    return p
Example #13
0
    def cm_callback():
        global target_data
        cm = confusion_matrix(target_data[label_select.value],
                              target_data[pred_select.value])
        cm = pd.DataFrame(cm)
        cm = cm.stack().reset_index().rename(
            columns={
                'level_0': label_select.value,
                'level_1': pred_select.value,
                0: 'count'
            })
        confusion_matrix_source.data = cm
        mapper = LinearColorMapper(palette=Viridis256,
                                   low=cm['count'].min(),
                                   high=cm['count'].max())
        hm.rect(x=pred_select.value,
                y=label_select.value,
                source=confusion_matrix_source,
                width=1,
                height=1,
                line_color=None,
                fill_color=transform('count', mapper))
        hm.text(x=pred_select.value,
                y=label_select.value,
                text="count",
                text_font_style="bold",
                source=confusion_matrix_source,
                text_align="left",
                text_baseline="middle")

        color_bar = ColorBar(color_mapper=mapper, label_standoff=12)
        hm.add_layout(color_bar, 'right')

        hm.visible = True
        unvisible_other_plots(hm, visible_plots)
Example #14
0
def create_marauder_map_figure(mdate):
    df = RIndexStock().get_data(mdate)
    TOOLTIPS = [("code", "@code"), ("(pday, profit)", "(@pday, @profit)")]
    TOOLS = [BoxZoomTool(), ResetTool(), HoverTool(tooltips=TOOLTIPS)]
    p = figure(x_axis_label='时间',
               y_axis_label='强度',
               tools=TOOLS,
               toolbar_location="above",
               title="牛熊比例")
    if df is None or df.empty: return p
    source = ColumnDataSource(df)
    color_mapper = LinearColorMapper(palette="Viridis256",
                                     low=df.profit.min(),
                                     high=df.profit.max())
    p.circle(x='pday',
             y='profit',
             color=transform('profit', color_mapper),
             size=5,
             alpha=0.6,
             source=source)
    color_bar = ColorBar(color_mapper=color_mapper,
                         label_standoff=12,
                         location=(0, 0),
                         title='强度')
    p.add_layout(color_bar, 'right')
    return p
def _bokeh_heatmap(source,
                   colormapper,
                   x_label,
                   y_label,
                   x_range,
                   y_range,
                   x_log=False,
                   y_log=False):
    p = figure(x_range=x_range,
               y_range=y_range,
               x_axis_type="log" if x_log else "linear",
               y_axis_type="log" if y_log else "linear",
               toolbar_location=None,
               tools="")
    p.rect(x=x_label,
           y=y_label,
           width=1,
           height=1,
           source=source,
           fill_color=transform('zz', colormapper),
           line_color=None)
    color_bar = ColorBar(color_mapper=colormapper,
                         location=(0, 0),
                         ticker=BasicTicker(desired_num_ticks=20),
                         formatter=BasicTickFormatter(use_scientific=False))
    p.add_layout(color_bar, 'right')
    p.xaxis.axis_label = x_label
    p.yaxis.axis_label = y_label
    return p
    def two_station_heatmap(self):
        fig = figure(width=700, match_aspect=True, tools='')
        fig.xgrid.grid_line_color = None
        fig.ygrid.grid_line_color = None
        s_id1, s_id2 = self.x_select.value, self.y_select.value
        df_counts = self.df_bikes_avail.groupby(
            [s_id1, s_id2]).size().reset_index(name='counts')
        df_counts.rename(columns={s_id1: s_id1, s_id2: s_id2}, inplace=True)
        source = ColumnDataSource(df_counts)

        pallette = [
            '#084594', '#2171b5', '#4292c6', '#6baed6', '#9ecae1', '#c6dbef',
            '#deebf7', '#f7fbff'
        ][::-1]
        mapper = LinearColorMapper(palette=pallette,
                                   low=0,
                                   high=df_counts.counts.max())
        color_bar = ColorBar(color_mapper=mapper)

        fig.rect(x=str(s_id1),
                 y=str(s_id2),
                 width=1.0,
                 height=1.0,
                 line_alpha=0.0,
                 fill_color=transform('counts', mapper),
                 source=source)

        fig.add_layout(color_bar, 'right')

        return fig
Example #17
0
 def plot_heatmap_condition(condition2plot):
     """ Create heatmap plot: x axis = time, y axis = MSOAs, colour = nr people with condition = condition2plot. condition2plot is key to conditions_dict."""
     
     # Prep data
     var2plot = msoacounts_dict[condition2plot]
     var2plot = var2plot.rename_axis(None, axis=1).rename_axis('MSOA', axis=0)
     var2plot.columns.name = 'Day'
     # reshape to 1D array or rates with a month and year for each row.
     df_var2plot = pd.DataFrame(var2plot.stack(), columns=['condition']).reset_index()
     source = ColumnDataSource(df_var2plot)
     # add better colour 
     mapper_1 = LinearColorMapper(palette=colours_ch_cond[condition2plot], low=0, high=var2plot.max().max())
     # create fig
     s1 = figure(title="Heatmap",
                x_range=list(var2plot.columns), y_range=list(var2plot.index), x_axis_location="above")
     s1.rect(x="Day", y="MSOA", width=1, height=1, source=source,
            line_color=None, fill_color=transform('condition', mapper_1))
     color_bar_1 = ColorBar(color_mapper=mapper_1, location=(0, 0), orientation = 'horizontal', ticker=BasicTicker(desired_num_ticks=len(colours_ch_cond[condition2plot])))
     s1.add_layout(color_bar_1, 'below')
     s1.axis.axis_line_color = None
     s1.axis.major_tick_line_color = None
     s1.axis.major_label_text_font_size = "7px"
     s1.axis.major_label_standoff = 0
     s1.xaxis.major_label_orientation = 1.0
     # Create hover tool
     s1.add_tools(HoverTool(
         tooltips=[
             ( f'Nr {condition2plot}',   '@condition'),
             ( 'Day',  '@Day' ), 
             ( 'MSOA', '@MSOA'),
         ],
     ))
     s1.toolbar.autohide = False
     plotref_dict[f"hm{condition2plot}"] = s1    
Example #18
0
    def _plot_wcorr(Wcorr, L):

        f = figure(tools="box_select, pan, reset, save")
        f.plot_width = 700
        f.plot_height = 600

        # Background settings
        f.background_fill_color = '#859dcd'
        f.background_fill_alpha = 0.05

        # Title settings
        f.title.text = "W-Correlation for L={}".format(L)
        f.title.text_font = 'Helvetica'
        f.title.text_font_size = '24px'
        f.title.align = 'center'
        f.title.text_font_style = "italic"

        # Axis settings
        f.xaxis.axis_label = 'Fⱼ'
        f.yaxis.axis_label = 'Fᵢ'
        f.axis.axis_label_text_font = 'Helvetica'
        f.axis.axis_label_text_font_size = '24px'
        f.axis.major_label_orientation = 0
        f.x_range = Range1d(start=0.5, end=L + 0.5)
        f.y_range = Range1d(start=L + 0.5, end=0.5)
        f.axis[0].ticker.desired_num_ticks = L
        f.axis[0].ticker.num_minor_ticks = 0

        data = DataFrame(Wcorr)
        axis = [i for i in range(1, Wcorr.shape[0] + 1)]

        data['F_i'] = axis
        data.set_index('F_i', inplace=True)

        data.columns = axis
        data.columns.name = 'F_j'
        df = DataFrame(data.stack(), columns=['corr']).reset_index()
        source = ColumnDataSource(df)

        # this is the colormap from the original NYTimes plot

        mapper = LinearColorMapper(palette=color.palettes['colors_2'], low=0, high=1)

        f.rect(x="F_i", y="F_j", width=1, height=1, source=source,
               line_color=None, fill_color=transform('corr', mapper))

        color_bar = ColorBar(color_mapper=mapper, location=(0, 0),
                             ticker=BasicTicker(desired_num_ticks=len(color.palettes['colors_2'])),
                             formatter=PrintfTickFormatter(format="%.2f"))

        f.add_layout(color_bar, 'right')

        hover = HoverTool(tooltips=[("Components", "(@F_i, @F_j)"),
                                    ("Correlations", "@corr")])
        f.add_tools(hover)

        show(f)
    def mat(self, output_name, nodesX, nodesY, counts):

        xname = []
        yname = []

        for node1 in nodesX:
            for node2 in nodesY:
                xname.append(node1)
                yname.append(node2)

        source = ColumnDataSource(data=dict(
            xnames=xname,
            ynames=yname,
            count=counts.flatten(),
        ))


        colors = ["#FFFFFF", "#CCCCFF", "#9999FF", "#5555FF", "#0000FF", "#0000CC", "#0000AA", "#000088", "#000000"]

        exp_cmap = LogColorMapper(palette=colors, low = 0, high = 10)
        # exp_cmap = LogColorMapper(palette=colors, low = np.min(counts), high = np.max(counts))

        p = figure(title="Matrix Figure",
                   x_axis_location="above", tools="hover,save",
                   x_range=nodesX, y_range=list(reversed(nodesY)))

        p.rect(x='xnames', y='ynames', width=0.9, height=0.9,
               source=source, line_color=None, fill_color=transform('count', exp_cmap))

        p.plot_width = 800
        p.plot_height = 800
        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 = np.pi / 3

        #圖例
        color_bar = ColorBar(color_mapper=exp_cmap, location=(0, 0),
                             ticker=BasicTicker(desired_num_ticks=len(colors)), 
                             formatter=PrintfTickFormatter(format="%d"))

        p.add_layout(color_bar, 'right')

        #游標所指“點”的說明格式
        p.select_one(HoverTool).tooltips = [
            ('names', '@ynames -> @xnames'),
            ('count', '@count'),
        ]

        output_file(output_name + ".html", title=output_name)
        
        show(p)  # show the plot
Example #20
0
def node_labels(graph_render):
    source = graph_render.node_renderer.data_source

    xcoord = bkm.CustomJSTransform(
        v_func=NODE_JS_CODE % "0",
        args=dict(provider=graph_render.layout_provider))
    ycoord = bkm.CustomJSTransform(
        v_func=NODE_JS_CODE % "1",
        args=dict(provider=graph_render.layout_provider))

    # Use the transforms to supply coords to a LabelSet
    labels = bkm.LabelSet(x=bkt.transform('index', xcoord),
                          y=bkt.transform('index', ycoord),
                          text='index',
                          text_font_size="10px",
                          x_offset=0,
                          y_offset=-5,
                          source=source,
                          render_mode='canvas',
                          text_align='center')
    return labels
Example #21
0
def heatmap(x=None,
            y=None,
            z=None,
            data=None,
            x_order=None,
            y_order=None,
            p=None,
            width=1200,
            height=500,
            **plot_kw):

    if x_order is None:
        x_order = data[x[0]].unique()
    if y_order is None:
        y_order = data[y].unique()

    tooltips = zip(data.columns, '@' + data.columns)

    if p is None:
        p = figure(height=max(height, 10 * len(y_order)),
                   plot_width=max(width, 10 * len(x_order)),
                   tooltips=list(tooltips),
                   tools=CFG['tools'],
                   x_range=x_order.tolist(),
                   y_range=y_order.tolist(),
                   x_axis_location="above",
                   min_border=CFG['padding'])

    mapper = LinearColorMapper(palette=cc.fire,
                               low=data[z].min(),
                               high=data[z].max())

    p.rect(x=x[0],
           y=y,
           width=1,
           height=1,
           source=data,
           line_color=None,
           fill_color=transform(z, mapper))

    color_bar = ColorBar(color_mapper=mapper,
                         ticker=BasicTicker(desired_num_ticks=5),
                         location=(0, 0))

    p.add_layout(color_bar, 'right')

    p.axis.axis_line_color = None
    p.axis.major_tick_line_color = None
    p.axis.major_label_text_font_size = "8pt"
    p.axis.major_label_standoff = 0
    p.xaxis.major_label_orientation = 'vertical'

    return p
Example #22
0
def general_mapper(column, mapped_to, to_map):
    m = dict(zip(to_map, mapped_to))
    v_func = """
    const first = xs[0]
    const norm = new Float64Array(xs.length)
    for (let i = 0; i < xs.length; i++) {
        norm[i] = m[xs[i]];
    }
    return norm
    """
    t = CustomJSTransform(args={"m": m}, v_func=v_func)

    return transform(column, t)
Example #23
0
def matrix_heatmap(the_matrix):
    'Create a bokeh graphic with matrix cells colored by value. Or use bokeh "heatmap".'
    # pandas 'stack' is equivalent to R reshape gather, or melt from reshape2, from wide to long format. 
    # Prepare data.frame in the right format
    # the_matrix.drop(['F', 'sup'], axis=1, inplace=True)
    # the_matrix.columns=  the_matrix.index
    df = the_matrix.stack().rename("value").reset_index()

    #  The plot output:
    # output_file(ROOT_DIR / "cmPlot.html")

    # You can use your own palette here
    colors = brewer['YlGnBu'][9][1:9]

    # Had a specific mapper to map color with value
    mapper = LinearColorMapper(
        palette=colors, low=df.value.min(), high=df.value.max())
    # Define a figure
    p = figure(
        plot_width=800,
        plot_height=700,
        title="20 Newsgroups Confusion Matrix",
        y_range=list(df.level_0.drop_duplicates()),
        x_range=list(df.level_1.drop_duplicates()),
        toolbar_location=None,
        tools="",
        x_axis_location="above")

    df['cnt'] = df['value'].apply(str)
    cds = ColumnDataSource(df)
    # Create rectangle for heatmap
    p.rect(
        y="level_0",
        x="level_1",
        alpha = 0.4,
        width=1,
        height=1,
        source=cds,
        line_color=None,
        fill_color=transform('value', mapper))
    glyph = Text(y="level_0", x="level_1", text="cnt", x_offset= -10.0, y_offset=10.0, text_color="black")
    p.add_glyph(cds, glyph)

    #Add legend
    color_bar = ColorBar(
        color_mapper=mapper,
        location=(0,0),
        ticker=BasicTicker(desired_num_ticks=len(colors)))
    p.add_layout(color_bar, 'right')

    return p
Example #24
0
def plotConfusionMatrix(df, width, height):

    from bokeh.palettes import Blues8

    # Had a specific mapper to map color with value
    mapper = LinearColorMapper(palette=Blues8[::-1],
                               low=df.value.min(),
                               high=df.value.max())

    TOOLS = "hover,save,reset"

    # Define a figure
    p = figure(
        plot_width=width,
        plot_height=height,
        #     title="",
        x_range=list(df.Treatment.drop_duplicates()),
        y_range=list(df.Prediction.drop_duplicates()),
        toolbar_location='above',
        tools=TOOLS,
        tooltips=[('Counts', '@value')],
        x_axis_location="below")

    p.xaxis.axis_label = "Prediction"
    p.yaxis.axis_label = "Truth"

    # Create rectangle for heatmap
    p.rect(x="Prediction",
           y="Treatment",
           width=1,
           height=1,
           source=ColumnDataSource(df),
           line_color=None,
           fill_color=transform('value', mapper))

    # Add legend
    color_bar = ColorBar(color_mapper=mapper,
                         location=(0, 0),
                         label_standoff=12,
                         border_line_color=None,
                         ticker=BasicTicker(desired_num_ticks=len(Blues8)))

    color_bar.background_fill_alpha = 0.0

    p.add_layout(color_bar, 'right')

    p.background_fill_alpha = 0.0
    p.border_fill_alpha = 0.0

    return p
Example #25
0
def create_confusion_matrix(results_df, pred_column, logger):

    y_true = results_df['Orig_Label'].values
    crt_y_pred = results_df[pred_column].values

    num_classes = int(logger.config_dict['NUM_CLASSES'])
    cmat = confusion_matrix(y_true, crt_y_pred)

    df = pd.DataFrame(cmat,
                      columns=[str(i) for i in range(num_classes)],
                      index=[str(i) for i in range(num_classes)])

    df.index.name = 'Label'
    df.columns.name = 'Prediction'
    df = df.stack().rename("value").reset_index()

    colors = [matplotlib.colors.rgb2hex(c) for c in sns.color_palette("Blues")]

    mapper = LinearColorMapper(palette=colors,
                               low=df.value.min(),
                               high=df.value.max())
    p = figure(plot_width=800,
               plot_height=800,
               title="Confusion matrix for {} test data".format(pred_column),
               x_range=list(df.Label.drop_duplicates()),
               y_range=list(df.Prediction.drop_duplicates()),
               tools="wheel_zoom,box_zoom,save, hover")
    p.rect(x="Label",
           y="Prediction",
           width=1,
           height=1,
           source=ColumnDataSource(df),
           line_color=None,
           fill_color=transform('value', mapper))
    color_bar = ColorBar(color_mapper=mapper,
                         location=(0, 0),
                         ticker=BasicTicker(desired_num_ticks=len(colors)))

    hover = p.select(dict(type=HoverTool))
    hover.tooltips = [("Value", "@value"), ("Label", "@Label"),
                      ("Prediction", "@Prediction")]
    p.add_layout(color_bar, 'right')

    model_type = logger.config_dict['RESULTS_FILE'].split('_')[0]
    output_file(
        logger.get_output_file("{}_{}_cmat.html".format(
            model_type, pred_column)))
    save(p)

    return df
Example #26
0
def viz_dist_map(df):

    #df = df.loc[df['station']]
    df = df.loc[(df['week'] >= 2) & (df['week'] <= 29)]

    df.station = df.station.astype('str')
    df['unique_day'] = df.apply(lambda x: 'w{}d{}'.format(x['week'], x['day']),
                                axis=1)
    df.unique_day = df.unique_day.astype('str')
    df = df.drop(columns=['year', 'week', 'day', 'satsun', 'counts'])
    df = df.set_index('unique_day').reset_index()

    source = ColumnDataSource(df)
    mapper = LinearColorMapper(palette=brewer['PRGn'][11],
                               low=-0.05,
                               high=1.05)

    print(list(df.percentile))

    p = figure(plot_width=1000,
               plot_height=800,
               x_range=list(df.unique_day.unique()),
               y_range=list(df.station.unique()),
               toolbar_location='above',
               x_axis_location="above")

    p.rect(y="station",
           x="unique_day",
           width=1,
           height=1,
           source=source,
           line_color=None,
           fill_color=transform('percentile', mapper))

    color_bar = ColorBar(
        color_mapper=mapper,
        location=(0, 0),
        ticker=FixedTicker(
            ticks=[0.05, 0.15, 0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 0.85, 0.95
                   ]),
        label_standoff=5,
        orientation='horizontal')
    p.add_layout(color_bar, 'below')

    p.xaxis.major_label_text_color = None
    p.yaxis.major_label_text_color = None
    p.add_tools(
        HoverTool(tooltips=[('station', '@station'), ('day', '@unique_day')]))

    return p
Example #27
0
def plot_wordsAwards(df,
                     amount_awarded_per_day,
                     word_count,
                     y_axis_type='log',
                     title='Funding vs Number of words used in the abstract'):

    wm = df[[amount_awarded_per_day, word_count]]

    source = ColumnDataSource(wm)
    color_mapper = LinearColorMapper(palette="Viridis256",
                                     low=wm['amount_awarded_per_day'].min(),
                                     high=wm['amount_awarded_per_day'].max())

    p = figure(plot_width=1000,
               plot_height=450,
               tools='box_select, wheel_zoom, reset, save',
               toolbar_location='above',
               x_axis_label='word count',
               y_axis_label='amount awarded per day (log)',
               y_axis_type=y_axis_type)

    p.circle(x=word_count,
             y=amount_awarded_per_day,
             color=transform(amount_awarded_per_day, color_mapper),
             size=10,
             alpha=0.6,
             source=wm)
    p.title.text = title

    color_bar = ColorBar(color_mapper=color_mapper,
                         label_standoff=15,
                         location=(0, 0),
                         title='Amount awarded per day')

    p.add_layout(color_bar, 'right')
    p.title.text_font_size = "16pt"
    p.title.text_color = 'MidnightBlue'
    p.xaxis.axis_label_text_font_size = '15pt'
    p.yaxis.axis_label_text_font_size = '15pt'

    # export plots
    _ = export_png(p, filename=img_path / 'wordawards.png')
    #output_file(img_path/'file.html')

    #p.output_backend = "svg"
    #export_svgs(p, filename=img_path/"file.svg")

    #display plot
    show(p)
Example #28
0
 def viewCorr(self, nugget):
     # Read Nugget to DataFrame
     nfile = self.nuggetDataPath + nugget + '.pkl'
     df = pd.read_pickle(nfile)
     # Get info from Nugget filename
     info = self.nugInfo(nfile)
     # Attach theme to current document
     curdoc().theme = self.bokehTheme  # light_minimal dark_minimal
     # Now we will create correlation matrix using pandas
     df = df.iloc[:, 0:-6]
     df = df.corr()
     # Rename index and columns
     df.index.name = 'AllColumns1'
     df.columns.name = 'AllColumns2'
     # Prepare data.frame in the right format
     df = df.stack().rename("value").reset_index()
     # You can use your own palette here
     # colors = ['#d7191c', '#fdae61', '#ffffbf', '#a6d96a', '#1a9641']
     # I am using 'Viridis256' to map colors with value, change it with 'colors' if you need some specific colors
     mapper = LinearColorMapper(
         palette=Viridis256, low=df.value.min(), high=df.value.max())
     # Define a figure and tools
     TOOLS = "pan,wheel_zoom,box_zoom,reset"
     p = figure(
         tools=TOOLS,
         sizing_mode='scale_width',
         title="Correlation plot of " + info['symb'] + ' ' + info['timeframe'],
         x_range=list(df.AllColumns1.drop_duplicates()),
         y_range=list(df.AllColumns2.drop_duplicates()),
         toolbar_location="right",
         x_axis_location="below")
     # Create rectangle for heatmap
     p.rect(
         x="AllColumns1",
         y="AllColumns2",
         width=1,
         height=1,
         source=ColumnDataSource(df),
         line_color=None,
         fill_color=transform('value', mapper))
     # Add legend
     color_bar = ColorBar(
         color_mapper=mapper,
         location=(0, 0),
         ticker=BasicTicker(desired_num_ticks=10))
     p.xaxis.major_label_orientation = pi / 4
     p.add_layout(color_bar, 'right')
     return components(p)
Example #29
0
    def update(attr, old, new):
        boroughs_to_plot = [
            borough_selection.labels[i] for i in borough_selection.active
        ]
        if isinstance(date_range_slider.value[0], (int, float)):
            start_date = pd.Timestamp(float(date_range_slider.value[0]) * 1e6)
            end_date = pd.Timestamp(float(date_range_slider.value[1]) * 1e6)
        else:
            start_date = pd.Timestamp(date_range_slider.value[0])
            end_date = pd.Timestamp(date_range_slider.value[1])
        new_src = make_dataset(x, y, boroughs_to_plot, start_date, end_date)
        src.data.update(new_src.data)
        print('plotting new stuff')
        colors = [
            "#4f685f", '#64827c', "#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2",
            "#f9eed4", "#f9d1ac", "#ddb7b1", "#cc7878", "#a54c4c", "#933b41",
            "#550b1d"
        ]
        mapper = LinearColorMapper(palette='Magma256',
                                   low=src.data['value'].min(),
                                   high=src.data['value'].max())
        if x_ticks:
            x_range = x_ticks
        else:
            x_range = sorted(list(set(src.data['x'])))

        if y_ticks:
            y_range = y_ticks
        else:
            y_range = sorted(list(set(src.data['y'])))
        p = figure(plot_width=1100,
                   plot_height=700,
                   x_range=x_range,
                   y_range=y_range,
                   title=title)
        p.rect(x='x',
               y='y',
               width=1,
               height=1,
               source=src,
               line_color='white',
               fill_color=transform('value', mapper))
        color_bar = ColorBar(color_mapper=mapper,
                             location=(0, 0),
                             ticker=BasicTicker(desired_num_ticks=len(colors)))
        p.add_layout(color_bar, 'right')
        print('new stuff done')
Example #30
0
def get_moonplot(exposures,
                 width=250,
                 height=250,
                 min_border_left=50,
                 min_border_right=50):
    '''
    Generates a scatter plot of MOONFRAC vs MOONALT. Each point is then colored
    with a gradient corresponding to its MOONSEP.

    Args:
        exposures: Table of exposures with columns "MOONFRAC", "MOONALT", "MOONSEP"

    Options:
        width, height: plot width and height in pixels
        min_border_left, min_border_right: set minimum width for external labels in pixels

    Returns bokeh Figure object
    '''
    p = bk.figure(plot_width=width,
                  plot_height=height,
                  x_axis_label="Moon Fraction",
                  y_axis_label="Moon Altitude",
                  min_border_left=min_border_left,
                  min_border_right=min_border_right)

    keep = exposures['PROGRAM'] != 'CALIB'
    exposures_nocalib = exposures[keep]
    color_mapper = LinearColorMapper(palette="Magma256", low=0, high=180)

    source = ColumnDataSource(data=dict(MOONFRAC=exposures_nocalib["MOONFRAC"],
                                        MOONALT=exposures_nocalib["MOONALT"],
                                        MOONSEP=exposures_nocalib["MOONSEP"]))

    color_bar = ColorBar(color_mapper=color_mapper,
                         label_standoff=12,
                         location=(0, 0),
                         width=5)
    p.add_layout(color_bar, 'right')
    p.title.text = 'Moon Fraction vs Moon Altitude, colored with MOONSEP'

    p.circle("MOONFRAC",
             "MOONALT",
             color=transform('MOONSEP', color_mapper),
             alpha=0.5,
             source=source)
    return p
Example #31
0
cmap = LinearColorMapper(palette=Viridis11, low=min(unemployment.values()), high=max(unemployment.values()))

county_source = ColumnDataSource(
    data=dict(
        county_xs=[us_counties[code]["lons"] for code in us_counties if us_counties[code]["state"] not in ["ak", "hi", "pr", "gu", "vi", "mp", "as"]],
        county_ys=[us_counties[code]["lats"] for code in us_counties if us_counties[code]["state"] not in ["ak", "hi", "pr", "gu", "vi", "mp", "as"]],
        rate=[unemployment[code] for code in us_counties if us_counties[code]["state"] not in ["ak", "hi", "pr", "gu", "vi", "mp", "as"]],
    )
)

plot = Plot(min_border=0, border_fill_color="white", plot_width=1300, plot_height=700)
plot.title.text = "2009 Unemployment Data"
plot.toolbar_location = None

county_patches = Patches(xs="county_xs", ys="county_ys", fill_color=transform("rate", cmap), fill_alpha=0.7, line_color="white", line_width=0.5)
plot.add_glyph(county_source, county_patches)

state_patches = Patches(xs="state_xs", ys="state_ys", fill_alpha=0.0, line_color="#884444", line_width=2)
plot.add_glyph(state_source, state_patches)

cbar =  ColorBar(color_mapper=cmap, location=(0, 0))
plot.add_layout(cbar, 'left')

doc = Document()
doc.add_root(plot)

if __name__ == "__main__":
    doc.validate()
    filename = "choropleth.html"
    with open(filename, "w") as f:
Example #32
0
 def test_basic(object):
     t = bt.transform("foo", "junk")
     assert t == dict(field="foo", transform="junk")
Example #33
0
File: plots.py Project: jigold/hail
def _get_scatter_plot_elements(
        sp: Plot, source_pd: pd.DataFrame, x_col: str, y_col: str, label_cols: List[str],
        colors: Dict[str, ColorMapper] = None, size: int = 4,
) -> Tuple[bokeh.plotting.Figure, Dict[str, List[LegendItem]], Legend, ColorBar, Dict[str, ColorMapper], List[Renderer]] :

    if not source_pd.shape[0]:
        print("WARN: No data to plot.")
        return sp, None, None, None, None, None

    sp.tools.append(HoverTool(tooltips=[(x_col, f'@{x_col}'), (y_col, f'@{y_col}')] +
                                       [(c, f'@{c}') for c in source_pd.columns if c not in [x_col, y_col]]
                              ))

    cds = ColumnDataSource(source_pd)

    if not label_cols:
        sp.circle(x_col, y_col, source=cds, size=size)
        return sp, None, None, None, None, None

    continuous_cols = [col for col in label_cols if
                       (str(source_pd.dtypes[col]).startswith('float') or
                        str(source_pd.dtypes[col]).startswith('int'))]
    factor_cols = [col for col in label_cols if col not in continuous_cols]

    #  Assign color mappers to columns
    if colors is None:
        colors = {}
    color_mappers = {}

    for col in continuous_cols:
        low = np.nanmin(source_pd[col])
        if np.isnan(low):
            low = 0
            high = 0
        else:
            high = np.nanmax(source_pd[col])
        color_mappers[col] = colors[col] if col in colors else LinearColorMapper(palette='Magma256', low=low, high=high)

    for col in factor_cols:
        if col in colors:
            color_mappers[col] = colors[col]
        else:
            factors = list(set(source_pd[col]))
            color_mappers[col] = _get_categorical_palette(factors)

    # Create initial glyphs
    initial_col = label_cols[0]
    initial_mapper = color_mappers[initial_col]
    legend_items = {}

    if not factor_cols:
        all_renderers = [
            sp.circle(x_col, y_col, color=transform(initial_col, initial_mapper), source=cds, size=size)
        ]

    else:
        all_renderers = []
        legend_items = {col: DefaultDict(list) for col in factor_cols}
        for key in source_pd.groupby(factor_cols).groups.keys():
            key = key if len(factor_cols) > 1 else [key]
            cds_view = CDSView(source=cds, filters=[GroupFilter(column_name=factor_cols[i], group=key[i]) for i in range(0, len(factor_cols))])
            renderer = sp.circle(x_col, y_col, color=transform(initial_col, initial_mapper), source=cds, view=cds_view, size=size)
            all_renderers.append(renderer)
            for i in range(0, len(factor_cols)):
                legend_items[factor_cols[i]][key[i]].append(renderer)

        legend_items = {factor: [LegendItem(label=key, renderers=renderers) for key, renderers in key_renderers.items()] for factor, key_renderers in legend_items.items()}

    # Add legend / color bar
    legend = Legend(visible=False, click_policy='hide', orientation='vertical') if initial_col not in factor_cols else Legend(items=legend_items[initial_col], click_policy='hide', orientation='vertical')
    color_bar = ColorBar(visible=False) if initial_col not in continuous_cols else ColorBar(color_mapper=color_mappers[initial_col])
    sp.add_layout(legend, 'left')
    sp.add_layout(color_bar, 'left')

    return sp, legend_items, legend, color_bar, color_mappers, all_renderers
Example #34
0
def test_transform():
    t = bt.transform("foo", "junk")
    assert t == dict(field="foo", transform="junk")
plot.yaxis.axis_label = 'Normalized Price'

aapl_source = ColumnDataSource(data=dict(
    aapl_date=datetime(AAPL['date']),
    aapl_close=AAPL['adj_close'],
))

goog_source = ColumnDataSource(data=dict(
    goog_date=datetime(GOOG['date']),
    goog_close=GOOG['adj_close'],
))

v_func = """
    const first = xs[0]
    const norm = new Float64Array(xs.length)
    for (let i = 0; i < xs.length; i++) {
        norm[i] = xs[i] / first
    }
    return norm
"""
normalize = CustomJSTransform(v_func=v_func)

plot.line(x='aapl_date', y=transform('aapl_close', normalize), line_width=2,
        color='#cf3c4d', alpha=0.6,legend="Apple", source=aapl_source)
plot.line(x='goog_date', y=transform('goog_close', normalize), line_width=2,
        color='#2f7bce', alpha=0.6, legend="Google", source=goog_source)
plot.legend.location='top_left'

output_file("customjs_transform.html")
show(plot)
Example #36
0
gender_transform = CustomJSTransform(args=dict(source=ages), func="", v_func="""
    var val = new Float64Array(xs.length)
    for (var i = 0; i < xs.length; i++) {
        if (source.data['Sex'][i] == 'Male')
            val[i] = -xs[i]
        else
            val[i] = xs[i]
    }
    return val
""")

pyramid = figure(plot_width=600, plot_height=500, toolbar_location=None, y_range=groups,
                 title="Population Breakdown by Age Group and Gender",
                 x_axis_label="Population (Millions)",y_axis_label="Age Group")
pyramid.hbar(y="AgeGrp", height=1, right=transform('Value', gender_transform),
             source=ages, legend="Sex", line_color="white",
             fill_color=factor_cmap('Sex', palette=["#3B8686", "#CFF09E"], factors=["Male", "Female"]))

pyramid.ygrid.grid_line_color = None
pyramid.xaxis[0].formatter = FuncTickFormatter(code="""
    return (Math.abs(tick) / 1e6) + " M"
""")

# line plot of known and predicted population

known = ColumnDataSource(data=dict(x=[], y=[]))
predicted = ColumnDataSource(data=dict(x=[], y=[]))

population = figure(plot_width=600, plot_height=180, toolbar_location=None,
                    title="Total Population by Year",