[英]How can I change the values on Y axis of Histogram plot in Python
I have data in the CSV file.我在 CSV 文件中有数据。 I am trying to plot a histogram using matplotlib.
我正在尝试使用 matplotlib 绘制直方图。 Here is the code that I am trying.
这是我正在尝试的代码。
data.hist(bins=10)
plt.ylabel('Frequency')
plt.xlabel('Data')
plt.show()
This is the plot that I get.这是我得到的情节。 Now using the same code, I need to create a normalized histogram that shows the probability distribution of the data.
现在使用相同的代码,我需要创建一个标准化的直方图来显示数据的概率分布。 But now on the y-axis, instead of plotting the number of data points that fall in each bin, you will plot the number of data points in that data bin divided by the total number of data points.
但是现在在 y 轴上,不是绘制落在每个 bin 中的数据点数,而是绘制该数据 bin 中的数据点数除以数据点总数。
How should I do it?我该怎么做?
Pandas' histogram adds some functionality to the underlying pyplot.hist()
. Pandas 的直方图为底层
pyplot.hist()
添加了一些功能。 Many of the parameters are passed through.许多参数被传递。 One of them is
density=
.其中之一是
density=
。
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
data = pd.DataFrame(np.random.uniform(258.1, 262.3, 20))
data.hist(bins=10, density=True)
plt.ylabel('Density')
plt.xlabel('Data')
plt.show()
A related library, seaborn, has a command to create a density histogram together with a kde curve as an approximation of the probability distribution.一个相关的库 seaborn 有一个命令来创建一个密度直方图和一条kde 曲线作为概率分布的近似值。
import seaborn as sns
sns.distplot(data, bins=10)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.