以 y 轴为百分比绘制直方图(使用 FuncFormatter?) [英] Plot an histogram with y-axis as percentage (using FuncFormatter?)

查看:79
本文介绍了以 y 轴为百分比绘制直方图(使用 FuncFormatter?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据列表,其中的数字在1000到20000之间.

data = [1000, 1000, 5000, 3000, 4000, 16000, 2000]

当我使用 hist()函数绘制直方图时,y轴表示bin中值出现的次数.除了出现的次数,我想知道出现的百分比.

以上情节的代码:

f, ax = plt.subplots(1, 1, figsize=(10,5))ax.hist(data, bins = len(list(set(data))))

我一直在看

实际期望的输出(具有全局变量的方法):

解决方案

其他答案似乎非常复杂.通过使用 1/n 对数据进行加权,可以很容易地生成显示比例而不是绝对数量的直方图,其中 n 是数据点的数量.

然后可以

在这里,我们看到7个值中的三个在第一个容器中,即3/7 = 43%.

I have a list of data in which the numbers are between 1000 and 20 000.

data = [1000, 1000, 5000, 3000, 4000, 16000, 2000]

When I plot a histogram using the hist() function, the y-axis represents the number of occurrences of the values within a bin. Instead of the number of occurrences, I would like to have the percentage of occurrences.

Code for the above plot:

f, ax = plt.subplots(1, 1, figsize=(10,5))
ax.hist(data, bins = len(list(set(data))))

I've been looking at this post which describes an example using FuncFormatter but I can't figure out how to adapt it to my problem. Some help and guidance would be welcome :)

EDIT: Main issue with the to_percent(y, position) function used by the FuncFormatter. The y corresponds to one given value on the y-axis I guess. I need to divide this value by the total number of elements which I apparently can' t pass to the function...

EDIT 2: Current solution I dislike because of the use of a global variable:

def to_percent(y, position):
    # Ignore the passed in position. This has the effect of scaling the default
    # tick locations.
    global n

    s = str(round(100 * y / n, 3))
    print (y)

    # The percent symbol needs escaping in latex
    if matplotlib.rcParams['text.usetex'] is True:
        return s + r'$\%$'
    else:
        return s + '%'

def plotting_hist(folder, output):
    global n

    data = list()
    # Do stuff to create data from folder

    n = len(data)
    f, ax = plt.subplots(1, 1, figsize=(10,5))
    ax.hist(data, bins = len(list(set(data))), rwidth = 1)

    formatter = FuncFormatter(to_percent)
    plt.gca().yaxis.set_major_formatter(formatter)

    plt.savefig("{}.png".format(output), dpi=500)

EDIT 3: Method with density = True

Actual desired output (method with global variable):

解决方案

Other answers seem utterly complicated. A histogram which shows the proportion instead of the absolute amount can easily produced by weighting the data with 1/n, where n is the number of datapoints.

Then a PercentFormatter can be used to show the proportion (e.g. 0.45) as percentage (45%).

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter

data = [1000, 1000, 5000, 3000, 4000, 16000, 2000]

plt.hist(data, weights=np.ones(len(data)) / len(data))

plt.gca().yaxis.set_major_formatter(PercentFormatter(1))
plt.show()

Here we see that three of the 7 values are in the first bin, i.e. 3/7=43%.

这篇关于以 y 轴为百分比绘制直方图(使用 FuncFormatter?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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