gpt4 book ai didi

python - 在 HoloViews 中为 Datashader 着色点和选择聚合方法

转载 作者:太空宇宙 更新时间:2023-11-03 15:54:38 25 4
gpt4 key购买 nike

我的目标是在 HoloViews 中创建 x、y、z 散点图,其中这些图是使用 Datashader 生成的,通过最小化“z”来聚合点,并根据“z”对点进行着色。最终这是为了做一些事情,比如制作概况似然图。

我在使用 HoloViews + Datashader 生成图时取得了很好的进展,甚至以很酷的方式链接图(参见例如 How to do linked data selections in HoloViews with Datashader + Bokeh backend ),但是我不知道如何控制点颜色和聚合方法。

下面是一些代码(在 Jupyter notebook 中运行),它们(几乎)完成了我在“普通 Vanilla ”Datashader + Bokeh 中想要的功能。如何通过 HoloViews 实现相同的目的,以便我可以利用该包中的优秀功能?

请特别注意,我希望将颜色分配给特定的 z 值,我不希望它自动归一化或任何类似的事情。我试图在下面的代码中通过在 'shade' 函数中设置 'span' 参数来实现这一点,尽管它不太有效,因为当我放大绘图时,我看到出现了新的绿色区域,这表明颜色的绝对标准化不会保持不变。无论如何,它应该足够接近以说明我所追求的。

import pandas as pd
from bokeh.plotting import figure, output_notebook
import datashader as ds
from datashader.bokeh_ext import InteractiveImage
from datashader import transfer_functions as tf

output_notebook(hide_banner=True)

import matplotlib.colors as colors

#Define colormap
mn=0
mx=5
s0=0./(mx-mn)
s1=1./(mx-mn)
s2=2./(mx-mn)
s3=3./(mx-mn)
s4=4./(mx-mn)
s5=5./(mx-mn)

cdict = {
'red' : ((s0, 0., 0.), (s1, 1., 1.), (s2, 1., 1.), (s3, 1., 1.), (s4, .5, .5), (s5, .2, .2)),
'green': ((s0, 1., 1.), (s1, 1., 1.), (s2, .5, .5), (s3, 0., 0.), (s4, 0., 0.), (s5, 0., 0.)),
'blue' : ((s0, 0., 0.), (s1, 0., 0.), (s2, 0., 0.), (s3, 0., 0.), (s4, 0., 0.), (s5, 0., 0.))
}

chi2cmap = colors.LinearSegmentedColormap('chi2_colormap', cdict, 1024)
chi2cmap.set_bad('w',1.)


# Create some data to plot
x = np.arange(0,10,1e-2)
y = np.arange(0,10,1e-2)
X,Y = np.meshgrid(x,y)
x = X.flatten()
y = Y.flatten()
z = 5 * np.sin(x) * np.cos(y)

#------ Create pandas dataframe object from the data ------
print "Creating Pandas dataframe object"
df = pd.DataFrame.from_dict({"x": x, "y": y, "z": z})

# Create callback function for bokeh
def create_image(x_range, y_range, w, h):
cvs = ds.Canvas(x_range=x_range, y_range=y_range, plot_width=200, plot_height=200)
agg = cvs.points(df, 'x', 'y', ds.min('z'))
img = tf.shade(agg, cmap=chi2cmap, how='linear', span=[mn,mx])
#return tf.dynspread(img, threshold=0.9, max_px=10)
return img

# Export image
#ds.utils.export_image(img, "test", fmt=".png", export_path=".", background="white")

# Interactive image via bokeh
p = figure(tools='pan,wheel_zoom,box_zoom,reset', background_fill_color="white",
plot_width=500, plot_height=500, x_range=(np.min(x),np.max(x)), y_range=(np.min(y),np.max(y)))

p.axis.visible = False
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None
InteractiveImage(p, create_image)

有输出

enter image description here

最佳答案

好吧,我似乎已经成功了,所以这就是我想出的。关键是创建一个派生自 holoviews.operation.datashader.datashade 的新类,并更改其中的聚合器和 cmap 数据成员:

class chi2_datashade(hvds.datashade):
"""Custom datashade class to do our projection and colormap"""
aggregator = ds.min('z')
cmap = chi2cmap
normalization = 'linear'
span = [mn,mx] # this requires https://github.com/ioam/holoviews/pull/1508 to work, which I just hacked in to holoviews for now

然后像使用原始数据阴影类一样使用它:

data = hv.Points(df)
chi2_datashade(data)

span 数据成员存在问题,因为它不存在,因此没有连接到底层数据着色器选项,但它将在即将发布的版本中修复,并且如果您想自己做,可以在源代码中轻松更改(参见 https://github.com/ioam/holoviews/pull/1508)

实际上还有另一个问题,这次来自数据着色器,因为它根据内部最小值偏移“z”数据,因此搞砸了“span”参数的含义。我向他们提出了这个问题,但如果你想自己做的话,它也是一个相当简单的源代码修复(参见 https://github.com/bokeh/datashader/issues/368 )

这是完整的例子:

import numpy as np
import pandas as pd
import datashader as ds
import holoviews as hv
import holoviews.operation.datashader as hvds
import matplotlib.colors as colors
hv.notebook_extension('bokeh')

#Define colormap
mn=0
mx=5
s0=0./(mx-mn)
s1=1./(mx-mn)
s2=2./(mx-mn)
s3=3./(mx-mn)
s4=4./(mx-mn)
s5=5./(mx-mn)

cdict = {
'red' : ((s0, 0., 0.), (s1, 1., 1.), (s2, 1., 1.), (s3, 1., 1.), (s4, .5, .5), (s5, .2, .2)),
'green': ((s0, 1., 1.), (s1, 1., 1.), (s2, .5, .5), (s3, 0., 0.), (s4, 0., 0.), (s5, 0., 0.)),
'blue' : ((s0, 0., 0.), (s1, 0., 0.), (s2, 0., 0.), (s3, 0., 0.), (s4, 0., 0.), (s5, 0., 0.))
}

chi2cmap = colors.LinearSegmentedColormap('chi2_colormap', cdict, 1024)
chi2cmap.set_bad('w',1.)


# Create some data to plot
x = np.arange(0,10,1e-2)
y = np.arange(0,10,1e-2)
X,Y = np.meshgrid(x,y)
x = X.flatten()
y = Y.flatten()
z = 5 * np.sin(x) * np.cos(y)

#------ Create pandas dataframe object from the data ------
print "Creating Pandas dataframe object"
df = pd.DataFrame.from_dict({"x": x, "y": y, "z": z})

class chi2_datashade(hvds.datashade):
"""Custom datashade class to do our projection and colormap"""
aggregator = ds.min('z')
cmap = chi2cmap
normalization = 'linear'
span = [mn,mx] # this requires https://github.com/ioam/holoviews/pull/1508 to work, which I just hacked in to holoviews for now


data = hv.Points(df)
chi2_datashade(data)

产生这张图片的:

enter image description here

这与 OP 图像有点不同,但事实证明这只是由于我提到的数据着色器错误。修复该错误并重新运行 OP 代码我得到以下输出:

enter image description here

匹配得很好。看起来 Holoviews 只是切断了所选“跨度”之外的数据或类似的数据,这对我当前的需求来说很好。

关于python - 在 HoloViews 中为 Datashader 着色点和选择聚合方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44291654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com