Holoviews change datashader colormap

ぃ、小莉子 提交于 2021-01-27 07:26:32

问题


I'm trying to change the colormap used by datashader.

I tried this:

datashade(scatter, cmap='Reds')

Where scatter is an hv.Scatter element. This doesn't work because datashader expects an iterable or a function that returns colors. As such, this works:

datashade(scatter, cmap=['blue'])

So how can I take the 'Reds' colormap and convert it into something datashader can use?

Thank you.


回答1:


Right; you can't pass the string name of a colormap to Datashader's cmap argument, because Datashader interprets a single string as the name of a single color, constructing a colormap from it by setting the R,G,B channels to that color and then varying the alpha channel. If you want a colormap, either pass a list of colors (as used by Bokeh for its palettes) or a Matplotlib colormap object (not the string name) to cmap:

from matplotlib import cm
datashade(scatter, cmap=cm.Reds)



回答2:


To choose from any colormap available to Holoviews, use the following code:

from holoviews.plotting.util import process_cmap

datashade(scatter, cmap=process_cmap("Magma", provider="bokeh"))

Change "Magma" and "Bokeh" to any of the supported colormaps (Thanks to @Chris for the link).


Request: It would be nice if opts parameters given to Holoviews plots were automatically transferred if datashade supports them, as requested by this GitHub issue: https://github.com/holoviz/holoviews/issues/4125.




回答3:


You can also use library hvplot which is built on top of HoloViews to create your plots, use datashader and change the color mapping, all in a convenient way:

import numpy as np
import pandas as pd
import hvplot.pandas

df = pd.DataFrame({
    'x': np.random.normal(size=100000),
    'y': np.random.normal(size=100000),
})

# use keyword datashade=True to turn on datashading
# use keyword cmap to change the default colormap
df.hvplot.scatter(
    x='x', 
    y='y', 
    datashade=True, 
    cmap='Magma',
)


Colormaps can be found here:
http://holoviews.org/user_guide/Colormaps.html

Resulting plot:



来源:https://stackoverflow.com/questions/56527054/holoviews-change-datashader-colormap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!