将matplotlib直方图除以最大bin值

时间:2015-05-01 04:56:23

标签: python numpy matplotlib histogram

我想在同一个图上绘制多个直方图,我需要比较数据的传播。我想通过将每个直方图除以其最大值来做到这一点,因此所有分布都具有相同的比例。但是,matplotlib的直方图函数的工作方式,我还没有找到一个简单的方法来做到这一点。

这是因为

中的n
n, bins, patches = ax1.hist(y, bins = 20, histtype = 'step', color = 'k')

是每个bin中的计数数量,但我无法将其重新计算到hist,因为它会重新计算。

我尝试过规范和密度函数,但是这些函数规范了分布区域,而不是分布的高度。我可以复制n然后使用bin输出重复bin边缘,但这很乏味。当然,hist函数必须允许将bin值除以常数?

示例代码如下,证明了问题。

y1 = np.random.randn(100)
y2 = 2*np.random.randn(50)
x1 = np.linspace(1,101,100)
x2 = np.linspace(1,51,50)
gs = plt.GridSpec(1,2, wspace = 0, width_ratios = [3,1])
ax = plt.subplot(gs[0])
ax1 = plt.subplot(gs[1])
ax1.yaxis.set_ticklabels([])   # remove the major ticks

ax.scatter(x1, y1, marker='+',color = 'k')#, c=SNR, cmap=plt.cm.Greys)
ax.scatter(x2, y2, marker='o',color = 'k')#, c=SNR, cmap=plt.cm.Greys)
n1, bins1, patches1 = ax1.hist(y1, bins = 20, histtype = 'step', color = 'k',linewidth = 2, orientation = 'horizontal')
n2, bins2, patched2 = ax1.hist(y2, bins = 20, histtype = 'step', linestyle = 'dashed', color = 'k', orientation = 'horizontal')

Example output. I want the max bins of the dashed and dotted lines to be 1.

4 个答案:

答案 0 :(得分:1)

我不知道默认情况下matplotlib是否允许这种规范化,但是我写了一个函数来自己做。

从plt.hist获取nbins的输出(如上所示)然后通过下面的函数传递它。

def hist_norm_height(n,bins,const):
    ''' Function to normalise bin height by a constant. 
        Needs n and bins from np.histogram or ax.hist.'''

    n = np.repeat(n,2)
    n = float32(n) / const
    new_bins = [bins[0]]
    new_bins.extend(np.repeat(bins[1:],2))
    return n,new_bins[:-1]

要立即绘制(我喜欢步骤直方图),您将它传递给plt.step。

例如plt.step(new_bins,n)。这将为您提供一个高度标准化的高度直方图。

答案 1 :(得分:0)

您可以将参数bins指定为值列表。使用np.arange()np.linspace()生成值。 http://matplotlib.org/api/axes_api.html?highlight=hist#matplotlib.axes.Axes.hist

答案 2 :(得分:0)

为比较设置了略有不同的方法。可以适应步骤风格:

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np

y = []
y.append(np.random.normal(2, 2, size=40))
y.append(np.random.normal(3, 1.5, size=40))
y.append(np.random.normal(4,4,size=40))
ls = ['dashed','dotted','solid']

fig, (ax1, ax2, ax3) = plt.subplots(ncols=3)
for l, data in zip(ls, y):
    n, b, p = ax1.hist(data, normed=False,
                       #histtype='step', #step's too much of a pain to get the bins
                       #color='k', linestyle=l,
                       alpha=0.2
                       )
    ax2.hist(data, normed=True,
             #histtype = 'step', color='k', linestyle=l,
             alpha=0.2
             )

    n, b, p = ax3.hist(data, normed=False,
                       #histtype='step', #step's too much of a pain to get the bins
                       #color='k', linestyle=l,
                       alpha=0.2
                       )
    high = float(max([r.get_height() for r in p]))
    for r in p:
        r.set_height(r.get_height()/high)
        ax3.add_patch(r)
    ax3.set_ylim(0,1)

ax1.set_title('hist')
ax2.set_title('area==1')
ax3.set_title('fix height')
plt.show()

一对输出:

enter image description here

enter image description here

enter image description here

答案 3 :(得分:0)

这可以使用numpy来获得先验直方图值,然后用bar plot绘制它们。

import numpy as np
import matplotlib.pyplot as plt

# Define random data and number of bins to use
x = np.random.randn(1000)
bins = 10

plt.figure()
# Obtain the bin values and edges using numpy
hist, bin_edges = np.histogram(x, bins=bins, density=True)
# Plot bars with the proper positioning, height, and width.
plt.bar(
    (bin_edges[1:] + bin_edges[:-1]) * .5, hist / hist.max(),
    width=(bin_edges[1] - bin_edges[0]), color="blue")

plt.show()

enter image description here