Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import pandas as pd
- import seaborn as sns
- from bokeh import client as bkclient
- from bokeh import io as bkio
- from bokeh import plotting as bkplot
- from bokeh import models as bkmodels
- HEAT_DATA = pd.DataFrame(data=np.random.uniform(-3, 3, (100, 100)),
- index=['R{}'.format(x) for x in range(100)],
- columns=['C{}'.format(x) for x in range(100)])
- SCATTER_DATA = pd.DataFrame(data=np.random.normal(-1, 1, (100, 2)),
- index=['R{}'.format(x) for x in range(100)],
- columns=list('xy'))
- def convert_to_colors(values, resolution=50):
- rgb = np.asarray(sns.color_palette('RdBu', n_colors=resolution)) * 255
- hex = np.asarray(['#%02x%02x%02x' % tuple(x) for x in rgb.astype(int)])[::-1]
- n_range = np.linspace(-3, 3, resolution)
- value_color_map = pd.DataFrame(dict({'value': n_range, 'color': hex}))
- idxs = np.apply_along_axis(lambda o: np.searchsorted(value_color_map['value'], o), 0, values)
- idxs[idxs > (value_color_map.shape[0] - 1)] -= 1
- colors = np.apply_along_axis(lambda o: value_color_map['color'][o], 0, idxs)
- return colors
- def refresh_heatmap(selections):
- print('\n\n\n\n\n')
- print(selections)
- print('\n\n\n\n\n')
- frame = HEAT_DATA.loc[selections, :]
- frame_unstacked = frame.unstack().to_frame('values')
- frame_unstacked = frame_unstacked.reset_index()
- frame_unstacked['color'] = convert_to_colors(frame_unstacked['values'])
- # Note: These calls are referencing global object (defined below).
- heat_source.data = bkmodels.ColumnDataSource(frame_unstacked).data
- heat_figure.y_range.factors = selections
- return
- def update_selected(attr, old, new):
- idxs = np.asarray(new['1d']['indices'])
- if len(idxs) > 0:
- selected = SCATTER_DATA.index[idxs].tolist()
- refresh_heatmap(selected)
- return
- # Scatter Plot
- scatter_source = bkmodels.ColumnDataSource(SCATTER_DATA)
- scatter_figure = bkplot.figure(title='Scatter', tools='pan,box_zoom,box_select,reset')
- scatter_plot = scatter_figure.scatter('x', 'y', source=scatter_source, alpha=0.6)
- scatter_figure.select(bkmodels.BoxSelectTool).select_every_mousemove = False
- scatter_plot.data_source.on_change('selected', update_selected)
- # Heat Map
- HEAT_DATA.index.name = 'Rows'
- HEAT_DATA.columns.name = 'Cols'
- heat_unstacked = HEAT_DATA.unstack().to_frame('values')
- heat_unstacked = heat_unstacked.reset_index()
- heat_unstacked['color'] = convert_to_colors(heat_unstacked['values'])
- heat_source = bkmodels.ColumnDataSource(heat_unstacked)
- hover = bkmodels.HoverTool(tooltips=[('Upper', '@Rows'), ('Lower', '@Cols'), ('Value', '@values')])
- heat_figure = bkplot.figure(title='Heat Map', tools=[hover], x_range=bkmodels.FactorRange(factors=[]),
- y_range=bkmodels.FactorRange(factors=[]))
- heat_figure.x_range.factors = HEAT_DATA.columns.tolist()
- heat_figure.y_range.factors = HEAT_DATA.index.tolist()
- heat_figure.xaxis.visible = None
- heat_figure.yaxis.visible = None
- heat_plot = heat_figure.rect('Cols', 'Rows', 1, 1, source=heat_source, color='color', line_color=None)
- # Render Page
- LAYOUT = bkplot.vplot(scatter_figure, heat_figure)
- bkplot.show(LAYOUT)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement