1

After playing around with my data, grouping and unstacking I got to the following Series :

        day          type   meat   veg         vegan
0    2017-07-02    Evening  62.0   11.0        16.0
1    2017-07-03  Afternoon  54.0    6.0        16.0
2    2017-07-03    Evening   1.0    0.0         0.0
3    2017-07-04    Morning  26.0    2.0         2.0
4    2017-07-04  Afternoon  47.0    5.0         7.0
5    2017-07-04    Evening  46.0    7.0        14.0
6    2017-07-05    Morning  16.0    4.0         9.0
7    2017-07-05  Afternoon  36.0    9.0        24.0
8    2017-07-05    Evening  44.0    6.0        20.0
9    2017-07-06  Afternoon  47.0    8.0        16.0
10   2017-07-06    Evening  36.0    6.0        18.0
11   2017-07-07    Morning  36.0    4.0         8.0
12   2017-07-07  Afternoon  23.0    3.0         5.0
13   2017-07-07    Evening  50.0   12.0        16.0
14   2017-07-08    Morning  21.0    0.0         9.0
15   2017-07-08  Afternoon  21.0    2.0         7.0
16   2017-07-08    Evening  48.0    8.0        16.0

Now what I would like to do is to plot a stacked bar where on the y axis the total number, and on the x axis the meals (Evening, afternoon, Morning), grouped by day.

At the moment the best I managed is

 .plot(kind='bar',stacked=True,rot=60)

enter image description here

that gives me a stacked plot but the information about the meals is lost. Ideally I'd like to have :


   |   |   |     |   |  |
-------------------------------------- ....
  Mor Aft Eve   Mor Aft Eve ....
  -----------   -----------
     01/07          02/07

Update 1

Getting inspiration from Multiple stacked bar plot with pandas and plotting evening, mornings and afternoon separately I get something better still not satisfactory.

fig, ax = plt.subplots()
gdf.loc[gdf['day'] == 'Morning'].plot(kind='bar',stacked=True,rot=60,ax=ax, position=1.5, width=0.1)
gdf.loc[gdf['day'] == 'Afternoon'].plot(kind='bar',stacked=True,rot=60,ax=ax, position=0.5, width=0.1)
gdf.loc[gdf['day'] == 'Evening'].plot(kind='bar',stacked=True,rot=60,ax=ax, position=-0.5, width=0.1)

plt.show()

enter image description here

I imagine this can all be done more elegantly without drawing separate subplots and then stitching them together

Update 2

re-indexing the data I can get a better plot, but still not what I'm aiming for, that is grouping labels that is


   |   |   |     |   |  |
-------------------------------------- ....
  Mor Aft Eve   Mor Aft Eve ....
  -----------   -----------
     01/07          02/07

enter image description here

pietro abate
  • 459
  • 1
  • 4
  • 10
  • Specify columns names. it is not clear what is total number – Serenity Oct 24 '17 at 09:53
  • In principle this is already answered here: https://stackoverflow.com/questions/22787209/how-to-have-clusters-of-stacked-bars-with-python-pandas or here: https://stackoverflow.com/questions/39013425/multiple-stacked-bar-plot-with-pandas. Now those solutions may not be ideal, but in that case please edit the question to make it clear in how far there is some different need here. – ImportanceOfBeingErnest Oct 24 '17 at 10:03
  • yup. I've seen these answers. They both use subplots starting from a set of data frames plotted individually and then stitched in the same graph. I'm basically trying to achieve the same result just by using the plot function (or seaborn) . I got the impression that this is possible, but I'm missing something somewhere ... – pietro abate Oct 24 '17 at 10:11
  • While it seems you are not satisfied with the output, it is not clear what would be wrong with it. Unless a mind-reader comes along, you would need to tell exactly what you want to achieve. – ImportanceOfBeingErnest Oct 24 '17 at 11:15
  • Two things. First using subplots seems to me a lot of work and I've the impression this could be done better. Secondly, even using subplots I'm not able to get the result I want, that is to group and label correctly the bar plot. – pietro abate Oct 24 '17 at 11:53
  • (Please use the @ notation to notify someone, otherwise I may only read this by coincidence) It seems you want some labeling like in this [this question](https://stackoverflow.com/questions/43545879/bar-chart-with-multiple-labels). – ImportanceOfBeingErnest Oct 24 '17 at 13:39

0 Answers0