5

I'm trying to visualize categorical spatial data using Datashader and Holoviews, similarly to https://anaconda.org/jbednar/census-hv-dask/notebook. However, when I try to assign different colors to categories, I always end up with same (presumably default) colors (An example of the output image.)

Here is the code I'm running in Jupyter notebook. Could anyone advise me on how to make the custom color map work? Or at least run the code to see if you end up with colors matching the legend or not. Thank you!

from sklearn.datasets.samples_generator import make_blobs
from matplotlib import pyplot
import pandas as pd

import holoviews as hv
import geoviews as gv
import datashader as ds
from cartopy import crs
from matplotlib.cm import get_cmap
from holoviews.operation.datashader import datashade, aggregate
hv.notebook_extension('bokeh', width=95)

# Generating blob data:
X, y = make_blobs(n_samples=5000000, centers=5, n_features=2)
df = pd.DataFrame(dict(x=X[:,0], y=X[:,1], label=y))

# Plotting the blobs using datashader and holoviews:
%opts Overlay [width=800 height=455 xaxis=None yaxis=None show_grid=False] 
%opts Shape (fill_color=None line_width=1.5) [apply_ranges=False] 
%opts Points [apply_ranges=False] WMTS (alpha=0.5) NdOverlay [tools=['tap']]

color_key = {0:'red', 1:'blue', 2:'green', 3:'yellow', 4:'black'}
labels    = {0:'red', 1:'blue', 2:'green', 3:'yellow', 4:'black'}

color_points = hv.NdOverlay({labels[k]: gv.Points([0,0], crs=crs.PlateCarree(),
                            label=labels[k])(style=dict(color=v))
                            for k, v in color_key.items()})

dataset = gv.Dataset(df, kdims=['x', 'y'], vdims=['label'])
shaded = datashade(hv.Points(dataset), cmap=color_key, aggregator=ds.count_cat('label'))

shaded * color_points
Runkles
  • 113
  • 8

1 Answers1

0

That code doesn't seem to be runnable (races is not defined, and gv is not imported), but in any case, categorical colors are determined by the color_key argument, not cmap, so you'd need to change cmap=color_key to color_key=color_key.

James A. Bednar
  • 3,195
  • 1
  • 9
  • 13
  • Thank you James, that solved my problem, I also edited the code to be runnable. I have just one more question: in your example notebook (link above in my original question) you are also using the *cmap* argument instead of *color_key* argument (when defining "shaded"), but the colors are right. How is this different to my case? – Runkles Jan 03 '19 at 18:29
  • Ah, that's the real problem here! That link points to an old copy of the notebook that's been sitting around from before we had a way to show notebooks in the actual web site. There used to be only one way to set colormaps, but we split the color key and cmap options because categorical plots don't work well with the default cmap colormaps, but that copy of the notebook hasn't been updated. Plus we're about to move the examples into pyviz.org so that we can actually manage to maintain them. Sorry for the confusion, and I guess I should at least upload an updated nb there. – James A. Bednar Jan 04 '19 at 03:55
  • Right, I will be using the notebooks in the actual web site for the reference from now on. Thanks again! – Runkles Jan 04 '19 at 09:19
  • Turns out that census_hv_dask was a very old copy of the "gerrymandering" notebook now available at datashader.org, so I've deleted it to avoid confusion. – James A. Bednar Jan 16 '19 at 15:23