在Holoviews / Datashader中更改分类数据的颜色图

时间:2019-01-03 16:50:18

标签: python colormap holoviews datashader

我正在尝试使用Datashader和Holoviews可视化分类空间数据,类似于https://anaconda.org/jbednar/census-hv-dask/notebook。但是,当我尝试为类别分配不同的颜色时,我总是得到相同的(大概是默认的)颜色(An example of the output image。)

这是我在Jupyter笔记本中运行的代码。谁能建议我如何使自定义颜色图起作用?或者至少运行代码以查看最终是否获得与图例匹配的颜色。谢谢!

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

1 个答案:

答案 0 :(得分:0)

该代码似乎不可运行(未定义种族,并且未导入gv),但是无论如何,分类颜色是由color_key参数而不是cmap确定的,因此您需要将cmap=color_key更改为color_key=color_key