问题
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