简体   繁体   English

以xy样式创建直方图数据的对数图-Python Matplotlib

[英]Create loglog plot of histogram data in xy style - python matplotlib

I have a list of values (integers) and I want to show on a graph how many times each value in it occurs. 我有一个值列表(整数),我想在图表上显示每个值出现多少次。 I have created a dictionary of values and corresponding frequencies from the list as after searching on here, that seemed best way to do it 在这里搜索之后,我从列表中创建了一个值和相应频率的字典,这似乎是最好的方法

I want to plot the data out on an xy loglog graph (with connecting lines) rather than use a bar plot. 我想将数据绘制在xy loglog图(带有连接线)上,而不是使用条形图。

The data points on the graph appear as expected when using 使用时,图形上的数据点按预期显示

plot.loglog(degree_frequencies.keys(),degree_frequencies.values()) 

however, since the lines on the graph connect the first key-value "co-ordinate" to the second, second to third etc and being unordered, this creates a line that jumps about all over the place, rather than a line that goes from left to right. 但是,由于图形上的线将第一个键值“坐标”连接到第二个,第二个到第三个等等,并且是无序的,因此创建了一条在整个位置上跳跃的线,而不是从左到右。

Here is an example dataset - where the first "co-ordinate" pair has a higher x value than the second 这是一个示例数据集-其中第一对“坐标”对具有比第二对更高的x值

degree_frequencies  = {8: 1, 2: 6, 3: 1, 5: 2, 6: 2} 

(if the dictionary was rearranged to {2: 6, 3: 1, 5: 2, 6: 2, 8: 1} the line would look fine) (如果将字典重新排列为{2:6,3:1、5:2、6:2、8:1},则该行看起来会很好)

Could anyone advise the best way to go about this? 有人可以建议最好的方法吗? I am wondering if I need to treat the list differently in the first place or if there is in fact a way to plot data like this using matplotlib at all. 我想知道我是否首先需要对列表进行不同处理,或者实际上是否真的有一种方法可以使用matplotlib绘制此类数据。

I'm a relatively inexperienced Python user, but I have spent some time trawling the internet and the documentation (which I'm finding heavy going) without finding an answer. 我是一个相对缺乏经验的Python用户,但是我花了一些时间在互联网和文档(我觉得很辛苦)上拖网而找不到答案。 Easy to understand/implement (in my circumstances) would probably be more helpful than elegant yet complex solutions (if poss). 容易理解/实施(在我的情况下)可能比优雅但复杂的解决方案(如果有)更有用。

Thanks 谢谢

x = sorted( degree_frequencies.keys( ) )
y = [ degree_frequencies[ k ] for k in x ]
plt.loglog( x, y )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM