python - 饼图作为 networkx 中的节点

标签 python matplotlib pie-chart networkx

我想知道如何绘制一个网络(如果可以的话),但我想要一个饼图而不是圆圈作为节点,因为我有一个包含社区的图表并且我想用在每个部门工作的人。

编辑: 好的,我刚刚试过这段代码,结果是好的:

import community
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import random as rnd
import operator as op
import matplotlib.patches as mpatches
import math

Com = nx.Graph()

Com.add_nodes_from(['0','1','2','3','4'])
comlist=[('0','2',5.0),('3','0',3.0),('1','3',1.0),('2','3',7.3)]
Com.add_weighted_edges_from(comlist)
ListDeps = ['literature','maths','science','physical education']
HistCom = {'0':{'literature':20,
                'maths':24,
                'science':12},
           '1':{'literature':1,
                'physical education':14,
                'science':6},
           '2':{'science':15},
           '3':{'physical education':4,
                'maths':20},
           '4':{'literature':20,
                'maths':24,
                'science':12}}

pos=nx.spring_layout(Com)

fig=plt.figure(figsize=(5,5))
ax=plt.axes([0,0,1,1])
ax.set_aspect('equal')
nx.draw_networkx_edges(Com,pos,ax=ax)

plt.xlim(-0.5,1.5)
plt.ylim(-0.5,1.5)

trans=ax.transData.transform
trans2=fig.transFigure.inverted().transform

piesize=0.1 #Degree
p2=piesize/2.0

dep_color = dict()
for com in Com:
    xx,yy=trans(pos[com]) # figure coordinates
    xa,ya=trans2((xx,yy)) # axes coordinates
    a = plt.axes([xa-p2,ya-p2, piesize, piesize])
    a.set_aspect('equal')
    contats = len(HistCom[com].items())
    fracs = []
    ColorFrac = []
    for dep,n in sorted(HistCom[com].items(), key=op.itemgetter(1), reverse=True):
        fracs.append(n/sum(HistCom[com].values()))
        dep_color[dep] = dep_color.get(dep, (rnd.random(),rnd.random(),rnd.random()))
        ColorFrac.append(dep_color[dep])
    a.pie(fracs, colors = ColorFrac)


P = [mpatches.Patch(color=dep_color[dep], label=dep) for dep in dep_color]
plt.legend(handles=P,bbox_to_anchor=(0.,1.02,1.,.102), loc=3,ncol=3, mode="expand", borderaxespad=0.)
plt.show()

我遇到的问题是,当我尝试绘制图例时,我试图将它放在图上方,但它出现在中间。

最佳答案

这是我不久前发布到 networkx-discuss 邮件列表的内容。 “转换”部分不太正确(需要 matplotlib 专家)所以缩放不能正常工作。但这是一个开始,

import networkx as nx
import matplotlib.pyplot as plt
G=nx.complete_graph(4)
pos=nx.spring_layout(G)

fig=plt.figure(figsize=(5,5))
ax=plt.axes([0,0,1,1])
ax.set_aspect('equal')
nx.draw_networkx_edges(G,pos,ax=ax)

plt.xlim(-0.5,1.5)
plt.ylim(-0.5,1.5)

trans=ax.transData.transform
trans2=fig.transFigure.inverted().transform

piesize=0.2
p2=piesize/2.0
for n in G:
    xx,yy=trans(pos[n]) # figure coordinates
    xa,ya=trans2((xx,yy)) # axes coordinates
    a = plt.axes([xa-p2,ya-p2, piesize, piesize])
    a.set_aspect('equal')
    fracs = [15,30,45, 10]
    a.pie(fracs)

plt.savefig('pc.png')

enter image description here

关于python - 饼图作为 networkx 中的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26714730/

相关文章:

Javascript Pie Ch

python - 自定义转换器添加附加列

python - 如何在 Ipython 中显示与内联的 matplotlib 图交错的打印语句?

python - 我们什么时候在程序中添加错误处理代码?

python - 从 tricontourf 去除轮廓周围的白色痕迹

python - 多次运行计算数学函数 (Numpy)

r - ggplot饼图标签

c# - AjaxControlToolkit 饼图未正确呈现

python - 在 cx_Oracle 上执行许多 CLOB 元素

python - 在 for 循环中使用 pandas 附加在新列中获取不需要的值