发布于 2021-04-25 08:47:27
直方图中的bins参数指定数据将均匀分布到的柱状图的数量。
假设你有一个这样的工资样本数据:
import pandas as pd
sample_dataframe = pd.DataFrame({'name':['joe','jill','martin','emily','frank','john','sue','sally','sam'],
'salary':[105324,65002,98314,24480,55000,62000,75000,79000,32000]})
#output:
name salary
0 joe 105324
1 jill 65002
2 martin 98314
3 emily 24480
4 frank 55000
5 john 62000
6 sue 75000
7 sally 79000
8 sam 32000如果你想绘制一个直方图,其中工资将分布在10个柱状图中,并且你想坚持使用你的函数,你可以这样做:
import matplotlib.pyplot as plt
def histograms(t):
plt.hist(t.salary, bins = 10, color = 'orange', edgecolor = 'black')
plt.xlabel('Salary')
plt.ylabel('Count')
plt.show()
histograms(sample_dataframe)

如果希望x轴记号反映10个存储箱的边界,可以添加以下行:
import numpy as np
plt.xticks(np.linspace(min(t.salary), max(t.salary), 11), rotation = 45)

最后,要将y-ticks显示为整数,请添加以下行:
from matplotlib.ticker import MaxNLocator
plt.gca().yaxis.set_major_locator(MaxNLocator(integer=True))最后一个函数如下所示:
def histograms(t):
plt.hist(t.salary, bins = 10, color = 'orange', edgecolor = 'black')
plt.xlabel('Salary')
plt.ylabel('Count')
plt.gca().yaxis.set_major_locator(MaxNLocator(integer=True))
plt.xticks(np.linspace(min(t.salary), max(t.salary), 11), rotation = 45)
plt.show()

发布于 2021-04-25 19:41:42
这就是你要找的东西吗?
import matplotlib.pyplot as plt
def histograms(t):
_min = min(t['salary'])
_max = max(t['salary'])
bins = int((_max - _min) / 1000) # dividing the salary range in bins of 1000 each
plt.hist(t['salary'], bins = bins)
histograms(df)https://stackoverflow.com/questions/67246261
复制相似问题