如何使用holoviews库绘制具有不同大小的顶点和边的图形?

时间:2017-12-15 18:55:30

标签: python graph ipython holoviews

如何使用不同大小的顶点和边绘制图形? 我正在使用ipython和holoviews库。

例如,

边缘输入

start,ends
1,4
2,4
3,5
3,6
2,6

节点输入

x,y,index,type
0.0,1.0,1,a
0.5,1.0,2,a
1.0,1.0,3,a
0.4,0.0,4,b
0.5,0.0,5,b
0.6,0.0,6,b

main.py

import numpy as np
import pandas as pd
import holoviews as hv
import networkx as nx
from holoviews.operation.datashader import datashade, bundle_graph

hv.extension('bokeh')

%opts Nodes Graph [width=800 height=200 xaxis=None yaxis=None]
%opts Graph (node_size=8 edge_line_width=0.5)

colors = ['#000000']+hv.Cycle('Category20').values

edges_df = pd.read_csv('edges.csv')
nodes = hv.Nodes(pd.read_csv('nodes.csv')).sort()

graph = hv.Graph((edges_df, nodes))
graph = graph.redim.range(x=(-0.05, 1.05), y=(-0.05, 1.05)).opts(style=dict(cmap=colors))

bundled = bundle_graph(graph)
bundled

输出

enter image description here

我试图修改这一行:

node_size=[3,4,5,8,3,8]
edge_size=[0.5,0.6,0.8,1.5,0.5]
%opts Graph (node_size=node_size edge_line_width=edge_size)

然而,这不起作用。

任何人都可以帮助我吗?如何创建不同大小的节点和边缘?

2 个答案:

答案 0 :(得分:2)

目前,节点大小和边缘线宽的缩放不是直接公开的(尽管很快就会公开),但是你可以直接修改数据源和字形来做这种事情。下面是一个定义所谓的finalize_hook的示例,它将修改数据源和字形,以便您定义两者的自定义缩放:

node_size=[3,4,5,8,3,8]
edge_size=[0.5,0.6,0.8,1.5,0.5]

def scale_sizes(plot, element):
    plot.handles['scatter_1_source'].data['node_size'] = node_size
    plot.handles['scatter_1_glyph'].size = 'node_size'

    plot.handles['multi_line_1_source'].data['edge_size'] = edge_size
    plot.handles['multi_line_1_glyph'].line_width = 'edge_size'

%opts Graph [finalize_hooks=[scale_sizes]]

答案 1 :(得分:1)

我使用输入文件中的新列解决了节点大小问题。

import numpy as np
import cv2

    cap = cv2.VideoCapture('C:\\Users\\KRK\\Desktop\\Dec17thVideo.mp4')

    while(True):
        # Capture frame-by-frame
        ret, frame = cap.read()

        # Our operations on the frame come here
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # Display the resulting frame
        cv2.imshow('frame',gray)
        if cv2.waitKey(1000) & 0xFF == ord('q'):
            break

    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()

之后,我在图表的选项中选择了列名

x,y,index,type,size
0.0,1.0,1,a,8
0.5,1.0,2,a,10
1.0,1.0,3,a,20
0.4,0.0,4,b,8
0.5,0.0,5,b,3
0.6,0.0,6,b,15

但是,此策略不适用于边缘大小。

enter image description here