[英]How can I color nodes of a graph, with datashader?
I want to visualize a graph with datashader (I have a lot of nodes), as in我想用数据着色器可视化一个图形(我有很多节点),如
import holoviews as hv
import pandas as pd
import dask.dataframe as dd
from holoviews.operation.datashader import (
datashade, aggregate, dynspread,
bundle_graph, split_dataframe, regrid
)
from holoviews.element.graphs import layout_nodes
from datashader.layout import random_layout
hv.extension('bokeh')
sources = [3, 1, 2, 3, 4]
targets = [5, 5, 5, 5, 5]
df = pd.DataFrame({'source': sources, 'target': targets})
edges_df = dd.from_pandas(df, npartitions=3)
graph = layout_nodes(hv.Graph(edges_df), layout=random_layout)
pad = dict(x=(-.5, 1.3), y=(-.5, 1.3))
datashade(graph, width=800, height=800) * graph.nodes.redim.range(**pad)
This works, but as my graph is bipartite , I would like to color the sources
and targets
nodes with different colors, eg using a color palette like:这是有效的,但由于我的图形是二分的,我想用不同的颜色为sources
节点和targets
节点着色,例如使用如下调色板:
my_colors_dict = {5: 'red', 3: 'blue', 1: 'blue', 2: 'blue', 4: 'blue'}
(ie all nodes blue but my single node "5" in my targets
) (即所有节点都是蓝色,但我的targets
单个节点“5”)
How can I achieve this?我怎样才能做到这一点? I am not very familiar with the library yet and could come up with clumsy attempts only so far..我对图书馆还不是很熟悉,到目前为止只能提出笨拙的尝试..
You should be able to do this by assigning a category to each node and then mapping that column to a color as described in http://holoviews.org/user_guide/Style_Mapping.html .您应该能够通过为每个节点分配一个类别,然后将该列映射到一种颜色来做到这一点,如http://holoviews.org/user_guide/Style_Mapping.html 中所述。 But if you don't want to change your data structures and don't mind being a bit hackish, you can always do it by overlaying a recolored subset of the nodes:但是如果你不想改变你的数据结构并且不介意有点hackish,你总是可以通过覆盖节点的重新着色的子集来做到这一点:
targets = graph.nodes.clone()
targets.data = graph.nodes.data[4:]
datashade(graph, width=800, height=800) * graph.nodes.redim.range(**pad) * targets.opts(color='red')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.