Matplotlib histogram bar width
I am using matplotlib histogram histtype='bar' to plot four datasets together. What it automatically does is that it changes the width of the bar of each dataset relative to the size of the dataset. I want to have equal bar width for all four datasets.
[n, bins, patches = plt.hist(
[np.sort(x2), np.sort(x3), np.sort(x4), np.sort(x5)],
bins=np.logspace(np.log10(8e-8), np.log10(1), 7),
histtype='bar',
weights=[w2,w3,w4,w5],
label=[
'$Delta {{chi}^2} <40$',
'$u_0 < 0.045$',
'${s}_{fitted} >5$',
'Multi-peaked'
]
)][1]

python matplotlib
|
show 3 more comments
I am using matplotlib histogram histtype='bar' to plot four datasets together. What it automatically does is that it changes the width of the bar of each dataset relative to the size of the dataset. I want to have equal bar width for all four datasets.
[n, bins, patches = plt.hist(
[np.sort(x2), np.sort(x3), np.sort(x4), np.sort(x5)],
bins=np.logspace(np.log10(8e-8), np.log10(1), 7),
histtype='bar',
weights=[w2,w3,w4,w5],
label=[
'$Delta {{chi}^2} <40$',
'$u_0 < 0.045$',
'${s}_{fitted} >5$',
'Multi-peaked'
]
)][1]

python matplotlib
1
No, you set the bar width yourself via thebinsargument.
– ImportanceOfBeingErnest
Nov 28 '18 at 1:02
1
Thanks, but I’m not sure if I understand your point. My understanding is that the bins are the same for all datasets, so how does that determine the widths?
– somayeh khakpash
Nov 28 '18 at 2:27
2
Oh, sorry, this kind of plot is really misleading. You kind of want a categorical histogram? I think you might decide to calculate the histogram first, then plot a barplot with the bars at the positions and widths you want.
– ImportanceOfBeingErnest
Nov 28 '18 at 3:05
1
@EdgarR.Mondragón I want a histogram, but I guess I can use the histogram to do a bar plot.
– somayeh khakpash
Nov 28 '18 at 14:27
1
@ImportanceOfBeingErnest I guess that would work. I will try it if don't find any other ways to fix my histogram.
– somayeh khakpash
Nov 28 '18 at 14:28
|
show 3 more comments
I am using matplotlib histogram histtype='bar' to plot four datasets together. What it automatically does is that it changes the width of the bar of each dataset relative to the size of the dataset. I want to have equal bar width for all four datasets.
[n, bins, patches = plt.hist(
[np.sort(x2), np.sort(x3), np.sort(x4), np.sort(x5)],
bins=np.logspace(np.log10(8e-8), np.log10(1), 7),
histtype='bar',
weights=[w2,w3,w4,w5],
label=[
'$Delta {{chi}^2} <40$',
'$u_0 < 0.045$',
'${s}_{fitted} >5$',
'Multi-peaked'
]
)][1]

python matplotlib
I am using matplotlib histogram histtype='bar' to plot four datasets together. What it automatically does is that it changes the width of the bar of each dataset relative to the size of the dataset. I want to have equal bar width for all four datasets.
[n, bins, patches = plt.hist(
[np.sort(x2), np.sort(x3), np.sort(x4), np.sort(x5)],
bins=np.logspace(np.log10(8e-8), np.log10(1), 7),
histtype='bar',
weights=[w2,w3,w4,w5],
label=[
'$Delta {{chi}^2} <40$',
'$u_0 < 0.045$',
'${s}_{fitted} >5$',
'Multi-peaked'
]
)][1]

python matplotlib
python matplotlib
edited Nov 28 '18 at 6:33
Mr. T
4,21391536
4,21391536
asked Nov 28 '18 at 0:24
somayeh khakpashsomayeh khakpash
133
133
1
No, you set the bar width yourself via thebinsargument.
– ImportanceOfBeingErnest
Nov 28 '18 at 1:02
1
Thanks, but I’m not sure if I understand your point. My understanding is that the bins are the same for all datasets, so how does that determine the widths?
– somayeh khakpash
Nov 28 '18 at 2:27
2
Oh, sorry, this kind of plot is really misleading. You kind of want a categorical histogram? I think you might decide to calculate the histogram first, then plot a barplot with the bars at the positions and widths you want.
– ImportanceOfBeingErnest
Nov 28 '18 at 3:05
1
@EdgarR.Mondragón I want a histogram, but I guess I can use the histogram to do a bar plot.
– somayeh khakpash
Nov 28 '18 at 14:27
1
@ImportanceOfBeingErnest I guess that would work. I will try it if don't find any other ways to fix my histogram.
– somayeh khakpash
Nov 28 '18 at 14:28
|
show 3 more comments
1
No, you set the bar width yourself via thebinsargument.
– ImportanceOfBeingErnest
Nov 28 '18 at 1:02
1
Thanks, but I’m not sure if I understand your point. My understanding is that the bins are the same for all datasets, so how does that determine the widths?
– somayeh khakpash
Nov 28 '18 at 2:27
2
Oh, sorry, this kind of plot is really misleading. You kind of want a categorical histogram? I think you might decide to calculate the histogram first, then plot a barplot with the bars at the positions and widths you want.
– ImportanceOfBeingErnest
Nov 28 '18 at 3:05
1
@EdgarR.Mondragón I want a histogram, but I guess I can use the histogram to do a bar plot.
– somayeh khakpash
Nov 28 '18 at 14:27
1
@ImportanceOfBeingErnest I guess that would work. I will try it if don't find any other ways to fix my histogram.
– somayeh khakpash
Nov 28 '18 at 14:28
1
1
No, you set the bar width yourself via the
bins argument.– ImportanceOfBeingErnest
Nov 28 '18 at 1:02
No, you set the bar width yourself via the
bins argument.– ImportanceOfBeingErnest
Nov 28 '18 at 1:02
1
1
Thanks, but I’m not sure if I understand your point. My understanding is that the bins are the same for all datasets, so how does that determine the widths?
– somayeh khakpash
Nov 28 '18 at 2:27
Thanks, but I’m not sure if I understand your point. My understanding is that the bins are the same for all datasets, so how does that determine the widths?
– somayeh khakpash
Nov 28 '18 at 2:27
2
2
Oh, sorry, this kind of plot is really misleading. You kind of want a categorical histogram? I think you might decide to calculate the histogram first, then plot a barplot with the bars at the positions and widths you want.
– ImportanceOfBeingErnest
Nov 28 '18 at 3:05
Oh, sorry, this kind of plot is really misleading. You kind of want a categorical histogram? I think you might decide to calculate the histogram first, then plot a barplot with the bars at the positions and widths you want.
– ImportanceOfBeingErnest
Nov 28 '18 at 3:05
1
1
@EdgarR.Mondragón I want a histogram, but I guess I can use the histogram to do a bar plot.
– somayeh khakpash
Nov 28 '18 at 14:27
@EdgarR.Mondragón I want a histogram, but I guess I can use the histogram to do a bar plot.
– somayeh khakpash
Nov 28 '18 at 14:27
1
1
@ImportanceOfBeingErnest I guess that would work. I will try it if don't find any other ways to fix my histogram.
– somayeh khakpash
Nov 28 '18 at 14:28
@ImportanceOfBeingErnest I guess that would work. I will try it if don't find any other ways to fix my histogram.
– somayeh khakpash
Nov 28 '18 at 14:28
|
show 3 more comments
1 Answer
1
active
oldest
votes
The bins are actually the same size (numeric value) for each set but your x axis is logarithmic so it looks like the width is different. Try to plot a single dataset in a linear x axis (not logarithmic) and you will see that the widths of the bars are the same.
1
Thanks. I think you're right. Do you suggest a way to fix that? Because I need to keep the x axis in log scale.
– somayeh khakpash
Nov 28 '18 at 14:36
The only way to keep x axis in log scale and show same width is to have different bin sizes with a logarithmin relation between then. Then they will be shown linearly (same width) in the x axis logarithim plot.
– b-fg
Nov 28 '18 at 14:46
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%2f53510291%2fmatplotlib-histogram-bar-width%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The bins are actually the same size (numeric value) for each set but your x axis is logarithmic so it looks like the width is different. Try to plot a single dataset in a linear x axis (not logarithmic) and you will see that the widths of the bars are the same.
1
Thanks. I think you're right. Do you suggest a way to fix that? Because I need to keep the x axis in log scale.
– somayeh khakpash
Nov 28 '18 at 14:36
The only way to keep x axis in log scale and show same width is to have different bin sizes with a logarithmin relation between then. Then they will be shown linearly (same width) in the x axis logarithim plot.
– b-fg
Nov 28 '18 at 14:46
add a comment |
The bins are actually the same size (numeric value) for each set but your x axis is logarithmic so it looks like the width is different. Try to plot a single dataset in a linear x axis (not logarithmic) and you will see that the widths of the bars are the same.
1
Thanks. I think you're right. Do you suggest a way to fix that? Because I need to keep the x axis in log scale.
– somayeh khakpash
Nov 28 '18 at 14:36
The only way to keep x axis in log scale and show same width is to have different bin sizes with a logarithmin relation between then. Then they will be shown linearly (same width) in the x axis logarithim plot.
– b-fg
Nov 28 '18 at 14:46
add a comment |
The bins are actually the same size (numeric value) for each set but your x axis is logarithmic so it looks like the width is different. Try to plot a single dataset in a linear x axis (not logarithmic) and you will see that the widths of the bars are the same.
The bins are actually the same size (numeric value) for each set but your x axis is logarithmic so it looks like the width is different. Try to plot a single dataset in a linear x axis (not logarithmic) and you will see that the widths of the bars are the same.
answered Nov 28 '18 at 9:17
b-fgb-fg
1,97411524
1,97411524
1
Thanks. I think you're right. Do you suggest a way to fix that? Because I need to keep the x axis in log scale.
– somayeh khakpash
Nov 28 '18 at 14:36
The only way to keep x axis in log scale and show same width is to have different bin sizes with a logarithmin relation between then. Then they will be shown linearly (same width) in the x axis logarithim plot.
– b-fg
Nov 28 '18 at 14:46
add a comment |
1
Thanks. I think you're right. Do you suggest a way to fix that? Because I need to keep the x axis in log scale.
– somayeh khakpash
Nov 28 '18 at 14:36
The only way to keep x axis in log scale and show same width is to have different bin sizes with a logarithmin relation between then. Then they will be shown linearly (same width) in the x axis logarithim plot.
– b-fg
Nov 28 '18 at 14:46
1
1
Thanks. I think you're right. Do you suggest a way to fix that? Because I need to keep the x axis in log scale.
– somayeh khakpash
Nov 28 '18 at 14:36
Thanks. I think you're right. Do you suggest a way to fix that? Because I need to keep the x axis in log scale.
– somayeh khakpash
Nov 28 '18 at 14:36
The only way to keep x axis in log scale and show same width is to have different bin sizes with a logarithmin relation between then. Then they will be shown linearly (same width) in the x axis logarithim plot.
– b-fg
Nov 28 '18 at 14:46
The only way to keep x axis in log scale and show same width is to have different bin sizes with a logarithmin relation between then. Then they will be shown linearly (same width) in the x axis logarithim plot.
– b-fg
Nov 28 '18 at 14:46
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%2f53510291%2fmatplotlib-histogram-bar-width%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
1
No, you set the bar width yourself via the
binsargument.– ImportanceOfBeingErnest
Nov 28 '18 at 1:02
1
Thanks, but I’m not sure if I understand your point. My understanding is that the bins are the same for all datasets, so how does that determine the widths?
– somayeh khakpash
Nov 28 '18 at 2:27
2
Oh, sorry, this kind of plot is really misleading. You kind of want a categorical histogram? I think you might decide to calculate the histogram first, then plot a barplot with the bars at the positions and widths you want.
– ImportanceOfBeingErnest
Nov 28 '18 at 3:05
1
@EdgarR.Mondragón I want a histogram, but I guess I can use the histogram to do a bar plot.
– somayeh khakpash
Nov 28 '18 at 14:27
1
@ImportanceOfBeingErnest I guess that would work. I will try it if don't find any other ways to fix my histogram.
– somayeh khakpash
Nov 28 '18 at 14:28