Python 为Matplotlib 2D plot组织三维数据
我正在分析网络图的连通性,测量所有节点的入度和出度 我希望在02:00时显示如中所示的结果 如您所见,讲师在x轴上绘制了in_度,在y轴上绘制了out_度。色标表示具有这些x、y特征的节点数。他的例子很简单,每个轴的最小值为0,最大值为3 我以Python 为Matplotlib 2D plot组织三维数据,python,matplotlib,2d,histogram,colorbar,Python,Matplotlib,2d,Histogram,Colorbar,我正在分析网络图的连通性,测量所有节点的入度和出度 我希望在02:00时显示如中所示的结果 如您所见,讲师在x轴上绘制了in_度,在y轴上绘制了out_度。色标表示具有这些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}) 数据可以运行到5000个
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})
数据可以运行到5000个左右的节点。上面的计数器对象未排序
我上面的示例采用以下形式
{(in_degree, out_degree): count, (in_degree, out_degree): count, etc. }
因此,您可以看到有7个节点具有0 in_度和0 out_度,2个节点具有1 in_度和3 out度。。。。。最后是一个节点,23 in_度,46 out_度
我正试图找出如何让它进入一种状态来绘制它,使用类似的东西
在不知道x轴和y轴的上限的情况下,我应该如何安排用于打印的数据,以及如何动态计算箱子大小
欢迎提供任何建议,不一定是解决方案 以下是使用计数数据获取热图图的示例:
%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');
它给出了这个图表:
谢谢@xdze2,太好了,而且效果很好。我发现我过度处理了数据,并将其作为元组列表保存,而不是作为计数器保存,而计数值对更容易解决。但是你的回答完全符合我提出的问题!非常感谢。