图的度数分布

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的更多相关文章

  1. 用Python 绘制分布(折线)图

    用Python 绘制分布(折线)图,使用的是 plot()函数. 一个简单的例子: # encoding=utf-8 import matplotlib.pyplot as plt from pyla ...

  2. Python绘制六种可视化图表详解,三维图最炫酷!你觉得呢?

    Python绘制六种可视化图表详解,三维图最炫酷!你觉得呢? 可视化图表,有相当多种,但常见的也就下面几种,其他比较复杂一点,大都也是基于如下几种进行组合,变换出来的.对于初学者来说,很容易被这官网上 ...

  3. python 绘制柱状图

    python 绘制柱状图 import matplotlib.pyplot as plt import numpy as np # 创建一个点数为 8 x 6 的窗口, 并设置分辨率为 80像素/每英 ...

  4. 使用python绘制根轨迹图

    最近在学自动控制原理,发现根轨迹这一张全是绘图的,然而书上教的全是使用matlab进行计算机辅助绘图.但国内对于使用python进行这种绘图的资料基本没有,后来发现python-control包已经将 ...

  5. Python的可视化包 – Matplotlib 2D图表(点图和线图,.柱状或饼状类型的图),3D图表(曲面图,散点图和柱状图)

    Python的可视化包 – Matplotlib Matplotlib是Python中最常用的可视化工具之一,可以非常方便地创建海量类型地2D图表和一些基本的3D图表.Matplotlib最早是为了可 ...

  6. Python绘制语谱图+时域波形

    """Python绘制语谱图""" """Python绘制时域波形""" # 导 ...

  7. Python 绘制 柱状图

    用Python 绘制 柱状图,使用的是bar()函数. 一个简单的例子: # 创建一个点数为 8 x 6 的窗口, 并设置分辨率为 80像素/每英寸 plt.figure(figsize=(10, 1 ...

  8. Python绘制面积图

    一.Python绘制面积图对应代码如下图所示 import matplotlib.pyplot as plt from pylab import mpl mpl.rcParams['font.sans ...

  9. Python绘制折线图

    一.Python绘制折线图 1.1.Python绘制折线图对应代码如下图所示 import matplotlib.pyplot as pltimport numpy as np from pylab ...

随机推荐

  1. ORA-20011

    Sun Jul 23 22:09:07 2017DBMS_STATS: GATHER_STATS_JOB encountered errors. Check the trace file.Errors ...

  2. qbzt day5 下午

    农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地.John打算在牧场上的某几格里种上美味的草,供他的奶牛们享 ...

  3. 词频分析 评论标签 nltp APP-分析买家评论的评分-高频词:二维关系

    0-定评论结果:好评.差评,1星.4星,二元化为“积极.消极”,取一元的数据为样本 1-得到词频结果:如手机类的“积极样本”得到前10的高频词:运行(run running ran).内存(memor ...

  4. linux 下spyder安装

    linux  下spyder安装: 安装qt4,python3 对应qt5 sudo apt-get install libxext6 libxext-dev libqt4-dev libqt4-gu ...

  5. format和urlencode的使用对比

    一:format的基本语法使用 基本语法是通过 {} 和 : 来代替以前的 % . format 函数可以接受不限个参数,位置可以不按顺序. 例如: >>>"{} {}&q ...

  6. UI自动化之特殊处理三(日期控件\表格\富文本)

    日期控件\表格\富文本也是一些常遇到的需要特殊处理的定位 目录 1.日期控件 2.表格 3.富文本 1.日期控件 第一种:输入框属性为:readonly="readonly" # ...

  7. delphi 访问 protected 属性 哈哈

    unit Unit39; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Syste ...

  8. Bootstrap 学习笔记 项目实战 首页内容介绍 上

    效果图: HTML代码: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset ...

  9. [Usaco2014 Feb] Roadblock

    有一个无向图,共N个节点,编号1至N,共M条边.FJ在节点1,它想到达节点N.FJ总是会选择最短路径到达节点N .作为捣蛋的奶牛Bessie,它想尽量延迟FJ到达节点N的时间,于是Bessie决定从M ...

  10. TensorFlow学习笔记13-循环、递归神经网络

    循环神经网络(RNN) 卷积网络专门处理网格化的数据,而循环网络专门处理序列化的数据. 一般的神经网络结构为: 一般的神经网络结构的前提假设是:元素之间是相互独立的,输入.输出都是独立的. 现实世界中 ...