在Python中绘制频率分布
我有一个以邻接列表格式存储的图。我随机选择一堆节点并记下每个节点拥有的邻居数量。我现在想要绘制分布图,我现在的方法是手动检查邻居集的大小是否落入特定的存储桶(我手动设置存储桶大小,这个检查过程会导致一堆非常难看的结果) if-then-else 语句),然后相应地增加频率。然后我调用 matplotlib 并绘制图表。整个过程看起来真的很麻烦,而且一点也不Pythonic。这在 Excel 中是完全可行的,但我正在努力使其尽可能编程化。
我确信有更好的方法可以做到这一点,但我找不到与频率绘图相关的任何内容。任何建议都会很棒。
I have a graph stored in an adjacency list format. I randomly select a bunch of nodes and note the number of neighbors each of them have. I now want to plot the distribution, and the way I do it right now is by manually checking if the size of the neighbor set falls into a particular bucket (I set the bucket sizes manually and this checking process results in a bunch of very ugly if-then-else statements) and then increment the frequency accordingly. I then call matplotlib and plot the graph. This entire process seems really cumbersome and not pythonic at all. It's totally doable in Excel but I'm trying to make it as programmatic as possible.
I'm sure there's a better way to do this but I couldn't find anything related to frequency plotting. Any suggestions would be awesome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是 matplotlib.pyplot.hist() 您正在寻找的东西?
Is matplotlib.pyplot.hist() what you are looking for?
为什么不简单地绘制您注意到的“每个人拥有的邻居数量”的密度,而不是计算间隔然后绘制它们? 这是一篇关于如何在 Python 中执行此操作的精彩文章。
Instead of computing intervals an then plotting them, why not simply plot the density of the "the number of neighbors each of them have" you noted? Here is a great post on how to do this in Python.