简体   繁体   中英

How to make a plot with multiple bars using seaborn

I have a pandas data frame and am trying to make a multi-bar bar chart using seaborn. I want to have each set of trials for the RF_# labels grouped together; for example, I want RF_1 to be separate from RF_6 and want the five trials for each respective RF_# value to be next to each other but separate from the other RF_# groups.

I have read that to make this kind of bar chart, the melt function needs to be used first, but I'm not sure how that would work for my specific data frame.

You can melt the dataframe, then use seaborn 's x and hue arguments to create the groupings you're looking for. The example below could benefit from some column renaming and visual tweaking, but this should get you in the right direction.

Note that you can perform the melt directly in the seaborn.barplot command, but you may want to reassign this melted dataframe to a new variable for use in other plotting commands.

df = pd.DataFrame({'RF_1': {'Trial_1': 64.66,
  'Trial_2': 48.37,
  'Trial_3': 84.0,
  'Trial_4': 11.16,
  'Trial_5': 77.05},
 'RF_6': {'Trial_1': 37.19,
  'Trial_2': 17.14,
  'Trial_3': 1.5,
  'Trial_4': 66.14,
  'Trial_5': 24.19},
 'RF_7': {'Trial_1': 6.81,
  'Trial_2': 31.22,
  'Trial_3': 80.24,
  'Trial_4': 4.04,
  'Trial_5': 58.26},
 'RF_8': {'Trial_1': 20.97,
  'Trial_2': 44.52,
  'Trial_3': 11.38,
  'Trial_4': 75.17,
  'Trial_5': 92.25},
 'RF_9': {'Trial_1': 95.29,
  'Trial_2': 19.66,
  'Trial_3': 98.07,
  'Trial_4': 54.02,
  'Trial_5': 86.31}})

sns.barplot(x='RF', y='value', hue='index',
            data=df.reset_index().melt(id_vars='index', var_name='RF'))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM