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 ...
随机推荐
- DAY 5 上午
或者跑一个dp dp[i]表示总花费不超过i的情况下的最短路 dij套dp o(nk)个点 对于每一个点u,建立k+1个点表示到点u花费费用为i 比如u-->v长度为c u,0-->v,c ...
- centos修改时区,同步时间
查看当前系统时区 ls -la /etc/localtime 查看支持的时区 timedatectl list-timezones # 查看所有时区 timedatectl list-timezone ...
- Vue知识整理1:$watch方法的使用
如下图所示:vue中,可以使用$watch方法显示变量的前面值和当前值,方便进行判断.使用方法: vm.$watch('a',function(newval,oldval){ ...... })
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_01 File类_4_File类的构造方法
学习一个类先学习构造方法和静态方法 重写了Object类的toString方法 文件夹结尾 相对路径 第二个构造方法 两个路径组装到了一起 加上双斜线 换成d盘 第三个构造 第一个参数是一个File对 ...
- Linux负载均衡实现
配置之前清空所有服务器防火墙规则 iptables -F 关闭selinux: 1./usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开 ...
- Ultra.Base
winform 基础类库 https://github.com/ZixiangBoy/Ultra.Base
- layer.msg()自动关闭后刷新页面
layer.msg("2秒就消失哦", { time: 2000 }, function () { window.location.href ...
- Java 实现Excel的简单读取操作
JAVA实现Excel表单的简单读取操作 实现Excel表单的简单读取操作,首先要导入相关的jar包: 如图所示: 此处贴上代码: public static List<List<Stri ...
- Tesseract5.0训练字库,提高OCR特殊场景识别率,合并字库(二)
一.准备工作 需要的文件 tif文件和box文件. 如果你打标打好了,但是是分批次打标的,那么可以合并字库,我们最初只需要 tif 和 box 文件,如下: 二.生成对应的 .tr 训练文件 根据不同 ...
- PyTorch笔记之 scatter() 函数
scatter() 和 scatter_() 的作用是一样的,只不过 scatter() 不会直接修改原来的 Tensor,而 scatter_() 会 PyTorch 中,一般函数加下划线代表直接在 ...