[英]Plot a histogram with the x axis values based on the height of the column
So say I have the following: 所以说我有以下几点:
[1,5,1,1,6,3,3,4,5,5,5,2,5]
Counts: {1:3, 2:1, 3:2, 4:1, 5:5, 6:1}
Now, I wanted to print a plot like a histogram that is sorted on the x axis, as in: 现在,我想打印一个像直方图的图,该图在x轴上排序,如下所示:
A traditional histogram is: 传统的直方图是:
X
X
X X
X X X
X X X X X X
1 2 3 4 5 6
What I would want is: 我想要的是:
X
X
X X
X X X
X X X X X
2 4 3 1 5
My current plotting code is: 我当前的绘图代码为:
plt.clf()
plt.cla()
plt.xlim(0,1)
plt.axvline(x=.85, color='r',linewidth=0.1)
plt.hist(correlation,2000,(0.0,1.0))
plt.xlabel(index[thecolumn]+' histogram')
plt.ylabel('X Data')
savefig(histogramsave,format='pdf')
Please help me out about how I can do this... I understand I posted a similar question before, but I believe I was unclear about it.... 请帮助我解决我的问题。我了解我之前曾发布过类似的问题,但我认为我对此尚不清楚。
Histogram is not the graph you are looking for. 直方图不是您想要的图形。 Use the bar chart.
使用条形图。
import numpy as np
import matplotlib.pyplot as plt
data = [1, 5, 1, 1, 6, 3, 3, 4, 5, 5, 5, 2, 5]
correlation = [(i, data.count(i)) for i in set(data)]
correlation.sort(key=lambda x: x[1])
labels, values = zip(*correlation)
indexes = np.arange(len(correlation))
width = 1
plt.bar(indexes, values, width)
plt.xticks(indexes + width * 0.5, labels)
plt.show()
EDIT: For large set of data better use collections.Counter instead of the list comprehension with count
. 编辑:对于大量数据,最好使用collections.Counter而不是
count
的列表理解。
And here is the way to archive same result much faster (without neither bar chart nor hist): 这是更快地存档相同结果的方法(既没有条形图也没有历史记录):
from collections import Counter
import numpy as np
import matplotlib.pyplot as plt
data = np.random.random_integers(0, 10**4, 10**5)
correlation = Counter(data).items()
correlation.sort(key=lambda x: x[1])
labels, values = zip(*correlation)
indexes = np.arange(len(correlation))
plt.plot(indexes, values)
plt.fill_between(indexes, values, 0)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.