Holoviews 如何知道将哪些颜色分配给叠加层中的每个散点图?

How does Holoviews know which colours to assign to each scatterplot in an overlay?

在 bokeh Holoviews 画廊中,有一个名为 'Scatter economic' 的示例。

http://holoviews.org/gallery/demos/bokeh/scatter_economic.html#bokeh-gallery-scatter-economic

在此图中,请注意 Scatter 的选项之一是 (color=Cycle('Category20'))。该图的最后一行是 gdp_unem_scatter.overlay('Country')

我想确保我正确地绘制了我想要的东西。

谢谢!

How does Holoviews know to connect each Scatter to a particular color in Cycle('Category20')? Is this just a property of Cycle()? Is there some way that the Overlay interacts with the Scatter and with the Cycle automatically?

您说得对,CycleOverlay 旨在自动以这种方式进行交互。更明确地说,Cycle 中的每种颜色都分配给叠加层的 'layer',直到循环用完所有颜色并循环。

For example, if I use the .opts method with this cycle color on the Scatter (i.e., second to the last line in the above example), and then do an .overlay('Country'), somehow Holoviews knows to assign each Scatter to a particular color based on the Country.

这是因为在调用 overlay 方法之前,您对 opts 的调用自定义了数据结构元素的选项(此数据结构是 HoloMap)。在那里设置的选项传播到 HoloMap 中的 Scatter 元素,现在将指定所选的 Cycle。这意味着当这些元素被放入叠加层时,HoloViews 可以适当地查找 Cycle 并将其正确应用到叠加层。

希望这是有道理的!

现在可以通过所谓的 dim 表达式映射 NdOverlay 中的类别(如上例中所用),然后定义一个表达式来进行映射:

dim_expr = hv.dim('category').categorize({'A': 'red', 'B': 'green', 'C': 'blue'})
overlay = hv.NdOverlay({chr(65+i): hv.Scatter(np.random.rand(10, 2)) for i in range(3)}, 'category')
overlay.opts(hv.opts.Scatter(color=dim_expr))

在此示例中,我们创建了一个指向 'category' 维度的 dim 表达式,然后映射每个类别('A'、'B' 和 'C' ) 到颜色 ('red'、'green'、'blue')。然后我们将其分配给颜色选项。