Creating a histogram that sorts array values into bins, and shows the the frequency of items in the bins
For an array of values between 0 and 1, I want to create a histogram of 5 bins where bin one show the frequency(# of times) numbers between 0-0.2 show up in the array, bin 2 shows the frequency of numbers between 0.2-0.4, bin 3 is 0.4-0.6, bin 4: 0.6-0.8, bin 5 0.8-1.
import numpy as np
arr = np.array([0.5, 0.1, 0.05, 0.67, 0.8, 0.9, 1, 0.22, 0.25])
y, other_stuff = np.histogram(arr, bins=5)
x = range(0,5)
graph = plt.bar(x,height=y)
plt.show()
python arrays numpy matplotlib histogram
add a comment |
For an array of values between 0 and 1, I want to create a histogram of 5 bins where bin one show the frequency(# of times) numbers between 0-0.2 show up in the array, bin 2 shows the frequency of numbers between 0.2-0.4, bin 3 is 0.4-0.6, bin 4: 0.6-0.8, bin 5 0.8-1.
import numpy as np
arr = np.array([0.5, 0.1, 0.05, 0.67, 0.8, 0.9, 1, 0.22, 0.25])
y, other_stuff = np.histogram(arr, bins=5)
x = range(0,5)
graph = plt.bar(x,height=y)
plt.show()
python arrays numpy matplotlib histogram
What is your issue exactly? One issue is If 0 and 1 aren't in the data, it won't be the bins you expect. You'll want to explicitly define your bins (or range) since they will be determined by the min and max of your data.
– busybear
Nov 21 '18 at 1:15
In case you want to use a bar plot, see e.g. this answer.
– ImportanceOfBeingErnest
Nov 21 '18 at 2:05
add a comment |
For an array of values between 0 and 1, I want to create a histogram of 5 bins where bin one show the frequency(# of times) numbers between 0-0.2 show up in the array, bin 2 shows the frequency of numbers between 0.2-0.4, bin 3 is 0.4-0.6, bin 4: 0.6-0.8, bin 5 0.8-1.
import numpy as np
arr = np.array([0.5, 0.1, 0.05, 0.67, 0.8, 0.9, 1, 0.22, 0.25])
y, other_stuff = np.histogram(arr, bins=5)
x = range(0,5)
graph = plt.bar(x,height=y)
plt.show()
python arrays numpy matplotlib histogram
For an array of values between 0 and 1, I want to create a histogram of 5 bins where bin one show the frequency(# of times) numbers between 0-0.2 show up in the array, bin 2 shows the frequency of numbers between 0.2-0.4, bin 3 is 0.4-0.6, bin 4: 0.6-0.8, bin 5 0.8-1.
import numpy as np
arr = np.array([0.5, 0.1, 0.05, 0.67, 0.8, 0.9, 1, 0.22, 0.25])
y, other_stuff = np.histogram(arr, bins=5)
x = range(0,5)
graph = plt.bar(x,height=y)
plt.show()
python arrays numpy matplotlib histogram
python arrays numpy matplotlib histogram
edited Nov 21 '18 at 1:10
eyllanesc
76k103156
76k103156
asked Nov 21 '18 at 1:10
JacobJacob
37324
37324
What is your issue exactly? One issue is If 0 and 1 aren't in the data, it won't be the bins you expect. You'll want to explicitly define your bins (or range) since they will be determined by the min and max of your data.
– busybear
Nov 21 '18 at 1:15
In case you want to use a bar plot, see e.g. this answer.
– ImportanceOfBeingErnest
Nov 21 '18 at 2:05
add a comment |
What is your issue exactly? One issue is If 0 and 1 aren't in the data, it won't be the bins you expect. You'll want to explicitly define your bins (or range) since they will be determined by the min and max of your data.
– busybear
Nov 21 '18 at 1:15
In case you want to use a bar plot, see e.g. this answer.
– ImportanceOfBeingErnest
Nov 21 '18 at 2:05
What is your issue exactly? One issue is If 0 and 1 aren't in the data, it won't be the bins you expect. You'll want to explicitly define your bins (or range) since they will be determined by the min and max of your data.
– busybear
Nov 21 '18 at 1:15
What is your issue exactly? One issue is If 0 and 1 aren't in the data, it won't be the bins you expect. You'll want to explicitly define your bins (or range) since they will be determined by the min and max of your data.
– busybear
Nov 21 '18 at 1:15
In case you want to use a bar plot, see e.g. this answer.
– ImportanceOfBeingErnest
Nov 21 '18 at 2:05
In case you want to use a bar plot, see e.g. this answer.
– ImportanceOfBeingErnest
Nov 21 '18 at 2:05
add a comment |
2 Answers
2
active
oldest
votes
I think you are looking for matplotlib's hist
method.
With your sample array the code would look like:
plt.hist(arr, bins=np.linspace(0,1,6), ec='black')
I don't want to specify the number of bins because the number of bins is variable, in this example I only have 5 but I could 30, or 40, or 1000. The graph in this picture is an example of what I want, but I might have 5 graphs like this beside eachother, so ideally I want each array index of y to be a bar
– Jacob
Nov 21 '18 at 12:37
Ignore everything after the last comma in my last sentence
– Jacob
Nov 21 '18 at 12:48
add a comment |
Is it what you are after ?
import numpy
from numpy.random import random
import matplotlib.pyplot as plt
arr = random(100)
y, other_stuff = numpy.histogram(arr, bins=5)
x = numpy.linspace(0.1, 0.9, 5)
graph = plt.bar(x, height=y, width=0.2, edgecolor='black')
plt.show()
As pointed out in the comment below, the snippet above does not define the edges of the bins in the call to histogram(). The one below corrects for that.
import numpy
import matplotlib.pyplot as plt
arr = numpy.array([0.5, 0.1, 0.05, 0.67, 0.8, 0.9, 1, 0.22, 0.25])
y, other_stuff = numpy.histogram(arr, bins=numpy.linspace(0, 1, 6))
graph = plt.bar(numpy.linspace(0.1, 0.9, 5), height=y, width=0.2,
edgecolor='black')
plt.show()
1
Attention! this only looks like it's working correctly. And that only by coincidence. You will need to define the bins as in the other answer.
– ImportanceOfBeingErnest
Nov 21 '18 at 1:53
What do you mean ? The call to histogram() gives back 5 values as per bins=5, and the call to bar() specifies the location of the center of the bins (through x and the default keyword align='center') as well as their total width. I agree hist() does the job way better (and I have upvoted the answer), but I do not see why my answer would be only coincidentally working ? I just used and improved the snippet provided by the OP.
– Patol75
Nov 21 '18 at 4:42
1
If you take 100 random points between 0 and 1 and put them into 5 bins, the likelyhood is pretty high that those bins are close to[0,0.2), [0.2,0.4), etc
. However, it is an assumption which is in general not true, and will lead to wrong results if you took e.g. the data provided in the question. Your approach would then give this result, which is obviously different from @user7374610's result and you may easily count the values by hand to see that it's not correctly binning the values.
– ImportanceOfBeingErnest
Nov 21 '18 at 4:57
Ah, yes, makes sense now, thank you.
– Patol75
Nov 21 '18 at 5:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403923%2fcreating-a-histogram-that-sorts-array-values-into-bins-and-shows-the-the-freque%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you are looking for matplotlib's hist
method.
With your sample array the code would look like:
plt.hist(arr, bins=np.linspace(0,1,6), ec='black')
I don't want to specify the number of bins because the number of bins is variable, in this example I only have 5 but I could 30, or 40, or 1000. The graph in this picture is an example of what I want, but I might have 5 graphs like this beside eachother, so ideally I want each array index of y to be a bar
– Jacob
Nov 21 '18 at 12:37
Ignore everything after the last comma in my last sentence
– Jacob
Nov 21 '18 at 12:48
add a comment |
I think you are looking for matplotlib's hist
method.
With your sample array the code would look like:
plt.hist(arr, bins=np.linspace(0,1,6), ec='black')
I don't want to specify the number of bins because the number of bins is variable, in this example I only have 5 but I could 30, or 40, or 1000. The graph in this picture is an example of what I want, but I might have 5 graphs like this beside eachother, so ideally I want each array index of y to be a bar
– Jacob
Nov 21 '18 at 12:37
Ignore everything after the last comma in my last sentence
– Jacob
Nov 21 '18 at 12:48
add a comment |
I think you are looking for matplotlib's hist
method.
With your sample array the code would look like:
plt.hist(arr, bins=np.linspace(0,1,6), ec='black')
I think you are looking for matplotlib's hist
method.
With your sample array the code would look like:
plt.hist(arr, bins=np.linspace(0,1,6), ec='black')
answered Nov 21 '18 at 1:32
user7374610user7374610
6981522
6981522
I don't want to specify the number of bins because the number of bins is variable, in this example I only have 5 but I could 30, or 40, or 1000. The graph in this picture is an example of what I want, but I might have 5 graphs like this beside eachother, so ideally I want each array index of y to be a bar
– Jacob
Nov 21 '18 at 12:37
Ignore everything after the last comma in my last sentence
– Jacob
Nov 21 '18 at 12:48
add a comment |
I don't want to specify the number of bins because the number of bins is variable, in this example I only have 5 but I could 30, or 40, or 1000. The graph in this picture is an example of what I want, but I might have 5 graphs like this beside eachother, so ideally I want each array index of y to be a bar
– Jacob
Nov 21 '18 at 12:37
Ignore everything after the last comma in my last sentence
– Jacob
Nov 21 '18 at 12:48
I don't want to specify the number of bins because the number of bins is variable, in this example I only have 5 but I could 30, or 40, or 1000. The graph in this picture is an example of what I want, but I might have 5 graphs like this beside eachother, so ideally I want each array index of y to be a bar
– Jacob
Nov 21 '18 at 12:37
I don't want to specify the number of bins because the number of bins is variable, in this example I only have 5 but I could 30, or 40, or 1000. The graph in this picture is an example of what I want, but I might have 5 graphs like this beside eachother, so ideally I want each array index of y to be a bar
– Jacob
Nov 21 '18 at 12:37
Ignore everything after the last comma in my last sentence
– Jacob
Nov 21 '18 at 12:48
Ignore everything after the last comma in my last sentence
– Jacob
Nov 21 '18 at 12:48
add a comment |
Is it what you are after ?
import numpy
from numpy.random import random
import matplotlib.pyplot as plt
arr = random(100)
y, other_stuff = numpy.histogram(arr, bins=5)
x = numpy.linspace(0.1, 0.9, 5)
graph = plt.bar(x, height=y, width=0.2, edgecolor='black')
plt.show()
As pointed out in the comment below, the snippet above does not define the edges of the bins in the call to histogram(). The one below corrects for that.
import numpy
import matplotlib.pyplot as plt
arr = numpy.array([0.5, 0.1, 0.05, 0.67, 0.8, 0.9, 1, 0.22, 0.25])
y, other_stuff = numpy.histogram(arr, bins=numpy.linspace(0, 1, 6))
graph = plt.bar(numpy.linspace(0.1, 0.9, 5), height=y, width=0.2,
edgecolor='black')
plt.show()
1
Attention! this only looks like it's working correctly. And that only by coincidence. You will need to define the bins as in the other answer.
– ImportanceOfBeingErnest
Nov 21 '18 at 1:53
What do you mean ? The call to histogram() gives back 5 values as per bins=5, and the call to bar() specifies the location of the center of the bins (through x and the default keyword align='center') as well as their total width. I agree hist() does the job way better (and I have upvoted the answer), but I do not see why my answer would be only coincidentally working ? I just used and improved the snippet provided by the OP.
– Patol75
Nov 21 '18 at 4:42
1
If you take 100 random points between 0 and 1 and put them into 5 bins, the likelyhood is pretty high that those bins are close to[0,0.2), [0.2,0.4), etc
. However, it is an assumption which is in general not true, and will lead to wrong results if you took e.g. the data provided in the question. Your approach would then give this result, which is obviously different from @user7374610's result and you may easily count the values by hand to see that it's not correctly binning the values.
– ImportanceOfBeingErnest
Nov 21 '18 at 4:57
Ah, yes, makes sense now, thank you.
– Patol75
Nov 21 '18 at 5:00
add a comment |
Is it what you are after ?
import numpy
from numpy.random import random
import matplotlib.pyplot as plt
arr = random(100)
y, other_stuff = numpy.histogram(arr, bins=5)
x = numpy.linspace(0.1, 0.9, 5)
graph = plt.bar(x, height=y, width=0.2, edgecolor='black')
plt.show()
As pointed out in the comment below, the snippet above does not define the edges of the bins in the call to histogram(). The one below corrects for that.
import numpy
import matplotlib.pyplot as plt
arr = numpy.array([0.5, 0.1, 0.05, 0.67, 0.8, 0.9, 1, 0.22, 0.25])
y, other_stuff = numpy.histogram(arr, bins=numpy.linspace(0, 1, 6))
graph = plt.bar(numpy.linspace(0.1, 0.9, 5), height=y, width=0.2,
edgecolor='black')
plt.show()
1
Attention! this only looks like it's working correctly. And that only by coincidence. You will need to define the bins as in the other answer.
– ImportanceOfBeingErnest
Nov 21 '18 at 1:53
What do you mean ? The call to histogram() gives back 5 values as per bins=5, and the call to bar() specifies the location of the center of the bins (through x and the default keyword align='center') as well as their total width. I agree hist() does the job way better (and I have upvoted the answer), but I do not see why my answer would be only coincidentally working ? I just used and improved the snippet provided by the OP.
– Patol75
Nov 21 '18 at 4:42
1
If you take 100 random points between 0 and 1 and put them into 5 bins, the likelyhood is pretty high that those bins are close to[0,0.2), [0.2,0.4), etc
. However, it is an assumption which is in general not true, and will lead to wrong results if you took e.g. the data provided in the question. Your approach would then give this result, which is obviously different from @user7374610's result and you may easily count the values by hand to see that it's not correctly binning the values.
– ImportanceOfBeingErnest
Nov 21 '18 at 4:57
Ah, yes, makes sense now, thank you.
– Patol75
Nov 21 '18 at 5:00
add a comment |
Is it what you are after ?
import numpy
from numpy.random import random
import matplotlib.pyplot as plt
arr = random(100)
y, other_stuff = numpy.histogram(arr, bins=5)
x = numpy.linspace(0.1, 0.9, 5)
graph = plt.bar(x, height=y, width=0.2, edgecolor='black')
plt.show()
As pointed out in the comment below, the snippet above does not define the edges of the bins in the call to histogram(). The one below corrects for that.
import numpy
import matplotlib.pyplot as plt
arr = numpy.array([0.5, 0.1, 0.05, 0.67, 0.8, 0.9, 1, 0.22, 0.25])
y, other_stuff = numpy.histogram(arr, bins=numpy.linspace(0, 1, 6))
graph = plt.bar(numpy.linspace(0.1, 0.9, 5), height=y, width=0.2,
edgecolor='black')
plt.show()
Is it what you are after ?
import numpy
from numpy.random import random
import matplotlib.pyplot as plt
arr = random(100)
y, other_stuff = numpy.histogram(arr, bins=5)
x = numpy.linspace(0.1, 0.9, 5)
graph = plt.bar(x, height=y, width=0.2, edgecolor='black')
plt.show()
As pointed out in the comment below, the snippet above does not define the edges of the bins in the call to histogram(). The one below corrects for that.
import numpy
import matplotlib.pyplot as plt
arr = numpy.array([0.5, 0.1, 0.05, 0.67, 0.8, 0.9, 1, 0.22, 0.25])
y, other_stuff = numpy.histogram(arr, bins=numpy.linspace(0, 1, 6))
graph = plt.bar(numpy.linspace(0.1, 0.9, 5), height=y, width=0.2,
edgecolor='black')
plt.show()
edited Nov 21 '18 at 5:05
answered Nov 21 '18 at 1:33
Patol75Patol75
6236
6236
1
Attention! this only looks like it's working correctly. And that only by coincidence. You will need to define the bins as in the other answer.
– ImportanceOfBeingErnest
Nov 21 '18 at 1:53
What do you mean ? The call to histogram() gives back 5 values as per bins=5, and the call to bar() specifies the location of the center of the bins (through x and the default keyword align='center') as well as their total width. I agree hist() does the job way better (and I have upvoted the answer), but I do not see why my answer would be only coincidentally working ? I just used and improved the snippet provided by the OP.
– Patol75
Nov 21 '18 at 4:42
1
If you take 100 random points between 0 and 1 and put them into 5 bins, the likelyhood is pretty high that those bins are close to[0,0.2), [0.2,0.4), etc
. However, it is an assumption which is in general not true, and will lead to wrong results if you took e.g. the data provided in the question. Your approach would then give this result, which is obviously different from @user7374610's result and you may easily count the values by hand to see that it's not correctly binning the values.
– ImportanceOfBeingErnest
Nov 21 '18 at 4:57
Ah, yes, makes sense now, thank you.
– Patol75
Nov 21 '18 at 5:00
add a comment |
1
Attention! this only looks like it's working correctly. And that only by coincidence. You will need to define the bins as in the other answer.
– ImportanceOfBeingErnest
Nov 21 '18 at 1:53
What do you mean ? The call to histogram() gives back 5 values as per bins=5, and the call to bar() specifies the location of the center of the bins (through x and the default keyword align='center') as well as their total width. I agree hist() does the job way better (and I have upvoted the answer), but I do not see why my answer would be only coincidentally working ? I just used and improved the snippet provided by the OP.
– Patol75
Nov 21 '18 at 4:42
1
If you take 100 random points between 0 and 1 and put them into 5 bins, the likelyhood is pretty high that those bins are close to[0,0.2), [0.2,0.4), etc
. However, it is an assumption which is in general not true, and will lead to wrong results if you took e.g. the data provided in the question. Your approach would then give this result, which is obviously different from @user7374610's result and you may easily count the values by hand to see that it's not correctly binning the values.
– ImportanceOfBeingErnest
Nov 21 '18 at 4:57
Ah, yes, makes sense now, thank you.
– Patol75
Nov 21 '18 at 5:00
1
1
Attention! this only looks like it's working correctly. And that only by coincidence. You will need to define the bins as in the other answer.
– ImportanceOfBeingErnest
Nov 21 '18 at 1:53
Attention! this only looks like it's working correctly. And that only by coincidence. You will need to define the bins as in the other answer.
– ImportanceOfBeingErnest
Nov 21 '18 at 1:53
What do you mean ? The call to histogram() gives back 5 values as per bins=5, and the call to bar() specifies the location of the center of the bins (through x and the default keyword align='center') as well as their total width. I agree hist() does the job way better (and I have upvoted the answer), but I do not see why my answer would be only coincidentally working ? I just used and improved the snippet provided by the OP.
– Patol75
Nov 21 '18 at 4:42
What do you mean ? The call to histogram() gives back 5 values as per bins=5, and the call to bar() specifies the location of the center of the bins (through x and the default keyword align='center') as well as their total width. I agree hist() does the job way better (and I have upvoted the answer), but I do not see why my answer would be only coincidentally working ? I just used and improved the snippet provided by the OP.
– Patol75
Nov 21 '18 at 4:42
1
1
If you take 100 random points between 0 and 1 and put them into 5 bins, the likelyhood is pretty high that those bins are close to
[0,0.2), [0.2,0.4), etc
. However, it is an assumption which is in general not true, and will lead to wrong results if you took e.g. the data provided in the question. Your approach would then give this result, which is obviously different from @user7374610's result and you may easily count the values by hand to see that it's not correctly binning the values.– ImportanceOfBeingErnest
Nov 21 '18 at 4:57
If you take 100 random points between 0 and 1 and put them into 5 bins, the likelyhood is pretty high that those bins are close to
[0,0.2), [0.2,0.4), etc
. However, it is an assumption which is in general not true, and will lead to wrong results if you took e.g. the data provided in the question. Your approach would then give this result, which is obviously different from @user7374610's result and you may easily count the values by hand to see that it's not correctly binning the values.– ImportanceOfBeingErnest
Nov 21 '18 at 4:57
Ah, yes, makes sense now, thank you.
– Patol75
Nov 21 '18 at 5:00
Ah, yes, makes sense now, thank you.
– Patol75
Nov 21 '18 at 5:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403923%2fcreating-a-histogram-that-sorts-array-values-into-bins-and-shows-the-the-freque%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is your issue exactly? One issue is If 0 and 1 aren't in the data, it won't be the bins you expect. You'll want to explicitly define your bins (or range) since they will be determined by the min and max of your data.
– busybear
Nov 21 '18 at 1:15
In case you want to use a bar plot, see e.g. this answer.
– ImportanceOfBeingErnest
Nov 21 '18 at 2:05