0

Hi fellow python users.

Okay so I have this pandas.core.series.Series looking something like this:

In [1]: ps
Out[1]: 
asdf-Day_00     0
fldk-Day_00     0
fsld-Day_00     6
               ..
gejw-Day_40     2
gefw-Day_40     8
Name: something, Length: 55383, dtype: category
Categories (12, object): ['0', '1', '2', '3', ..., '8', '9', '10', '11']

Indices are comprised of 'Day_00', 'Day_10', 'Day_20', 'Day_30', 'Day_40', with arbitrary string and dash in front. Values are, as you can see above, made up of '0' ~ '11'.

I want to make this pandas series into proportional plot, just like the attached .png file below (Sorry for poor elaboration of what I want to do and instead just attaching the example figure)

Proportional plot example

Proportional plot example

Mr. T
  • 11,960
  • 10
  • 32
  • 54
Ryan
  • 3
  • 1
  • Possible duplicate [here](https://stackoverflow.com/questions/22787209/how-to-have-clusters-of-stacked-bars-with-python-pandas) and [here](https://stackoverflow.com/questions/47138271/how-to-create-a-stacked-bar-chart-for-my-dataframe-using-seaborn). Your question is general, if you come up with some actual code attempts or specific question, we might be able to help you better. – Thymen Dec 15 '20 at 11:56
  • However, it remains elusive what the categories are. The `asdf` parts of the name, and the y-value would be 0? That will be quite a busy legend given that seemingly these categories are not repeated. – Mr. T Dec 15 '20 at 12:07

1 Answers1

0

Not very sure what you have as data, please provide it in the future. Trying my best here to recreate your series:

np.random.seed(123)
days= ['Day_00', 'Day_10', 'Day_20', 'Day_30', 'Day_40']
prefix = ['asdf','fldk','fsld','gejw','gefw']
idx = [i + "-" + j for j in days for i in prefix]

ps = pd.Series(pd.Categorical(np.random.randint(0,11,len(idx))),index=idx)

Looks like this:

asdf-Day_00     2
fldk-Day_00     2
fsld-Day_00     6
gejw-Day_00     1
gefw-Day_00     3
asdf-Day_10    10
fldk-Day_10     9
fsld-Day_10     6
gejw-Day_10     1

And we use a combination of crosstab and a substitution on your index:

ax = pd.crosstab(ps,ps.index.str.replace("[^ ]*-",""),normalize=0).plot.bar(stacked=True)
ax.legend(loc='lower center',bbox_to_anchor=(0.5, -0.3),ncol=5)

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72