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