[英]Error in plotting of frequency histogram from csv data
我正在使用 python3 上带有 pandas 模块的 csv 文件。 .csv 文件由 5 列组成:职位、公司名称、职位描述、评论数量、职位位置; 我想绘制频率直方图,在那里我只选择包含“机械工程师”一词的工作,并找到“机械工程师”工作最常见的 5 个位置的频率。
所以,我定义了一个变量 engloc 来存储所有“机械工程师”的工作。
engloc=df[df.position.str.contains('mechanical engineer|mechanical engineering', flags=re.IGNORECASE, regex=True)].location
并使用我在网上找到的代码使用 matplotlib 绘制了直方图
x = np.random.normal(size = 1000)
plt.hist(engloc, bins=50)
plt.gca().set(title='Frequency Histogram ', ylabel='Frequency');
但它是这样打印的
我如何绘制一个合适的频率直方图,它只使用 5 个最常见的位置来绘制包含“机械工程师”字样的工作,而不是将所有位置都放在图中?
以下几行应该可以帮助您处理数值数据:
import numpy as np
counts_, bins_ = np.histogram(englog.values)
filtered = [(c,b) for (c,b) in zip(counts_,bins_) if counts_>=5]
counts, bins = list(zip(*filtered))
plt.hist(bins[:-1], bins, weights=counts)
对于字符串类型尝试:
from collections import Counter
coords, counts = list(zip(*Counter(englog.values).most_common(5)))
plt.bar(coords, counts)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.