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()
Source for reference:
degree-histogram, networkx

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()

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)
read more: 用python + networkx探索和分析网络数据
python绘制图的度分布柱状图, draw graph degree histogram with Python的更多相关文章
- 用Python 绘制分布(折线)图
用Python 绘制分布(折线)图,使用的是 plot()函数. 一个简单的例子: # encoding=utf-8 import matplotlib.pyplot as plt from pyla ...
- Python绘制六种可视化图表详解,三维图最炫酷!你觉得呢?
Python绘制六种可视化图表详解,三维图最炫酷!你觉得呢? 可视化图表,有相当多种,但常见的也就下面几种,其他比较复杂一点,大都也是基于如下几种进行组合,变换出来的.对于初学者来说,很容易被这官网上 ...
- python 绘制柱状图
python 绘制柱状图 import matplotlib.pyplot as plt import numpy as np # 创建一个点数为 8 x 6 的窗口, 并设置分辨率为 80像素/每英 ...
- 使用python绘制根轨迹图
最近在学自动控制原理,发现根轨迹这一张全是绘图的,然而书上教的全是使用matlab进行计算机辅助绘图.但国内对于使用python进行这种绘图的资料基本没有,后来发现python-control包已经将 ...
- Python的可视化包 – Matplotlib 2D图表(点图和线图,.柱状或饼状类型的图),3D图表(曲面图,散点图和柱状图)
Python的可视化包 – Matplotlib Matplotlib是Python中最常用的可视化工具之一,可以非常方便地创建海量类型地2D图表和一些基本的3D图表.Matplotlib最早是为了可 ...
- Python绘制语谱图+时域波形
"""Python绘制语谱图""" """Python绘制时域波形""" # 导 ...
- Python 绘制 柱状图
用Python 绘制 柱状图,使用的是bar()函数. 一个简单的例子: # 创建一个点数为 8 x 6 的窗口, 并设置分辨率为 80像素/每英寸 plt.figure(figsize=(10, 1 ...
- Python绘制面积图
一.Python绘制面积图对应代码如下图所示 import matplotlib.pyplot as plt from pylab import mpl mpl.rcParams['font.sans ...
- Python绘制折线图
一.Python绘制折线图 1.1.Python绘制折线图对应代码如下图所示 import matplotlib.pyplot as pltimport numpy as np from pylab ...
随机推荐
- CentOS7 日常操作
A 安装netstat1.首先配置好本机的yum源: yum repolist all2.利用netstat命令,却提示:-bash: netstat: command not found3.执行yu ...
- 像计算机科学家一样思考python-第3章 函数
在程序设计中,函数是指用于进行某种计算的一系列语句的有名称的组合.定义一个函数时,需要指定函数的名称并写下一系列程序语句.之后,就可以使用名称来“调用”这个函数 3.1函数调用 一个函数调用的例子 & ...
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第5节 String类_5_字符串的获取相关方法
original:原来的.开始的 如果有多次的情况 因为查找的是第一次的出现的位置的索引值 如果要查找的字符串压根没有的话返回的就是-1
- HttpServletRequest中的request.setAttribute()和request.getSession().setAttribute()
request.setAttribute("num",value):有效范围是一个请求范围,不发送请求的界面无法获取到value的值,jsp界面获取使用EL表达式${num}:re ...
- SqlServer 主重复制
一.准备工作: 主数据库服务器: OS:Windows Server 2008 R2 DB: SQL Server 2008 R2 Hostname : CXMasterDB IP: 192.1 ...
- 黑群晖DSM 6.x 配置文件grub.cfg修改 mac地址/sn等修改
新的DSM 6.x配置文件和以前的XPEnoboot的配置文件不一样了,我们可以通过OSFMount虚拟光驱软件打开img后再修改. 安装完成后运行OSFMount点击左下角-Mount new,选择 ...
- 【EWM系列】SAP EWM Warehouse Order Creation
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[EWM系列]SAP EWM Warehouse ...
- python控制流-导入模块
一.模块 1.含义 Python 程序可以调用一组基本的函数,这称为“内建函数”,包括你见到过的 print().input()和 len()函数.Python 也包括一组模块,称为“标准库”.每个模 ...
- Reverse Linked List(反转单向链表)
来源:https://leetcode.com/problems/reverse-linked-list Reverse a singly linked list. 递归方法:递归调用直到最后一个节点 ...
- linux shell中的正则表达式
正则表达式的使用 正则表达式,又称规则表达式.(英语:Regular Expression [ˈreɡjulə] 规则的 [ iksˈpreʃən] 表达 ),在代码中常简写为regex.regexp ...