Pandas groupby group visualization by dividing between groups










0















I am facing a very annoying problem. I have a dataset where I have the sales amounts for different regions and years.



I would like to visualize the yearly aggregated sales amounts based on different regions.



Below is my groupby code:



groups = df.groupby(["Region", "Year"])["Sales"].sum()
groups.plot.bar(color="blue")
plt.show()


And the output I get looks like this:
enter image description here



I have two questions:
1. How could I somehow separate the region and year bars from each other as this way my chart looks really confusing? A separator line or actually a highlighter would also work, or even a bigger gap would be a good solution to me.
(Please see below, what I mean:)
enter image description here
or
enter image description here



I have no clue at all, how to solve this problem.



Question no 2. How could I have this image sorted by the regions with most sales, followed by the second most sales yearly, and so on? Kind of sorting in a descending order based on regions.



I tried the code below:



groups = df.groupby(["Region", "Year"])["Sales"].sum()
groups2=groups.sort_values(axis=[0][1],ascending=False)
groups.plot.bar(color="blue")
plt.show()


But I get a list index out of range error. Using axis=[0] does not solve the problem.



Thank you very much for your help in advance!










share|improve this question

















  • 3





    seaborn barplots accept a second variable hue that allows this sort of grouping.

    – ChrisD
    Nov 14 '18 at 20:31















0















I am facing a very annoying problem. I have a dataset where I have the sales amounts for different regions and years.



I would like to visualize the yearly aggregated sales amounts based on different regions.



Below is my groupby code:



groups = df.groupby(["Region", "Year"])["Sales"].sum()
groups.plot.bar(color="blue")
plt.show()


And the output I get looks like this:
enter image description here



I have two questions:
1. How could I somehow separate the region and year bars from each other as this way my chart looks really confusing? A separator line or actually a highlighter would also work, or even a bigger gap would be a good solution to me.
(Please see below, what I mean:)
enter image description here
or
enter image description here



I have no clue at all, how to solve this problem.



Question no 2. How could I have this image sorted by the regions with most sales, followed by the second most sales yearly, and so on? Kind of sorting in a descending order based on regions.



I tried the code below:



groups = df.groupby(["Region", "Year"])["Sales"].sum()
groups2=groups.sort_values(axis=[0][1],ascending=False)
groups.plot.bar(color="blue")
plt.show()


But I get a list index out of range error. Using axis=[0] does not solve the problem.



Thank you very much for your help in advance!










share|improve this question

















  • 3





    seaborn barplots accept a second variable hue that allows this sort of grouping.

    – ChrisD
    Nov 14 '18 at 20:31













0












0








0








I am facing a very annoying problem. I have a dataset where I have the sales amounts for different regions and years.



I would like to visualize the yearly aggregated sales amounts based on different regions.



Below is my groupby code:



groups = df.groupby(["Region", "Year"])["Sales"].sum()
groups.plot.bar(color="blue")
plt.show()


And the output I get looks like this:
enter image description here



I have two questions:
1. How could I somehow separate the region and year bars from each other as this way my chart looks really confusing? A separator line or actually a highlighter would also work, or even a bigger gap would be a good solution to me.
(Please see below, what I mean:)
enter image description here
or
enter image description here



I have no clue at all, how to solve this problem.



Question no 2. How could I have this image sorted by the regions with most sales, followed by the second most sales yearly, and so on? Kind of sorting in a descending order based on regions.



I tried the code below:



groups = df.groupby(["Region", "Year"])["Sales"].sum()
groups2=groups.sort_values(axis=[0][1],ascending=False)
groups.plot.bar(color="blue")
plt.show()


But I get a list index out of range error. Using axis=[0] does not solve the problem.



Thank you very much for your help in advance!










share|improve this question














I am facing a very annoying problem. I have a dataset where I have the sales amounts for different regions and years.



I would like to visualize the yearly aggregated sales amounts based on different regions.



Below is my groupby code:



groups = df.groupby(["Region", "Year"])["Sales"].sum()
groups.plot.bar(color="blue")
plt.show()


And the output I get looks like this:
enter image description here



I have two questions:
1. How could I somehow separate the region and year bars from each other as this way my chart looks really confusing? A separator line or actually a highlighter would also work, or even a bigger gap would be a good solution to me.
(Please see below, what I mean:)
enter image description here
or
enter image description here



I have no clue at all, how to solve this problem.



Question no 2. How could I have this image sorted by the regions with most sales, followed by the second most sales yearly, and so on? Kind of sorting in a descending order based on regions.



I tried the code below:



groups = df.groupby(["Region", "Year"])["Sales"].sum()
groups2=groups.sort_values(axis=[0][1],ascending=False)
groups.plot.bar(color="blue")
plt.show()


But I get a list index out of range error. Using axis=[0] does not solve the problem.



Thank you very much for your help in advance!







python pandas matplotlib group-by visualization






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 20:11









