如何使Matplotlib/Pandas条形图看起来像历史图? [英] How to make matplotlib/pandas bar chart look like hist chart?

查看:83
本文介绍了如何使Matplotlib/Pandas条形图看起来像历史图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pandas.Series中提供一些数据rv

  1. 调用 hist 直接在数据上进行绘图

  1. Calling hist directly on the data to plot

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

Calculating the histogram results (with numpy.histogram) then plotting with 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()绘制

hist() Plotting

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)

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

这种情况的用例是仅保存直方图数据以供使用和以后绘制(通常其大小小于原始数据).

The use case for this is needing to save only the histogrammed data to use and plot later (it is typically smaller in size than the original data).

推荐答案

条形图差异

要获得类似于hist图的bar图,需要对bar的默认行为进行一些操作.

Bar plotting differences

Obtaining a bar plot that looks like the hist plot requires some manipulating of default behavior for bar.

  1. 强制bar通过传递x(hist.index)和y(hist.values)来使用实际的x数据绘制范围. 默认的bar行为是在任意范围内绘制y数据并将x数据作为标签. /li>
  2. width参数设置为与x数据的实际步长相关(默认为0.8)
  3. align参数设置为'center'.
  4. 手动设置轴图例.
  1. Force bar to use actual x data for plotting range by passing both x (hist.index) and y (hist.values). The default bar behavior is to plot the y data against an arbitrary range and put the x data as the label.
  2. Set the width parameter to be related to actual step size of x data (The default is 0.8)
  3. Set the align parameter to 'center'.
  4. Manually set the axis legend.

这些更改需要通过 matplotlib bar() //pandas.pydata.org"rel =" nofollow noreferrer> pandas

These changes need to be made via matplotlib's bar() called on the axis (ax) instead of pandas's bar() called on the data (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'])



这篇关于如何使Matplotlib/Pandas条形图看起来像历史图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