Create a Histogram for Weighted Values

Create a histogram for weighted values

Package plotrix has a function weighted.hist which does what you want:

w<-seq(1,1000)
v<-sort(runif(1000))
weighted.hist(v, w)

Example of <code>weighted.hist</code>

histogram for weighted data using bokeh

Basically, you pass the weights to your numpy's histogram function.

import numpy as np

from bokeh.layouts import gridplot
from bokeh.plotting import figure, show, output_file

p1 = figure(title="Normal Distribution (μ=50, σ=20) and normal weights",tools="save",
background_fill_color="#E8DDCB")

measured = np.random.normal(50, 20, 1000)

myweights = np.random.normal(0, 1, 1000)
hist, edges = np.histogram(measured, density=True, bins=10,weights=myweights)

x = np.linspace(-2, 2, 1000)
p1.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:],
fill_color="#036564", line_color="#033649")
p1.legend.location = "top_left"
p1.xaxis.axis_label = 'x'
p1.yaxis.axis_label = 'Pr(x)'

show(p1)

Building a weighted histogram using two binary files

The mistake is that total = np.zeros(nbins, np.int64) is assigning an integer type to each of the elements of the array total. Given that subtotal does not contain the count number in a weighted histogram but a float-type, total should also be of type float.

Weighted bins in a distribution hist plot

You want to use the weights kwarg (see numpy docs) which is passed through ax.hist (see).

Something like

fig, ax = plt.subplots()
ax.hist(num_sold, bins, weights=num_sold)


Related Topics



Leave a reply



Submit