简体   繁体   English

根据列的高度绘制带有x轴值的直方图

[英]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.

相关问题 在x轴上绘制索引值并在y轴上绘制每个列值的频率的直方图 - Plot histogram with index values on x axis and frequencies of each column value on y axis 如何为只有 1 列和多行的数据框创建直方图(行值应绘制在 x 轴上,列值应绘制在 y 轴上) - How to create a histogram for a dataframe with just 1 column and multiple rows (row values should be plot on x axis and column values on y axis) Matplotlib基于x轴上的值的多种颜色的绘图 - Matplotlib plot with multiple colors based on values on x-axis 如何将时间戳列转换为 plot 中 X 轴的值 - How can I turn the Timestamp column into the values ​of the X axis in the plot 以列名作为 x 轴绘制直方图 - Plotting a histogram with the column names as the x-axis 如何绘制直方图,yaxis 为“对应于每个 x bin 的 y 值的总和”,x 轴为 python 中 x 的 n 个 bin? - How to plot a histogram with the yaxis as the "total of y values corresponding to each x bin" and x axis as n bins of x in python? X 轴上的字符串值的堆叠直方图失败 - Stacked histogram fails for string values in X axis 根据数据框列中的值绘制直方图 - Plot a histogram based on a value in a column of a dataframe 无法使用 Matplotlib 和 Python 在 x 轴上绘制带有时间的直方图 - Unable to plot histogram with time on x-axis using Matplotlib and Python 在PyPlot中绘制纪元列表的直方图,x轴按月 - 年 - Plot histogram of epoch list, x axis by month-year in PyPlot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM