2

I am trying to draw a bar plot with matplotlib. the data set looks like this

I managed to get a bar plot of the count of 'GICS sector', and the code is

pd.value_counts(df['GICS Sector'],normalize=False).plot.bar()
# plt.hist(df['GICS Sector'])
plt.xlabel('sectors',fontsize=14)
plt.xticks(fontsize=12,rotation=90)
plt.title('Histogram of GICS Sectors',fontsize = 20)
plt.ylabel('count for sectors', fontsize=12)
plt.show()

The plot looks like this count of each sector.

For each sector, there are different 'GICS Sub Industry' as you can see in the data frame I printed out. For example, 'Health Care' sector has 'Health Care Equipment' and 'Pharmaceuticals' as sun industries. I would like to change each sector to a stacked bar plot of different sub industries which belongs to the same GICS sector. I could get a bar plot for each sector separately, code here

categories = df['GICS Sector'].unique()
for c in categories:
    print(c,":",df[df['GICS Sector']==c]['GICS Sub Industry'].nunique())
for c in categories:
    pd.value_counts(df[df['GICS Sector']==c]['GICS Sub Industry'],normalize=False).plot.bar()
    plt.ylabel('count for sub industries')
    plt.title(c)
    plt.show()

bar plot when for sector 'industrials'

but I am not able to stack them together and combine them all into one plot.

Any suggestion helps! Thanks!

Tinaw
  • 21
  • 2
  • the groupby feature could be very useful here. `df.group['GICS Sector', 'GICS Sub Industry'].count().unstack().plot(kind='barh', stacked=True)` – Bryce Wayne Apr 05 '20 at 03:50
  • Here is a resource. https://scentellegher.github.io/programming/2017/07/15/pandas-groupby-multiple-columns-plot.html – Bryce Wayne Apr 05 '20 at 03:51
  • 2
    Please don't post images of code, data, or Tracebacks. Copy and paste it as text then format it as code (select it and type `ctrl-k`) ... [Discourage screenshots of code and/or errors](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors) – wwii Apr 05 '20 at 04:06
  • 2
    One [Stacked bar plot](https://matplotlib.org/3.2.1/gallery/lines_bars_and_markers/bar_stacked.html#sphx-glr-gallery-lines-bars-and-markers-bar-stacked-py) example in the Matplotlib gallery. – wwii Apr 05 '20 at 04:08
  • Does this answer your question? [stacked bar plot using matplotlib](https://stackoverflow.com/questions/44309507/stacked-bar-plot-using-matplotlib) – wwii Apr 05 '20 at 04:11
  • 1
    Thanks! It works, except we need df.groupby([...]) – Tinaw Apr 05 '20 at 04:14
  • Glad it worked. – Bryce Wayne Apr 05 '20 at 04:27

0 Answers0