【问题标题】:Plot two histograms on the same graph and have their columns sum to 100在同一张图上绘制两个直方图,并让它们的列总和为 100
【发布时间】:2016-06-23 00:42:14
【问题描述】:

我想在同一个直方图上绘制两组不同的大小。然而,由于一组具有约 330,000 个值,而另一组具有约 16,000 个值,因此它们的频率直方图很难比较。我想绘制一个比较两组的直方图,使得 y 轴是该箱中出现的百分比。我下面的代码接近于此,除了各个 bin 值的总和为 1.0,直方图的积分总和为 1.0(这是因为 normed=True 参数)。

我怎样才能实现我的目标?我已经尝试手动计算频率百分比并使用 plt.bar() 但不是覆盖图,而是并排比较图。我想保持 alpha=0.5

的效果
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

if plt.get_fignums():
    plt.close('all')

electric = pd.read_csv('electric.tsv', sep='\t')
gas = pd.read_csv('gas.tsv', sep='\t')

electric_df = pd.DataFrame(electric)
gas_df = pd.DataFrame(ngma_nonheat)

electric = electric_df['avg_daily']*30
gas = gas_df['avg_daily']*30


## Create a plot for NGMA gas usage
plt.figure("Usage Comparison")

weights_electric = np.ones_like(electric)/float(len(electric))
weights_gas = np.ones_like(gas)/float(len(gas))

bins=np.linspace(0, 200, num=50)

n, bins, rectangles = plt.hist(electric, bins, alpha=0.5, label='electric usage', normed=True, weights=weights_electric)
plt.hist(gas, bins, alpha=0.5, label='gas usage', normed=True, weights=weights_gas)

plt.legend(loc='upper right')
plt.xlabel('Average 30 day use in therms')
plt.ylabel('% of customers')
plt.title('NGMA Customer Usage Comparison')
plt.show()

【问题讨论】:

  • 如果您使用的是weights,在这种情况下您可以省略normed。如果您希望总和为 100 而不是 1,请将权重乘以 100。通过省略 normed/density,并使用正确的 weights,您将获得缩放的 bin 计数,而不是值积分为 1。

标签: python matplotlib plot histogram normalize


【解决方案1】:

在这种情况下,听起来您不想要 normed/density kwarg。您已经在使用weights。如果你将你的权重乘以 100 并忽略 normed=True 选项,你应该得到你想要的。

例如:

import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1)

x = np.random.normal(5, 2, 10000)
y = np.random.normal(2, 1, 3000000)

xweights = 100 * np.ones_like(x) / x.size
yweights = 100 * np.ones_like(y) / y.size

fig, ax = plt.subplots()
ax.hist(x, weights=xweights, color='lightblue', alpha=0.5)
ax.hist(y, weights=yweights, color='salmon', alpha=0.5)

ax.set(title='Histogram Comparison', ylabel='% of Dataset in Bin')
ax.margins(0.05)
ax.set_ylim(bottom=0)
plt.show()

另一方面,您当前正在执行的操作(weights 和 normed)将导致(注意 y 轴上的单位):

import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1)

x = np.random.normal(5, 2, 10000)
y = np.random.normal(2, 1, 3000000)

xweights = 100 * np.ones_like(x) / x.size
yweights = 100 * np.ones_like(y) / y.size

fig, ax = plt.subplots()
ax.hist(x, weights=xweights, color='lightblue', alpha=0.5, normed=True)
ax.hist(y, weights=yweights, color='salmon', alpha=0.5, normed=True)

ax.set(title='Histogram Comparison', ylabel='% of Dataset in Bin')
ax.margins(0.05)
ax.set_ylim(bottom=0)
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-10
    • 2012-08-21
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    相关资源
    最近更新 更多