在Networkx中将饼图创建为节点 [英] Creating piechart as nodes in Networkx
问题描述
我想在使用Networkx创建的图形的每个节点位置创建饼图.基于
I want to create piechart in each node position of a graph created using Networkx. Based on the comments given in this post, I've tried the following.
import pygraphviz as pgv
import networkx as nx
import matplotlib.pyplot as plt
import plotly.graph_objects as go
from pprint import pprint
from collections import OrderedDict
if __name__ == '__main__':
tail = [1, 2, 3]
head = [2, 3, 4]
xpos = [0, 1, 2, 3]
ypos = [0, 0, 0, 0]
xpos_ypos = [(x, y) for x, y in zip(xpos, ypos)]
ed_ls = [(x, y) for x, y in zip(tail, head)]
G = nx.OrderedDiGraph()
G.add_edges_from(ed_ls)
# set node positions
pos = OrderedDict(zip(G.nodes, xpos_ypos))
nx.draw(G, pos=pos, with_labels=True)
nx.set_node_attributes(G, pos, 'pos')
# set node property 1
prop1 = [0.1, 0.2, 0.3, 0.4]
nx.set_node_attributes(G, prop1, 'prop1')
# set node property 2
prop2 = [0.5, 0.6, 0.4, 0.1]
nx.set_node_attributes(G, prop2, 'prop2')
# set node property 3
prop3 = [20, 10, 5, 1]
nx.set_node_attributes(G, prop3, 'prop3')
# set node property 4
prop4 = [24, 256, 2547, 101]
nx.set_node_attributes(G, prop4, 'prop4')
# create pie-chart in nodes
H = nx.nx_agraph.to_agraph(G)
H.node_attr['style'] = 'wedged'
for i in H.nodes():
n = H.get_node(i)
n.attr['prop1'] = # Here, I want to convert this prop1 = [0.1, 0.2, 0.3, 0.4] to colormap and assign colors
plt.show()
There are four properties assigned to nodes, prop1, prop2, prop3 and prop4. I want to create 4 equal fractions in the piechart ( similar to this, but equal fraction) and color each fraction based on the values stored in variables prop1, prop2, prop3 and prop4.
In brief, instead of assigning a single color to each node, here I have a pie chart with number of fractions equal to the number of node attributes/properties. Values stored for each property will be used to assign colors to the fractions present in the piechart that positioned at the center of each node.
Any suggestions on how to do this will be really helpful
For the nodes: plt.pie accepts coordinates to set the center. These coordinates can be generated with netwrokx. Then, you plot the pies instead of the nodes.
To get the colors, you can make use of the fact that feeding float values into a colormap returns the rgb values, which plt.pie accepts as inputs. So, if you normalise you data, you can feed the individual values into the colormap.
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
# parameters for pie plot
radius = 0.2
cmap = plt.cm.viridis
# make graph
G = nx.Graph()
nodes = [1,2,3,4]
edges = ((1,2),(3,2),(1,4),(3,4))
G.add_nodes_from(nodes)
G.add_edges_from(edges)
pos = nx.spring_layout(G)
# storing attributes in a dict
attrs = {
1:[1,1,1],
2:[0.5, 1, 0],
3:[0, 0.6, 0],
4:[0.5, 1, 0.5],
}
# normalising data to make colors
a = np.array(list(attrs.values()))
maxes = np.max(a, axis=0) # note that this will throw an error if different nodes have different number of features.
# collect colors in dictionary
colors= {}
for key, val in attrs.items():
colors[key] = list(np.array(val)/maxes)
# draw graph and draw pieplots instead of nodes
nx.draw_networkx_edges(G, pos=pos)
for node in G.nodes:
attributes = attrs[node]
a = plt.pie(
[1]*len(attributes), # s.t. all wedges have equal size
center=pos[node],
colors = [cmap(a) for a in colors[node]],
radius=radius)
plt.ylim(-2,2)
plt.xlim(-2,2)
这篇关于在Networkx中将饼图创建为节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!