去除plt.bar中触摸条的颜色

问题描述 投票:0回答:1

所以我有两个问题:

  1. 我的直方图的密度在设置时没有达到1 我的代码中的密度 = True 。请参阅以下代码:
 plt.hist(my_data, 25, histtype = 'step', edgecolor ='skyblue', alpha = 0.7, density = True, label = '17GHz',)
plt.title('Horn 4', fontsize = 'x-large')
plt.xlabel('Percentage of zeroes in the file [%]')
plt.ylabel('Counts')
plt.xticks(rotation = 45)  # Rotate x-axis labels for better readability
plt.xticks(np.arange(0, 110, 5)) # Adjust the number of x-axis ticks # start, stop, step
plt.legend()

和情节

enter image description here

首先,为什么会这样?有没有办法来解决这个问题 ?根据这个堆栈主题(numpy直方图累积密度总和不等于1),我使用了

plt.bar
,这导致了我的第二个问题

  1. 我无法使用
    plt.bar
    np.hist
    进行相同的格式...如何 我是否要删除触摸条的边缘颜色,使其看起来像上图?

enter image description here

plt.bar(bins_h4_19[:-1], probability_densities_h4_19, width = np.diff(bins_h4_19), align='edge', edgecolor='skyblue',   color ='white', label = '19 GHz')
plt.bar(bins_h4_17[:-1], probability_densities_h4_17, width = np.diff(bins_h4_17), align='edge', edgecolor='purple',   color = '#FF000000', label = '17 GHz')
plt.xlabel('Percentage of zeroes in the file [%]')
plt.ylabel('Probability Density')
plt.title('Horn 4')
plt.show()
matplotlib histogram
1个回答
0
投票

各个 bin 的比例之和将为 1。密度是比例除以 bin 宽度(每单位 bin 宽度的样本比例)。密度 = (计数 / 总计数) / bin_宽度。括号中的部分是该箱中样本的比例(我认为您正在考虑这一点),完整的方程考虑了单位或宽度。

enter image description here

让它们相加为一:

print(
    np.sum(densities0 * bin_width0),
    np.sum(densities1 * bin_width1)
)

#result: 1.0 1.0

可重现的示例:

import numpy as np
from matplotlib import pyplot as plt

#Data for testing
np.random.seed(2)
data = np.random.uniform(0, 1, size=(500, 2))
data[-50:, 1] = np.random.uniform(0, 0.2, size=(50))
data[-50:, 0] = np.random.uniform(0.8, 1, size=(50))
n_bins = 25

densities0, bins0, ax = plt.hist(data[:, 0], n_bins, histtype='step',
                                  edgecolor='skyblue', linewidth=3,
                                  density=True, label='17GHz')

densities1, bins1, _ = plt.hist(data[:, 1], n_bins, histtype='step',
                                edgecolor='tab:brown', linewidth=3,
                                density=True, label='19GHz')
plt.legend(ncols=2)

#Formatting
plt.gcf().set_size_inches(8, 3)
plt.xlabel('proportion of files')
plt.ylabel('density')
plt.gca().spines[['right', 'top']].set_visible(False)
plt.gca().spines['bottom'].set_bounds(0, 1)
plt.gca().spines['left'].set_bounds(0, 1.75)

#Densities come to 1
bin_width0 = np.diff(bins0)[0]
bin_width1 = np.diff(bins1)[0]

print(
    np.sum(densities0 * bin_width0),
    np.sum(densities1 * bin_width1)
)
© www.soinside.com 2019 - 2024. All rights reserved.