-2

I'm working on a binary classification project with several quantitative variables. I'd like to plot a histogram like below in the exploratory analysis phase. However, after researching on-line for two days, I still could find a solution in Python.

In R, I know how to that with the one-line code: ggplot(train, aes(var_x, fill = var_y)). With Python, anyone could tell me an equivalent syntax with matplotlib or seaborn?

enter image description here

Chasen Li
  • 205
  • 1
  • 3
  • 12

1 Answers1

-1

To get an overlaid bargraph like that in seaborn it is slightly more involved than the one line code from R.

You actually need to create two plots, one for the 'background' and one for the 'foreground'

stacked_bar_data["total"] = stacked_bar_data.Series1 + stacked_bar_data.Series2 # Creates 'total column of two series you're interested in.


#Plot 1 - background - "total" (top) series
sns.barplot(x = stacked_bar_data.Group, y = stacked_bar_data.total, color = "blue")

#Plot 2 - overlay - "bottom" series
bottom_plot = sns.barplot(x = stacked_bar_data.Group, y = stacked_bar_data.Series1, color = "red")


topbar = plt.Rectangle((0,0),1,1,fc="red", edgecolor = 'none')
bottombar = plt.Rectangle((0,0),1,1,fc='#0000A3',  edgecolor = 'none')
l = plt.legend([bottombar, topbar], ['Bottom Bar', 'Top Bar'], loc=1, ncol = 2, prop={'size':16})
l.draw_frame(False)

The above code was taken from: http://randyzwitch.com/creating-stacked-bar-chart-seaborn/

Sam Bozek
  • 87
  • 1
  • 4