Reputation: 27
Dear Computer Scientist Family
I was wondering is it possible to assign any value I give to a certain bin, in a histogram. If you notice in my code it will produce a histogram with 2 bins populated with a quantity of 1.
# -*- coding: utf-8 -*-
"""
Created on Sat May 9 20:23:51 2020
@author: DeAngelo
"""
import matplotlib.pyplot as plt
import numpy as np
import math
fig,ax = plt.subplots(1,1)
a = np.array([11,75])
ax.hist(a, bins = [0,25,50,75,100])
ax.set_title("histogram of result")
ax.set_xticks([0,25,50,75,100])
ax.set_xlabel('marks')
ax.set_ylabel('no. of students')
plt.show()
First could you theoretically tell the computer that you want to place the value that was assigned in the 75-100 bin. And move it to the 0-25 bin. Meaning I now would now have 2 entries in the 0-25 bin. But my array would still be a=[11,75]
Also another example would be say I have an array 'b=np.array[3]' and I plotted this on my histogram. I know that this would be given the bin 0-25, but could I tell the computer to put this in the 75-100 bin?
If so how?
Second what about the average I know you can use np.mean(a)
to calculate the average. But say I wanted to put that value in the bin that corresponds to 75-100. Would that be possible?
I looked at this code How to assign a number to a value falling in a certain bin , but that was in Ancient Egyptian hieroglyphics and unfortunately my degree is in physics and not that.
If you can help me with this it would mean a lot to me.<3
Upvotes: 0
Views: 3197
Reputation: 10545
Yes, this is possible. You can catch the return value of the histogram function by assigning it to a variable:
h = ax.hist(a, bins = [0, 25, 50, 75, 100])
h
(array([1., 0., 0., 1.]),
array([ 0, 25, 50, 75, 100]),
<a list of 4 Patch objects>)
As the documentation says, this "is a tuple (n, bins, patches)". We are only interested in the counts and bins, so let's assign them to individual variables:
counts, bins, _ = h
Now you can manipulate the counts in any way you like, e.g. move one count from the fourth to the first bin:
counts[3] -= 1
counts[0] += 1
counts
array([2., 0., 0., 0.])
We can turn these data into a histogram plot as shown in the documentation under the weights
parameter:
plt.hist(bins[:-1], bins, weights=counts);
Upvotes: 1
Reputation: 2002
A histogram is just represented as a bar chart, so you could manipulate the bar values. Here you can precalculate the histogram and plot it as a bar chart:
import matplotlib.pyplot as plt
import numpy as np
import math
a = np.array([11,75])
# calculate histogram values
vals, bins = np.histogram(a, bins = [0,25,50,75,100])
width = np.ediff1d(bins)
fig,ax = plt.subplots(1,1)
# plot histogram values as bar chart
ax.bar(bins[:-1] + width/2, vals, width)
ax.set_title("histogram of result")
ax.set_xticks([0,25,50,75,100])
ax.set_xlabel('marks')
ax.set_ylabel('no. of students')
plt.show()
This gives you your example. You can now however manipulate the bar values before plotting should you so wish:
# the bin values
vals
>>> array([1, 0, 0, 1])
# bin edges
bins
>>> array([ 0, 25, 50, 75, 100])
# do manipulation -> remove one count from 75-100 bin and put in 0-25 bin
vals[-1] -= 1
vals[0] += 1
# plot new graph
fig,ax = plt.subplots(1,1)
# plot histogram values as bar chart
ax.bar(bins[:-1] + width/2, vals, width)
ax.set_title("histogram of result")
ax.set_xticks([0,25,50,75,100])
ax.set_xlabel('marks')
ax.set_ylabel('no. of students')
plt.show()
I have to comment, what are your reasons for doing this? In your example you want to calculate the average and put it in the wrong bin. You can certainly do that as I have shown above, but I'm not sure what it means at that point?
Upvotes: 1