python 画柱状图 数据精确度_python绘制图的度分布柱状图, draw graph degree histogram with Python...

图的度数分布

import collections

import matplotlib.pyplot as plt

import networkx as nx

G = nx.gnp_random_graph(100, 0.02)

degree_sequence = sorted([d for n, d in G.degree()], reverse=True) # degree sequence

# print "Degree sequence", degree_sequence

degreeCount = collections.Counter(degree_sequence)

deg, cnt = zip(*degreeCount.items())

# #as an alternation, you can pick out the top N items for the plot:

#d = sorted(degreeCount.items(), key=lambda item:item[1], reverse=True)[:30] # pick out the up 30 items from counter

#deg = [i[0] for i in d]

#cnt = [i[1] for i in d]

fig, ax = plt.subplots()

plt.bar(deg, cnt, width=0.80, color='b')

plt.title("Degree Histogram")

plt.ylabel("Count")

plt.xlabel("Degree")

ax.set_xticks([d + 0.4 for d in deg])

ax.set_xticklabels(deg)

# draw graph in inset

plt.axes([0.4, 0.4, 0.5, 0.5])

Gcc = sorted(nx.connected_component_subgraphs(G), key=len, reverse=True)[0]

pos = nx.spring_layout(G)

plt.axis('off')

nx.draw_networkx_nodes(G, pos, node_size=20)

nx.draw_networkx_edges(G, pos, alpha=0.4)

plt.draw()

Draw the histogram for values of dict

import collections

import matplotlib.pyplot as plt

dict_granuLevel = {'1283': 9, '291': 5, '451': 6, '964': 8, '1093': 5, '525': 8, '878': 11, '1553': 9, '1107': 6, '1588': 8,

'1435': 6, '861': 8, '1054': 9}

value_sequence = sorted([d for d in dict_granuLevel.values()], reverse=True) # value sequence

print("value sequence:", value_sequence)

valueCount = collections.Counter(value_sequence)

val, cnt = zip(*valueCount.items())

# # as an alternation, you can pick out the top N items for the plot:

# d = sorted(degreeCount.items(), key=lambda item:item[1], reverse=True)[:10] # pick out the up 10 items from counter

# val = [i[0] for i in d]

# cnt = [i[1] for i in d]

fig, ax = plt.subplots()

plt.bar(val, cnt, width=0.80, color='b')

plt.title("value Histogram")

plt.ylabel("Count")

plt.xlabel("value")

ax.set_xticks([d + 0.4 for d in val])

ax.set_xticklabels(val)

plt.show()

780771-20191215213205661-1904942784.png

The function style:

import collections

import matplotlib.pyplot as plt

def plot_histogram(list_input, k=0):

'''

draw the histogram for items in list_input

:param list: list of count_numbers. all items are required to be int.

:param k: the top k-th count of items to be considered for drawing the plot. default: k=0, plot all

:return:

'''

valueCount = collections.Counter(list_input)

val, cnt = zip(*valueCount.items())

print(' len of val, cnt:', len(val), end='')

if k != 0:

print(' pick the largest', k, 'cnt for histogram.')

d = sorted(valueCount.items(), key=lambda item: item[1], reverse=True)[:k] # pick out the up k items from counter

else:

d = sorted(valueCount.items(), key=lambda item: item[1], reverse=True)

print(' k = 0. Pick all the cnt for histogram.')

val = [i[0] for i in d]

cnt = [i[1] for i in d]

fig, ax = plt.subplots()

plt.bar(val, cnt, width=0.80, color='b')

plt.title("value Histogram")

plt.ylabel("Count")

plt.xlabel("value")

ax.set_xticks([d + 0.4 for d in val])

ax.set_xticklabels(val)

plt.show()

return

dict_granuLevel = {'tom': 9, 'cat': 5, 'dot': 6, 'dog': 8, 'hors': 5, 'fao': 8, 'pao': 11, 'koo': 9, 'jan': 6, 'dec': 8,

'foo': 6, 'doo': 8, 'coo': 9}

value_sequence = sorted([d for d in dict_granuLevel.values()], reverse=True) # value sequence

print("value sequence:", value_sequence)

plot_histogram(value_sequence, 3)

draw the edge_weighted graph:

for network with weighted edges, draw the graph :

###

import matplotlib.pyplot as plt

pos = nx.planar_layout(G) # pos = nx.spring_layout(G)

labels = nx.get_edge_attributes(G,'weight')

nx.draw_networkx_edge_labels(G,pos,edge_labels=labels)

plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值