- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的目标是在 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)
有输出
最佳答案
好吧,我似乎已经成功了,所以这就是我想出的。关键是创建一个派生自 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)
产生这张图片的:
这与 OP 图像有点不同,但事实证明这只是由于我提到的数据着色器错误。修复该错误并重新运行 OP 代码我得到以下输出:
匹配得很好。看起来 Holoviews 只是切断了所选“跨度”之外的数据或类似的数据,这对我当前的需求来说很好。
关于python - 在 HoloViews 中为 Datashader 着色点和选择聚合方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44291654/
我有一个表格数据集,它有多个列,可能是某些绘图的关键维度。 ds = hv.Dataset(data_df, kdims=['time', 'forecasttime', 'group'], vdim
在 bokeh Holoviews 画廊中,有一个名为“Scatter economic”的示例。 http://holoviews.org/gallery/demos/bokeh/scatter_e
我想将全息图保存为 SVG,但是我收到一条我不明白的错误消息: np.random.seed(9) data = np.random.rand(10, 2) points =
我正在使用 Holoviews 构建图表仪表板。其中一些图表在 y 轴上有百分比,而其他图表有总和/计数等。当我尝试将我创建的所有图表输出到 html 文件时,所有图表都会更改它们的 y 轴以匹配第一
我正在尝试使用 Holoviews 绘制热图。我做了几次尝试但没有成功。我绘制的数据有 2 个值(一个在“x”轴上,一个在“y”轴上),我希望每种颜色代表数量(就像热图中的直方图)。 最佳答案 在 H
我在 Bokeh 之上使用全息 View 创建了一个热图 import numpy as np import pandas as pd import holoviews as hv from holo
这是代码: import pandas as pd import holoviews as hv hv.extension('bokeh','matplotlib','plotly') mydf =
我这里有一个非常简单的例子: plot = [] for d in range(2): name=str(d) data = pd.DataFrame({'x':[2,5,1], 'y
我希望曲线的用户能够从 slider (斜率)调整曲线,但我希望斜率的值返回一组不同的值。 这样,如果用户选择 1,曲线将使用值 0.5,如果用户选择 5,曲线将使用值 0.8。 我尝试使用 lamb
我一直在尝试了解如何完成绘制两个数据集(每个数据集具有不同颜色)这一非常简单的任务,但我在网上发现的任何内容似乎都无法做到这一点。这是一些示例代码: import pandas as pd impor
我目前正在拼命尝试在网格状布局中对齐三个不同大小的图像。 本质上,我想插入三个不同形状的光栅图像。我希望能够独立定义每个栅格的边界和纵横比,使它们不会在一维上拉伸(stretch)。但是,我找不到这样
我正在尝试更改数据着色器使用的颜色图。 我试过这个: datashade(scatter, cmap='Reds') 其中 scatter 是一个 hv.Scatter 元素。这是行不通的,因为数据着
由于我处理的数据量很大,我使用 Datashader 制作不同参数的交叉图。我的想法是启用某种交互,允许用户直接从图中选择,基于数据趋势的异常值显示了这些。我想知道 Holoviews Selecti
我看到有关如何在全息 View 中编辑单个对象(如图像或直方图)的范围(又名轴限制)的示例 such as this answer on stack exchange for histogram ex
我正在尝试在 Holoviews 中为每个点生成一个具有自定义颜色值的热图,以便指定不同的 alpha 颜色值。 目前我正在生成两个具有不同 alpha 值的 Holoviews 图并像这样叠加它们:
我有一个带有两个静态 HLine 的条形图,我想为它们添加标签(或图例),以便在图上定义它们。我试过类似的东西: eq = ( sr2.hvplot( kind="bar",
我正在学习带有 Bokeh 后端的 HoloViews,并且正在制作 Pandas 数据帧所选列的 iPython/Jupyter 显示(实际上它是一个 xarray 数据集,但用数据帧显示问题更简单
有没有办法在 Holoviews 中将绘图旋转 90°? 我有兴趣知道一般如何做到这一点,但我的直接目的是生成一个直方图,该直方图被旋转以将频率放在水平方向上,将关键值放在垂直方向上,以布局在散点旁边
是否有某种方法可以设置在 HoloViews 中组成的各个绘图的范围或边界?我试图使用下面的代码同时显示图像和线图。如果我如下所述设置图像的边界,我似乎无法修改线图的范围;后者以轴 x = (0, 1
我是 Holoviews/bokeh 的新手,我对如何构建图表有了大致的了解,但我仍然迷失在一些细微差别中,并且发现文档中的示例非常有限。 有了分类数据的时间序列,我尝试呈现多个堆叠条形图,这些条形图
我是一名优秀的程序员,十分优秀!