python - 如何使 matplotlib/pandas 条形图看起来像直方图?

标签 python numpy pandas matplotlib plot

绘制 barhist 之间的差异

给定一个 pandas.Series 中的一些数据,rv,有区别

  1. 调用 hist直接在数据上绘制

  2. 计算直方图结果(使用 numpy.histogram )然后使用 bar 绘图

示例数据生成

%matplotlib inline

import numpy as np
import pandas as pd
import scipy.stats as stats
import matplotlib
matplotlib.rcParams['figure.figsize'] = (12.0, 8.0)
matplotlib.style.use('ggplot')

# Setup size and distribution
size = 50000
distribution = stats.norm()

# Create random data
rv = pd.Series(distribution.rvs(size=size))
# Get sane start and end points of distribution
start = distribution.ppf(0.01)
end = distribution.ppf(0.99)

# Build PDF and turn into pandas Series
x = np.linspace(start, end, size)
y = distribution.pdf(x)
pdf = pd.Series(y, x)

# Get histogram of random data
y, x = np.histogram(rv, bins=50, normed=True)
# Correct bin edge placement
x = [(a+x[i+1])/2.0 for i,a in enumerate(x[0:-1])]
hist = pd.Series(y, x)

hist() 绘图

ax = pdf.plot(lw=2, label='PDF', legend=True)
rv.plot(kind='hist', bins=50, normed=True, alpha=0.5, label='Random Samples', legend=True, ax=ax)

hist plotting

bar() 绘图

ax = pdf.plot(lw=2, label='PDF', legend=True)
hist.plot(kind='bar', alpha=0.5, label='Random Samples', legend=True, ax=ax)

bar plotting

如何使 bar 图看起来像 hist 图?

此用例只需要保存直方图数据以供稍后使用和绘制(它的大小通常小于原始数据)。

最佳答案

条形图差异

获得一个看起来像 hist 图的 bar 图需要对 bar 的默认行为进行一些操作。

  1. 通过传递 x (hist.index) 和 y (hist.values),强制 bar 使用实际的 x 数据绘制范围. The default bar behavior is to plot the y data against an arbitrary range and put the x data as the label .
  2. 设置width参数与x数据的实际步长相关(默认为0.8)
  3. align 参数设置为 'center'
  4. 手动设置轴图例。

这些更改需要通过 matplotlib 进行的 bar()在轴上调用 (ax) 而不是 pandasbar()调用数据 (hist)。

示例绘图

%matplotlib inline

import numpy as np
import pandas as pd
import scipy.stats as stats
import matplotlib
matplotlib.rcParams['figure.figsize'] = (12.0, 8.0)
matplotlib.style.use('ggplot')

# Setup size and distribution
size = 50000
distribution = stats.norm()

# Create random data
rv = pd.Series(distribution.rvs(size=size))
# Get sane start and end points of distribution
start = distribution.ppf(0.01)
end = distribution.ppf(0.99)

# Build PDF and turn into pandas Series
x = np.linspace(start, end, size)
y = distribution.pdf(x)
pdf = pd.Series(y, x)

# Get histogram of random data
y, x = np.histogram(rv, bins=50, normed=True)
# Correct bin edge placement
x = [(a+x[i+1])/2.0 for i,a in enumerate(x[0:-1])]
hist = pd.Series(y, x)

# Plot previously histogrammed data
ax = pdf.plot(lw=2, label='PDF', legend=True)
w = abs(hist.index[1]) - abs(hist.index[0])
ax.bar(hist.index, hist.values, width=w, alpha=0.5, align='center')
ax.legend(['PDF', 'Random Samples'])

histogrammed plot

关于python - 如何使 matplotlib/pandas 条形图看起来像直方图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37548732/

相关文章:

python - 如何在 pymongo 中存储和检索 pandas 数据框列表?

python - 如何在 python 套接字中使用浏览器作为客户端?

python - 凯撒密码Python

python - Python 3 中的 CSV .count?

python - 尝试创建 numpy 矩阵时出现内存错误

python - 从文件中读取和存储任意字节长度的整数

python - 使用 Python 的 'fabric' 运行 docker-compose up

python - Tensorflow (GPU) 与 Numpy

python - 将列移到左侧 Pandas Dataframe

python - 映射 pandas DataFrame 索引