hunsnowboarderhunsnowboarder

7911




7911







  • 3





    seaborn barplots accept a second variable hue that allows this sort of grouping.

    – ChrisD
    Nov 14 '18 at 20:31












  • 3





    seaborn barplots accept a second variable hue that allows this sort of grouping.

    – ChrisD
    Nov 14 '18 at 20:31







3




3





seaborn barplots accept a second variable hue that allows this sort of grouping.

– ChrisD
Nov 14 '18 at 20:31





seaborn barplots accept a second variable hue that allows this sort of grouping.

– ChrisD
Nov 14 '18 at 20:31












1 Answer
1






active

oldest

votes


















2














Following ChrisD's advice you can obtain a working result with seaborn's catplot to display your bars into different facets by region.



sns.catplot(x='Year', y='Sales', col='Region', data=groups, kind='bar')


You may have to format the aspect ratios for your display purposes.






share|improve this answer























  • Thank you for your reply. Unfortunately I get a "Could not interpret" Year" error. What am I doing wrong? :( Thank you!

    – hunsnowboarder
    Nov 15 '18 at 5:14











  • It may be an issue of the data table shape or format. Have you tried reseting the index in your groupby with reset_index()?

    – kevins_1
    Nov 15 '18 at 14:52












  • Thank you so much Kevin! No, I didnt reset the index. So now I reset it and works like a charm! :) Thank you so much! Is there any way to sort all these chart in descending order? I mean that the region which has the most sales should be put first and all the others follow in a descending order? Thank you again for your tremendous help!

    – hunsnowboarder
    Nov 15 '18 at 15:01












  • You can typically order the data visualized using catplot's order parameters like col_order (maybe row_order, hue_order, or order depending on your taste). Have you tried that out?

    – kevins_1
    Nov 15 '18 at 15:17











  • I know I should be looking arounc col_order but I am not sure what should I add as parameter... I could not figure it out from the information on catplot method unfortunately. :(

    – hunsnowboarder
    Nov 15 '18 at 15:43










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%2f53308054%2fpandas-groupby-group-visualization-by-dividing-between-groups%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









2














Following ChrisD's advice you can obtain a working result with seaborn's catplot to display your bars into different facets by region.



sns.catplot(x='Year', y='Sales', col='Region', data=groups, kind='bar')


You may have to format the aspect ratios for your display purposes.






share|improve this answer























  • Thank you for your reply. Unfortunately I get a "Could not interpret" Year" error. What am I doing wrong? :( Thank you!

    – hunsnowboarder
    Nov 15 '18 at 5:14











  • It may be an issue of the data table shape or format. Have you tried reseting the index in your groupby with reset_index()?

    – kevins_1
    Nov 15 '18 at 14:52












  • Thank you so much Kevin! No, I didnt reset the index. So now I reset it and works like a charm! :) Thank you so much! Is there any way to sort all these chart in descending order? I mean that the region which has the most sales should be put first and all the others follow in a descending order? Thank you again for your tremendous help!

    – hunsnowboarder
    Nov 15 '18 at 15:01












  • You can typically order the data visualized using catplot's order parameters like col_order (maybe row_order, hue_order, or order depending on your taste). Have you tried that out?

    – kevins_1
    Nov 15 '18 at 15:17











  • I know I should be looking arounc col_order but I am not sure what should I add as parameter... I could not figure it out from the information on catplot method unfortunately. :(

    – hunsnowboarder
    Nov 15 '18 at 15:43















2














Following ChrisD's advice you can obtain a working result with seaborn's catplot to display your bars into different facets by region.



sns.catplot(x='Year', y='Sales', col='Region', data=groups, kind='bar')


You may have to format the aspect ratios for your display purposes.






share|improve this answer























  • Thank you for your reply. Unfortunately I get a "Could not interpret" Year" error. What am I doing wrong? :( Thank you!

    – hunsnowboarder
    Nov 15 '18 at 5:14











  • It may be an issue of the data table shape or format. Have you tried reseting the index in your groupby with reset_index()?

    – kevins_1
    Nov 15 '18 at 14:52












  • Thank you so much Kevin! No, I didnt reset the index. So now I reset it and works like a charm! :) Thank you so much! Is there any way to sort all these chart in descending order? I mean that the region which has the most sales should be put first and all the others follow in a descending order? Thank you again for your tremendous help!

    – hunsnowboarder
    Nov 15 '18 at 15:01












  • You can typically order the data visualized using catplot's order parameters like col_order (maybe row_order, hue_order, or order depending on your taste). Have you tried that out?

    – kevins_1
    Nov 15 '18 at 15:17











  • I know I should be looking arounc col_order but I am not sure what should I add as parameter... I could not figure it out from the information on catplot method unfortunately. :(

    – hunsnowboarder
    Nov 15 '18 at 15:43













2












2








2







Following ChrisD's advice you can obtain a working result with seaborn's catplot to display your bars into different facets by region.



sns.catplot(x='Year', y='Sales', col='Region', data=groups, kind='bar')


You may have to format the aspect ratios for your display purposes.






share|improve this answer













Following ChrisD's advice you can obtain a working result with seaborn's catplot to display your bars into different facets by region.



sns.catplot(x='Year', y='Sales', col='Region', data=groups, kind='bar')


You may have to format the aspect ratios for your display purposes.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 21:44









kevins_1kevins_1

345212




345212












  • Thank you for your reply. Unfortunately I get a "Could not interpret" Year" error. What am I doing wrong? :( Thank you!

    – hunsnowboarder
    Nov 15 '18 at 5:14











  • It may be an issue of the data table shape or format. Have you tried reseting the index in your groupby with reset_index()?

    – kevins_1
    Nov 15 '18 at 14:52












  • Thank you so much Kevin! No, I didnt reset the index. So now I reset it and works like a charm! :) Thank you so much! Is there any way to sort all these chart in descending order? I mean that the region which has the most sales should be put first and all the others follow in a descending order? Thank you again for your tremendous help!

    – hunsnowboarder
    Nov 15 '18 at 15:01












  • You can typically order the data visualized using catplot's order parameters like col_order (maybe row_order, hue_order, or order depending on your taste). Have you tried that out?

    – kevins_1
    Nov 15 '18 at 15:17











  • I know I should be looking arounc col_order but I am not sure what should I add as parameter... I could not figure it out from the information on catplot method unfortunately. :(

    – hunsnowboarder
    Nov 15 '18 at 15:43

















  • Thank you for your reply. Unfortunately I get a "Could not interpret" Year" error. What am I doing wrong? :( Thank you!

    – hunsnowboarder
    Nov 15 '18 at 5:14











  • It may be an issue of the data table shape or format. Have you tried reseting the index in your groupby with reset_index()?

    – kevins_1
    Nov 15 '18 at 14:52












  • Thank you so much Kevin! No, I didnt reset the index. So now I reset it and works like a charm! :) Thank you so much! Is there any way to sort all these chart in descending order? I mean that the region which has the most sales should be put first and all the others follow in a descending order? Thank you again for your tremendous help!

    – hunsnowboarder
    Nov 15 '18 at 15:01












  • You can typically order the data visualized using catplot's order parameters like col_order (maybe row_order, hue_order, or order depending on your taste). Have you tried that out?

    – kevins_1
    Nov 15 '18 at 15:17











  • I know I should be looking arounc col_order but I am not sure what should I add as parameter... I could not figure it out from the information on catplot method unfortunately. :(

    – hunsnowboarder
    Nov 15 '18 at 15:43
















Thank you for your reply. Unfortunately I get a "Could not interpret" Year" error. What am I doing wrong? :( Thank you!

– hunsnowboarder
Nov 15 '18 at 5:14





Thank you for your reply. Unfortunately I get a "Could not interpret" Year" error. What am I doing wrong? :( Thank you!

– hunsnowboarder
Nov 15 '18 at 5:14













It may be an issue of the data table shape or format. Have you tried reseting the index in your groupby with reset_index()?

– kevins_1
Nov 15 '18 at 14:52






It may be an issue of the data table shape or format. Have you tried reseting the index in your groupby with reset_index()?

– kevins_1
Nov 15 '18 at 14:52














Thank you so much Kevin! No, I didnt reset the index. So now I reset it and works like a charm! :) Thank you so much! Is there any way to sort all these chart in descending order? I mean that the region which has the most sales should be put first and all the others follow in a descending order? Thank you again for your tremendous help!

– hunsnowboarder
Nov 15 '18 at 15:01






Thank you so much Kevin! No, I didnt reset the index. So now I reset it and works like a charm! :) Thank you so much! Is there any way to sort all these chart in descending order? I mean that the region which has the most sales should be put first and all the others follow in a descending order? Thank you again for your tremendous help!

– hunsnowboarder
Nov 15 '18 at 15:01














You can typically order the data visualized using catplot's order parameters like col_order (maybe row_order, hue_order, or order depending on your taste). Have you tried that out?

– kevins_1
Nov 15 '18 at 15:17





You can typically order the data visualized using catplot's order parameters like col_order (maybe row_order, hue_order, or order depending on your taste). Have you tried that out?

– kevins_1
Nov 15 '18 at 15:17













I know I should be looking arounc col_order but I am not sure what should I add as parameter... I could not figure it out from the information on catplot method unfortunately. :(

– hunsnowboarder
Nov 15 '18 at 15:43





I know I should be looking arounc col_order but I am not sure what should I add as parameter... I could not figure it out from the information on catplot method unfortunately. :(

– hunsnowboarder
Nov 15 '18 at 15:43



















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%2f53308054%2fpandas-groupby-group-visualization-by-dividing-between-groups%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







這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands