Reputation: 481
I am looking to plot 'surgeon id' on the x-axis and 'Total Cases' on the y-axis. Within each bar, I would like to color the bar in relation to the % for Treatment X and Y.
treatment surgeon_id initial_severity Total Cases Treatment X% Treatment Y%
0 2 MINOR 16.0 6.250000 93.750000
1 3 MINOR 41.0 36.585366 63.414634
2 4 MINOR 69.0 33.333333 66.666667
3 5 MINOR 102.0 34.313725 65.686275
4 6 MINOR 348.0 11.781609 88.218391
5 7 MINOR 20.0 40.000000 60.000000
6 8 MINOR 90.0 28.888889 71.111111
7 9 MINOR 41.0 17.073171 82.926829
8 10 MINOR 139.0 24.460432 75.539568
9 11 MINOR 46.0 23.913043 76.086957
10 12 MINOR 35.0 20.000000 80.000000
11 13 MINOR 96.0 29.166667 70.833333
12 16 MINOR 51.0 21.568627 78.431373
13 17 MINOR 90.0 27.777778 72.222222
14 18 MINOR 100.0 24.000000 76.000000
15 19 MINOR 78.0 28.205128 71.794872
16 20 MINOR 96.0 25.000000 75.000000
17 21 MINOR 23.0 26.086957 73.913043
18 22 MINOR 9.0 33.333333 66.666667
19 25 MINOR 78.0 30.769231 69.230769
20 28 MINOR 7.0 14.285714 85.714286
21 30 MINOR 70.0 25.714286 74.285714
22 31 MINOR 21.0 42.857143 57.142857
23 32 MINOR 101.0 29.702970 70.297030
24 41 MINOR 1.0 100.000000 0.000000
25 43 MINOR 15.0 0.000000 100.000000
Current plotting code:
sns.barplot(x="surgeon_id", y="Total Cases", data=minor_treatment_choice)
How can I color each bar in relation to the % for Treatment X and Y? I would also like the color for Treatment X and Y within the bars to be the same color for every surgeon_id.
Thanks!
Upvotes: 0
Views: 7916
Reputation: 80279
You can add two extra columns, where you multiply the percentages with the total cases.
df["Treatment X"] = df["Treatment X%"] * df["Total Cases"] / 100
df["Treatment Y"] = df["Treatment Y%"] * df["Total Cases"] / 100
Then, with pandas you could create a stacked bar plot:
df.set_index("surgeon_id")[["Treatment X", "Treatment Y"]].plot.bar(color=['crimson', 'dodgerblue'], stacked=True)
To create stacked bars with Seaborn, you can first draw the lower bars, and in a second call add the upper bars setting their bottom to the top of the lower bars:
from matplotlib import pyplot as plt
import pandas as pd
import seaborn as sns
df["Treatment X"] = df["Treatment X%"] * df["Total Cases"] / 100
df["Treatment Y"] = df["Treatment Y%"] * df["Total Cases"] / 100
ax = sns.barplot(data=df, x="surgeon_id", y=df["Treatment X"], color='crimson', label="Treatment X")
sns.barplot(data=df, x="surgeon_id", y=df["Treatment Y"], bottom=df["Treatment X"], color='dodgerblue',
label="Treatment Y", ax=ax)
ax.legend()
plt.show()
Upvotes: 2