首页 > 解决方案 > 基于日期时间的 Holoviews 颜色和颜色条

问题描述

我是全息视图和散景的新手,我正在尝试根据颜色基于日期的时间序列创建散点图。类似于此页面上的第三个代码单元:https ://docs.pymc.io/notebooks/GLM-rolling-regression.html

有谁知道该怎么做?

Ps:我需要使用带有散景后端的全息视图。

例子:
基于日期时间的holoviews颜色条

标签: pythontime-seriesbokehscatter-plotholoviews

解决方案


我通过使用参数用日期覆盖颜色条的刻度标签来解决这个问题:

 colorbar_opts={
     'major_label_overrides'={}
 }

这是一个工作示例(简化):

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

# create sample data
X = np.random.normal(size=(2, 1000))

df = pd.DataFrame(
    data={
        'col1': X[0],
        'col2': X[1],
        # use 'date' to overwirte the ticklabels of the colorbar
        'date': pd.date_range(start='2017-01-01', freq='D', periods=1000),
    }
)

# use 'time_color' for the colorbar, since the colors need to be a float or int
df['time_color'] = df.index.to_series()


# draw scatter plot
# using the 'time_color' column to color the markers
# and overwrite the tick label using the date column

cbar_opts = dict(
                major_label_overrides  = df['date'].dt.strftime('%Y-%m-%d').to_dict(),
                major_label_text_align = 'left',
                )

hv_coloured = df.hvplot.points(x='col1', y='col2', c='time_color'
                              ).opts(colorbar_opts=cbar_opts, cmap='viridis')

hv_coloured 

hv_colored 示例


如果您执行 `hv.help(hv.Points)`,您可以获得有关颜色条周围所有可用选项的更多说明:

colorbar:
是否显示颜色条。

colorbar_opts:
允许为颜色条设置特定的样式选项,覆盖在 colorbar_specs 类属性中定义的选项。包括位置、方向、高度、宽度、scale_alpha、title、title_props、margin、padding、background_fill_color 等等。

colorbar_position:允许在多个预定义的颜色条位置选项之间进行选择。可以在 colorbar_specs 类属性中自定义预定义选项。


这个问题为我指明了正确的方向:

如何为 HoloViews 中的点图手动设置颜色条的刻度位置?


推荐阅读