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 ...
随机推荐
- nw打包vue项目 安装包
接着上篇nw打包vue项目exe中: copy /b nw.exe+dome.nw dome.exe 出现了dome.exe文件之后,要是打算打包成为安装包,网上推荐的是Inno Setup Comp ...
- Leveldb源码分析--2
coming from http://blog.csdn.net/sparkliang/article/details/8573618
- <每日一题> Day4:CodeForces-1042A.Benches(二分 + 排序)
题目链接 参考代码: /* 排序 + 每次取小 #include <iostream> #include <algorithm> using namespace std; co ...
- A + B Problem II(1002)
Problem Description I have a very simple problem for you. Given two integers A and B, your job is to ...
- app防攻击办法
方法一 要求请求端带上一个随机字符串state(也可以是特定规则生成的,甚至是从服务器上请求过来的),服务端(用过滤/拦截器之类的实现不会影响业务代码)收到之后缓存一定的时间(长短视业务和硬件),每次 ...
- 问题 M: 最亲密的x个人
问题 M: 最亲密的x个人 时间限制: 1 Sec 内存限制: 128 MB提交: 412 解决: 38[提交] [状态] [命题人:jsu_admin] 题目描述 有一天,地球受到了降维打击,从 ...
- Flutter 初探 -
flutter 安装 经过许久的关注,及最近google算是真正地推行flutter时,加上掘金小册也有相应的教程,我知道自己得跟着这一波潮流学习了,不然迟早会面临着小程序的危(大家都会了就你不会), ...
- 把int 类型转化为varchar并且去掉小数点同时以千分号‘,’分割
把int 类型转化为money 类型,再转化为varchar,去掉小数点同时以千分号‘,’分割. select '$' + left(Convert(VARCHAR, cast(10000 as mo ...
- highcharts控制X刻度值整数调整
function chartData() { var app_id = $('.app_id').attr('app_id'); var gener_id = $('.gener_id').attr( ...
- moongoose对象无法新增删除属性
昨天用nodes中的moongoose去查询一个结果遇到一个大坑,这个坑貌似用moongoose可能会遇到.背景是这样的,我在nodejs中去查询document,得到的可以看作是一个对象list.在 ...