-1

I'm trying to visualize a data frame I have with a stacked barchart, where the x is websites, the y is frequency and then the groups on the barchart are different groups using them. This is the dataframe: enter image description here

This is the plot created just by doing this:

web_data_roles.plot(kind='barh', stacked=True, figsize=(20,10))

enter image description here

As you can see its not what I want, vie tried changing the plot so the axes match up to the different columns of the dataframe but it just says no numerical data to plot, Not sure how to go about this anymore. so all help is appreciated

  • You didn't specify `x` or `y`, so the API will only plot numeric data or dates. Use seaborn. `import seaborn as sns` and then `sns.barplot(data=web_data_roles, x='freq', y='website', hue='role', dodge=False)`. – Trenton McKinney Jan 16 '21 at 04:31
  • **[No Screenshots](https://meta.stackoverflow.com/questions/303812/)** of code or data. Always provide a [mre] with code, **data, errors, current output, and expected output**, as **[formatted text](https://stackoverflow.com/help/formatting)**. If relevant, plot images are okay. Please see [How to ask a good question](https://stackoverflow.com/help/how-to-ask). Provide data with [How to provide a reproducible copy of your DataFrame using `df.head(15).to_clipboard(sep=',')`](https://stackoverflow.com/questions/52413246), then **[edit] your question**, and paste the clipboard into a code block. – Trenton McKinney Jan 16 '21 at 04:32

1 Answers1

0

You need to organise your dataframe so that role is a column.

  1. set_index() initial preparation
  2. unstack() to move role out of index and make a column
  3. droplevel() to clean up multi index columns
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1,1, figsize=[10,5],
                      sharey=False, sharex=False, gridspec_kw={"hspace":0.3})

df = pd.read_csv(io.StringIO("""website,role,freq
www.bbc.co.uk,director,2000
www.bbc.co.uk,technical,500
www.twitter.com,director,4000
www.twitter.com,technical,1500
"""))

df.set_index(["website","role"]).unstack(1).droplevel(0,axis=1).plot(ax=ax, kind="barh", stacked=True)

enter image description here

Rob Raymond
  • 29,118
  • 3
  • 14
  • 30