简体   繁体   English

如何在 Python 中更改直方图 Y 轴上的值

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

相关问题 如何在Python中打印直方图的y轴值? - How to print y-axis values of histogram plot in Python? 如何在Python中从数据框绘制带有x和y轴的直方图 - How to plot histogram with x and y axis from dataframe in python 如何创建 y 轴上的值列表,而不必在 Python 中创建 plot 图形? - How can I create a list of the values on the y-axis without having to plot a graph in Python? 如何在 Python 图中以弧度设置 y 轴? - How can I set the y axis in radians in a Python plot? 如何在 Python 中将直方图的 y 轴值乘以固定数字 - How to multiply the y-axis values of a histogram by a fixed number in Python 如何更改 seaborn 直方图中的 y 轴限制? - How do I change y-axis limits in seaborn histogram? 如何绘制直方图,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? 多直方图上的 Python 更改轴 - Python Change axis on Multi Histogram plot 如何在 python 中以像素数为 x 轴,灰度颜色为 y 轴 plot 图形? - How can I plot the figure with the number of pixel as a x-axis and the grayscale color as a y-axis in python? Python matplotlib 如何更改“直方图”的 y 值 - Python matplotlib how to change y-values of "histogram"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM