def plot_landscape(data): X, x, y, xlim, ylim = data mesh = holoviews.Bivariate(X) scatter = holoviews.Scatter((x, y)) contour_mesh = mesh * scatter return contour_mesh.redim( x=holoviews.Dimension("x", range=xlim), y=holoviews.Dimension("y", range=ylim), )
def plot_landscape(data): """Plot the walkers distribution overlaying a histogram on a bivariate plot.""" X, x, y, xlim, ylim = data mesh = holoviews.Bivariate(X) scatter = holoviews.Scatter((x, y)) contour_mesh = mesh * scatter return contour_mesh.redim( x=holoviews.Dimension("x", range=xlim), y=holoviews.Dimension("y", range=ylim), )
def bivariate(data): return holoviews.Bivariate(data, *args, **kwargs)
def _scatter(adata, x, y, condition, by=None, subsample='datashade', steps=40, keep_frac=0.2, seed=None, legend_loc='top_right', size=4, xlabel=None, ylabel=None, title=None, use_raw=True, hover=None, hover_width=10, hover_height=10, kde=None, density=None, density_size=150, jitter=None, perc=None, xlim=None, ylim=None, cmap=None, show_legend=True, plot_height=400, plot_width=400): _sentinel = object() def create_density_plots(df, density, kdims, cmap): cm = {} if density == 'all': dfs = {_sentinel: df} elif density == 'group': if 'z' not in df.columns: warnings.warn( f'`density=\'groups\' was specified, but no group found. Did you specify `color=...`?' ) dfs = {_sentinel: df} elif not is_categorical(df['z']): warnings.warn( f'`density=\'groups\' was specified, but column `{condition}` is not categorical.' ) dfs = {_sentinel: df} else: dfs = {k: v for k, v in df.groupby('z')} cm = cmap else: raise ValueError( f'Invalid `density` type: \'`{density}`\'. Possible values are `\'all\'`, `\'group\'`.' ) # assumes x, y order in kdims return [ hv.Overlay([ hv.Distribution(df, kdims=dim).opts(color=cm.get(k, 'black'), framewise=True) for k, df in dfs.items() ]) for dim in kdims ] assert keep_frac >= 0 and keep_frac <= 1, f'`keep_perc` must be in interval `[0, 1]`, got `{keep_frac}`.' adata_mraw = get_mraw(adata, use_raw) if subsample == 'uniform': cb_kwargs = {'steps': steps} elif subsample == 'density': cb_kwargs = {'size': int(keep_frac * adata.n_obs), 'seed': seed} else: cb_kwargs = {} categorical = False if condition is None: cmap = ['black'] * len(x) if subsample == 'datashade' else 'black' elif is_categorical(condition): categorical = True cmap = Sets1to3 if cmap is None else cmap cmap = odict( zip(condition.cat.categories, adata.uns.get(f'{by}_colors', cmap))) else: cmap = Viridis256 if cmap is None else cmap jitter_x, jitter_y = None, None if isinstance(jitter, (tuple, list)): assert len( jitter ) == 2, f'`jitter` must be of length `2`, found `{len(jitter)}`.' jitter_x, jitter_y = jitter elif jitter is not None: jitter_x, jitter_y = jitter, jitter if jitter_x is not None: x += np.random.normal(0, jitter_x, size=x.shape) if jitter_y is not None: y += np.random.normal(0, jitter_y, size=y.shape) data = {'x': x, 'y': y, 'z': condition} vdims = ['z'] hovertool = None if hover is not None: for k, dt in hover.items(): vdims.append(k) data[k] = dt hovertool = HoverTool( tooltips=[(key.capitalize(), f'@{key}') for key in ( ['index'] if subsample == 'datashade' else hover.keys())]) data = pd.DataFrame(data) if categorical: data['z'] = data['z'].astype('category') if not vdims: vdims = None if xlim is None: xlim = pad(*minmax(x)) if ylim is None: ylim = pad(*minmax(y)) kdims = [('x', 'x' if xlabel is None else xlabel), ('y', 'y' if ylabel is None else ylabel)] scatter = hv.Scatter(data, kdims=kdims, vdims=vdims).sort('z') scatter = scatter.opts(size=size, xlim=xlim, ylim=ylim) kde_plot= None if kde is None else \ hv.Bivariate(scatter).opts(bandwidth=kde, show_legend=False, line_width=2) xdist, ydist = (None, None) if density is None else create_density_plots( data, density, kdims, cmap) if categorical: scatter = scatter.opts(cmap=cmap, color='z', show_legend=show_legend, legend_position=legend_loc) elif 'z' in data: scatter = scatter.opts(cmap=cmap, color='z', clim=tuple(map(float, minmax(data['z'], perc))), colorbar=True, colorbar_opts={'width': 20}) else: scatter = scatter.opts(color='black') legend = None if subsample == 'datashade': subsampled = dynspread(datashade( scatter, aggregator=(ds.count_cat('z') if categorical else ds.mean('z')) if vdims is not None else None, color_key=cmap, cmap=cmap, streams=[hv.streams.RangeXY(transient=True), hv.streams.PlotSize], min_alpha=255).opts(axiswise=True, framewise=True), threshold=0.8, max_px=5) if show_legend and categorical: legend = hv.NdOverlay({ k: hv.Points([0, 0], label=str(k)).opts(size=0, color=v) for k, v in cmap.items() }) if hover is not None: t = hv.util.Dynamic(rasterize(scatter, width=hover_width, height=hover_height, streams=[hv.streams.RangeXY], aggregator=ds.reductions.min('index')), operation=hv.QuadMesh)\ .opts(tools=[hovertool], axiswise=True, framewise=True, alpha=0, hover_alpha=0.25, height=plot_height, width=plot_width) scatter = t * subsampled else: scatter = subsampled elif subsample == 'decimate': scatter = decimate(scatter, max_samples=int(adata.n_obs * keep_frac), streams=[hv.streams.RangeXY(transient=True)], random_seed=seed) if legend is not None: scatter = (scatter * legend).opts(legend_position=legend_loc) if kde_plot is not None: scatter *= kde_plot scatter = scatter.opts(height=plot_height, width=plot_width) scatter = scatter.opts(hv.opts.Scatter( tools=[hovertool])) if hovertool is not None else scatter if xdist is not None and ydist is not None: scatter = (scatter << ydist.opts(width=density_size)) << xdist.opts( height=density_size) return scatter.opts(title=title if title is not None else '')
sns.plt.xlim(-122.425, -122.250) sns.plt.ylim(47.5, 47.8) sns.scatterplot(x='X', y='Y', hue='SEVERITYCODE', data=data[np.logical_not(data.SEVERITYCODE.isin(['0', '1']))]) severity1 = data[data.SEVERITYCODE.isin(['1','2','2b','3'])] severity2 = data[data.SEVERITYCODE.isin(['2','2b','3'])] severity3 = data[data.SEVERITYCODE.isin(['3'])] sns.kdeplot(data=severity3.X, data2=severity3.Y, cmap='Reds', shade=True, bw=0.15) import holoviews as hv from holoviews import opts, Cycle hv.extension('bokeh') hv.Bivariate(severity3[['X','Y']]) normal = np.random.randn(100,2) sns.jointplot("X", "Y", data=data, kind="kde") ########## # GEOPLOTTING ######### shapefile = geopandas.read_file(fp) def add_basemap(ax, zoom, url='http://tile.stamen.com/terrain/tileZ/tileX/tileY.png'): xmin, xmax, ymin, ymax = ax.axis() basemap, extent = ctx.bounds2img(xmin, ymin, xmax, ymax, zoom=zoom, url=url) ax.imshow(basemap, extent=extent, interpolation='bilinear')