简体   繁体   English

如何绘制直方图,yaxis 为“对应于每个 x bin 的 y 值的总和”,x 轴为 python 中 x 的 n 个 bin?

[英]How to plot a histogram with the yaxis as the "total of y values corresponding to each x bin" and x axis as n bins of x in python?

Say I have two arrays:假设我有两个数组:

x=np.random.uniform(0,10,100)
y=np.random.uniform(0,1,100)

I want to make n bins between xmin=0 and xmax=10.我想在 xmin=0 和 xmax=10 之间制作 n 个 bin。 For each y, there is a corresponding x which belongs to one of these n bins.对于每个 y,有一个对应的 x 属于这 n 个 bin 之一。 Say the value corresponding to each bin is initially zero.假设对应于每个 bin 的值最初为零。 What I wish to do is add each value of y to its corresponding x's bin and plot a histogram with the x-axis as xmin to xmax with n bins and the y axis as the total of all y values added to corresponding x's bins.我希望做的是将 y 的每个值添加到其对应的 x 的 bin 并绘制一个直方图,x 轴为 xmin 到 xmax,n 个 bin,y 轴为添加到相应 x 的 bin 的所有 y 值的总和。 How can one do this in python?如何在 python 中做到这一点?

First of all, i think its easier to use a bar plot instead of a histogram.首先,我认为使用条形图而不是直方图更容易。

import matplotlib.pyplot as plt
import numpy as np

xmax = 6600
xmin = 6400
x = np.random.uniform(xmin, xmax, 10000)
y = np.random.uniform(0, 1, 10000)
number_of_bins = 100

bins = [xmin]
step = (xmax - xmin)/number_of_bins
print("step = ", step)
for i in range(1, number_of_bins+1):
    bins.append(xmin + step*i)
print("bins = ", bins)

# time to create the sums for each bin
sums = [0] * number_of_bins

for i in range(len(x)):
    sums[int((x[i]-xmin)/step)] += y[i]
print(sums)

xbar = []
for i in range(number_of_bins):
 xbar.append(step/2 + xmin + step*i)
# now i have to create the x axis values for the barplot
plt.bar(xbar, sums, label="Bars")
plt.xlabel("X axis label")
plt.ylabel("Y axis label")
plt.legend()
plt.show()

Here is the new result这是新的结果

If you don't understand something please tell me, in order to explain it如果你不明白什么,请告诉我,以便解释

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python中从数据框绘制带有x和y轴的直方图 - How to plot histogram with x and y axis from dataframe in python 在x轴上绘制索引值并在y轴上绘制每个列值的频率的直方图 - Plot histogram with index values on x axis and frequencies of each column value on y axis y值的python直方图,每个bin中有相同数量的data(x) - python histogram of the y value with equal number of data(x) in each bin Python np.array,其中x个值分成x个容器,其中每个值都由1个容器表示 - Python np.array with x values into x bins, where each value is represented by 1 bin Matplotlib直方图来自x,y值,日期时间为月份 - Matplotlib histogram from x,y values with datetime months as bins Matplotlib:如何绘制一个x值的一维数组,且y轴与热图相对应? - Matplotlib: How does one plot a 1D array of x values with y-axis corresponding to a heatmap? 麻烦的直方图绘制图分隔图元并限制x轴值 - Trouble plotting histogram Bins are separated and the x-axis values are cramped 如何为直方图选择 x 和 y 轴的值? - How are the values for x and y axis chosen for histogram plots? 如何为只有 1 列和多行的数据框创建直方图(行值应绘制在 x 轴上,列值应绘制在 y 轴上) - How to create a histogram for a dataframe with just 1 column and multiple rows (row values should be plot on x axis and column values on y axis) 如图所示,x 轴为 plot 天,y 轴为时间,热图 plot 为 python 中的值? - How to plot day in x axis, time in y axis and a heatmap plot for the values in python as shown in the figure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM