Python Change axis on Multi Histogram plot





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I have a pandas dataframe df for which I plot a multi-histogram as follow :



df.hist(bins=20)


This give me a result that look like this (Yes this exemple is ugly since there is only one data per histogram, sorry) :
enter image description here



I have a subplot for each numerical column of my dataframe.



Now I want all my histograms to have an X-axis between 0 and 1. I saw that the hist() function take a ax parameter, but I cannot manage to make it work.
How is it possible to do that ?



EDIT :



Here is a minmal example :



import pandas as pd
import matplotlib.pyplot as plt

myArray = [(0,0,0,0,0.5,0,0,0,1),(0,0,0,0,0.5,0,0,0,1)]

myColumns = ['col1','col2','col3','co4','col5','col6','col7','col8','col9']

df = pd.DataFrame(myArray,columns=myColumns)

print(df)
df.hist(bins=20)

plt.show()









share|improve this question

























  • So you just want to change the labels of your x-axis?

    – Mohit Motwani
    Jan 3 at 11:16











  • In the above example, I want all X-axis to be between 0 and 1. So in the cases the 'blue bar' is at 0, it sould be displayed on the left of the histogram, for a bar at 1 it sould be displayed on the left and for 0.5 it should be on the middle of the histogram.

    – Nakeuh
    Jan 3 at 11:19


















1















I have a pandas dataframe df for which I plot a multi-histogram as follow :



df.hist(bins=20)


This give me a result that look like this (Yes this exemple is ugly since there is only one data per histogram, sorry) :
enter image description here



I have a subplot for each numerical column of my dataframe.



Now I want all my histograms to have an X-axis between 0 and 1. I saw that the hist() function take a ax parameter, but I cannot manage to make it work.
How is it possible to do that ?



EDIT :



Here is a minmal example :



import pandas as pd
import matplotlib.pyplot as plt

myArray = [(0,0,0,0,0.5,0,0,0,1),(0,0,0,0,0.5,0,0,0,1)]

myColumns = ['col1','col2','col3','co4','col5','col6','col7','col8','col9']

df = pd.DataFrame(myArray,columns=myColumns)

print(df)
df.hist(bins=20)

plt.show()









share|improve this question

























  • So you just want to change the labels of your x-axis?

    – Mohit Motwani
    Jan 3 at 11:16











  • In the above example, I want all X-axis to be between 0 and 1. So in the cases the 'blue bar' is at 0, it sould be displayed on the left of the histogram, for a bar at 1 it sould be displayed on the left and for 0.5 it should be on the middle of the histogram.

    – Nakeuh
    Jan 3 at 11:19














1












1








1








I have a pandas dataframe df for which I plot a multi-histogram as follow :



df.hist(bins=20)


This give me a result that look like this (Yes this exemple is ugly since there is only one data per histogram, sorry) :
enter image description here



I have a subplot for each numerical column of my dataframe.



Now I want all my histograms to have an X-axis between 0 and 1. I saw that the hist() function take a ax parameter, but I cannot manage to make it work.
How is it possible to do that ?



EDIT :



Here is a minmal example :



import pandas as pd
import matplotlib.pyplot as plt

myArray = [(0,0,0,0,0.5,0,0,0,1),(0,0,0,0,0.5,0,0,0,1)]

myColumns = ['col1','col2','col3','co4','col5','col6','col7','col8','col9']

df = pd.DataFrame(myArray,columns=myColumns)

print(df)
df.hist(bins=20)

plt.show()









share|improve this question
















I have a pandas dataframe df for which I plot a multi-histogram as follow :



df.hist(bins=20)


This give me a result that look like this (Yes this exemple is ugly since there is only one data per histogram, sorry) :
enter image description here



I have a subplot for each numerical column of my dataframe.



Now I want all my histograms to have an X-axis between 0 and 1. I saw that the hist() function take a ax parameter, but I cannot manage to make it work.
How is it possible to do that ?



EDIT :



Here is a minmal example :



import pandas as pd
import matplotlib.pyplot as plt

myArray = [(0,0,0,0,0.5,0,0,0,1),(0,0,0,0,0.5,0,0,0,1)]

myColumns = ['col1','col2','col3','co4','col5','col6','col7','col8','col9']

df = pd.DataFrame(myArray,columns=myColumns)

print(df)
df.hist(bins=20)

plt.show()






python pandas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 12:45







Nakeuh

















asked Jan 3 at 11:11









NakeuhNakeuh

306113




306113













  • So you just want to change the labels of your x-axis?

    – Mohit Motwani
    Jan 3 at 11:16











  • In the above example, I want all X-axis to be between 0 and 1. So in the cases the 'blue bar' is at 0, it sould be displayed on the left of the histogram, for a bar at 1 it sould be displayed on the left and for 0.5 it should be on the middle of the histogram.

    – Nakeuh
    Jan 3 at 11:19



















  • So you just want to change the labels of your x-axis?

    – Mohit Motwani
    Jan 3 at 11:16











  • In the above example, I want all X-axis to be between 0 and 1. So in the cases the 'blue bar' is at 0, it sould be displayed on the left of the histogram, for a bar at 1 it sould be displayed on the left and for 0.5 it should be on the middle of the histogram.

    – Nakeuh
    Jan 3 at 11:19

















So you just want to change the labels of your x-axis?

– Mohit Motwani
Jan 3 at 11:16





So you just want to change the labels of your x-axis?

– Mohit Motwani
Jan 3 at 11:16













In the above example, I want all X-axis to be between 0 and 1. So in the cases the 'blue bar' is at 0, it sould be displayed on the left of the histogram, for a bar at 1 it sould be displayed on the left and for 0.5 it should be on the middle of the histogram.

– Nakeuh
Jan 3 at 11:19





In the above example, I want all X-axis to be between 0 and 1. So in the cases the 'blue bar' is at 0, it sould be displayed on the left of the histogram, for a bar at 1 it sould be displayed on the left and for 0.5 it should be on the middle of the histogram.

– Nakeuh
Jan 3 at 11:19












1 Answer
1






active

oldest

votes


















1














Here is a solution that works, but for sure is not ideal:



import pandas as pd
import matplotlib.pyplot as plt

myArray = [(0,0,0,0,0.5,0,0,0,1),(0,0,0,0,0.5,0,0,0,1)]

myColumns = ['col1','col2','col3','co4','col5','col6','col7','col8','col9']

df = pd.DataFrame(myArray,columns=myColumns)

print(df)
ax = df.hist(bins=20)
for x in ax:
for y in x:
y.set_xlim(0,1)

plt.show()





share|improve this answer
























  • I arrived at the same solution as you. It does the work but I noticed a weird behavior. Histograms can have different width for the bars. For example using myArray = [(0,0,0,0,0,0,0,0,0), (0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9)]

    – Nakeuh
    Jan 3 at 15:39











  • weird, I'm not sure whats causing that behavior. If this solution solved your problem could you mark it as answered? :)

    – Anton
    Jan 4 at 17:25












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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021177%2fpython-change-axis-on-multi-histogram-plot%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









1














Here is a solution that works, but for sure is not ideal:



import pandas as pd
import matplotlib.pyplot as plt

myArray = [(0,0,0,0,0.5,0,0,0,1),(0,0,0,0,0.5,0,0,0,1)]

myColumns = ['col1','col2','col3','co4','col5','col6','col7','col8','col9']

df = pd.DataFrame(myArray,columns=myColumns)

print(df)
ax = df.hist(bins=20)
for x in ax:
for y in x:
y.set_xlim(0,1)

plt.show()





share|improve this answer
























  • I arrived at the same solution as you. It does the work but I noticed a weird behavior. Histograms can have different width for the bars. For example using myArray = [(0,0,0,0,0,0,0,0,0), (0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9)]

    – Nakeuh
    Jan 3 at 15:39











  • weird, I'm not sure whats causing that behavior. If this solution solved your problem could you mark it as answered? :)

    – Anton
    Jan 4 at 17:25
















1














Here is a solution that works, but for sure is not ideal:



import pandas as pd
import matplotlib.pyplot as plt

myArray = [(0,0,0,0,0.5,0,0,0,1),(0,0,0,0,0.5,0,0,0,1)]

myColumns = ['col1','col2','col3','co4','col5','col6','col7','col8','col9']

df = pd.DataFrame(myArray,columns=myColumns)

print(df)
ax = df.hist(bins=20)
for x in ax:
for y in x:
y.set_xlim(0,1)

plt.show()





share|improve this answer
























  • I arrived at the same solution as you. It does the work but I noticed a weird behavior. Histograms can have different width for the bars. For example using myArray = [(0,0,0,0,0,0,0,0,0), (0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9)]

    – Nakeuh
    Jan 3 at 15:39











  • weird, I'm not sure whats causing that behavior. If this solution solved your problem could you mark it as answered? :)

    – Anton
    Jan 4 at 17:25














1












1








1







Here is a solution that works, but for sure is not ideal:



import pandas as pd
import matplotlib.pyplot as plt

myArray = [(0,0,0,0,0.5,0,0,0,1),(0,0,0,0,0.5,0,0,0,1)]

myColumns = ['col1','col2','col3','co4','col5','col6','col7','col8','col9']

df = pd.DataFrame(myArray,columns=myColumns)

print(df)
ax = df.hist(bins=20)
for x in ax:
for y in x:
y.set_xlim(0,1)

plt.show()





share|improve this answer













Here is a solution that works, but for sure is not ideal:



import pandas as pd
import matplotlib.pyplot as plt

myArray = [(0,0,0,0,0.5,0,0,0,1),(0,0,0,0,0.5,0,0,0,1)]

myColumns = ['col1','col2','col3','co4','col5','col6','col7','col8','col9']

df = pd.DataFrame(myArray,columns=myColumns)

print(df)
ax = df.hist(bins=20)
for x in ax:
for y in x:
y.set_xlim(0,1)

plt.show()






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 15:12









AntonAnton

858




858













  • I arrived at the same solution as you. It does the work but I noticed a weird behavior. Histograms can have different width for the bars. For example using myArray = [(0,0,0,0,0,0,0,0,0), (0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9)]

    – Nakeuh
    Jan 3 at 15:39











  • weird, I'm not sure whats causing that behavior. If this solution solved your problem could you mark it as answered? :)

    – Anton
    Jan 4 at 17:25



















  • I arrived at the same solution as you. It does the work but I noticed a weird behavior. Histograms can have different width for the bars. For example using myArray = [(0,0,0,0,0,0,0,0,0), (0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9)]

    – Nakeuh
    Jan 3 at 15:39











  • weird, I'm not sure whats causing that behavior. If this solution solved your problem could you mark it as answered? :)

    – Anton
    Jan 4 at 17:25

















I arrived at the same solution as you. It does the work but I noticed a weird behavior. Histograms can have different width for the bars. For example using myArray = [(0,0,0,0,0,0,0,0,0), (0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9)]

– Nakeuh
Jan 3 at 15:39





I arrived at the same solution as you. It does the work but I noticed a weird behavior. Histograms can have different width for the bars. For example using myArray = [(0,0,0,0,0,0,0,0,0), (0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9)]

– Nakeuh
Jan 3 at 15:39













weird, I'm not sure whats causing that behavior. If this solution solved your problem could you mark it as answered? :)

– Anton
Jan 4 at 17:25





weird, I'm not sure whats causing that behavior. If this solution solved your problem could you mark it as answered? :)

– Anton
Jan 4 at 17:25




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021177%2fpython-change-axis-on-multi-histogram-plot%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith