组织Matplotlib 2D图的三维数据

时间:2018-08-10 18:41:14

标签: python matplotlib 2d histogram colorbar

我正在分析网络图的连通性,测量所有节点的in_degree和out_degrees。

我正在尝试显示this video中02:00所示的结果

您可以看到,讲师在x轴上绘制了in_degrees,在y轴上绘制了out_degrees。色标表示具有这些x,y特性的节点数。他的示例很简单,每个轴的最小值为0,最大值为3。

我以

的格式生成了数据
Counter({(0, 0): 7, (1, 3): 2, (19, 0): 2, (0, 2): 2, 
etc. 
(27, 42): 1, (25, 43): 1, (26, 36): 1, (23, 46): 1})

数据可以运行到5,000个左右的节点。上面的计数器对象未排序。

我上面的示例采用

的形式
{(in_degree, out_degree): count, (in_degree, out_degree): count, etc. }

因此,您可以看到有7个节点的in_degrees和0 out_degrees,2个节点的in_degrees和3 out_degrees ...以及最后一个节点具有23个in_degrees和46个out_degrees。

我正在尝试使用this example之类的方法弄清楚如何使其处于绘制状态。

在不知道x和y轴的上限时,应如何安排数据以进行绘图-如何动态计算出箱尺寸?

任何指针,不一定是解决方案,都将受到欢迎!

1 个答案:

答案 0 :(得分:1)

以下是使用计数数据获取热图图的示例:

%matplotlib inline
import matplotlib.pylab as plt

import numpy as np

data = {(0, 0): 7, (1, 3): 2, (19, 0): 2, (0, 2): 2, 
        (3, 3): 2, (2, 3): 6, (8, 6): 2, (9, 4): 2, 
        (10, 12): 1, (15, 13): 1, (16, 16): 1, (13, 15): 1}

# Create an empty array:
max_x = max( xy[0] for xy in data.keys() ) 
max_y = max( xy[1] for xy in data.keys() ) 

count_grid = np.zeros((max_x+1, max_y+1))

# Populate the array:
for xy, count in data.items():
    x, y = xy
    count_grid[x, y] = count

# Plot
plt.pcolor(count_grid); plt.colorbar()
plt.xlabel('x'); plt.ylabel('y'); plt.title('count');

下面给出这张图:

heatmap