4

我正在尝试使用 Datashader 和 Holoviews 可视化分类空间数据,类似于https://anaconda.org/jbednar/census-hv-dask/notebook。但是,当我尝试为类别分配不同的颜色时,我总是以相同的(可能是默认的)颜色结束(输出图像的示例。)

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

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
4

1 回答 1

0

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

于 2019-01-03T17:11:58.227 回答