Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Seaborn stacked barplot multiple columns [duplicate]

data frame trying to graph using seaborn barplot

I'm trying to plot the dataframe above using Seaborn barplot.

plt.figure()
sns.set_theme(context = "notebook", style = "darkgrid", palette = "deep", font = "sans-serif",
              rc = {"figure.figsize": (20, 12)})

ax = sns.barplot(data = graph_df, x = "Month", y = "Contract", color = "green")
ax = sns.barplot(data = graph_df, x = "Month", y = "Definite", color = "lightgreen")
ax = sns.barplot(data = graph_df, x = "Month", y = "T1", color = "blue")
ax = sns.barplot(data = graph_df, x = "Month", y = "T2", color = "lightblue")
ax = sns.barplot(data = graph_df, x = "Month", y = "CXLD", color = "red")
ax.yaxis.set_major_formatter(tick.FuncFormatter(reformat_large_tick_values))
plt.ylabel = "Total Rent"

plt.show()

The plot that is produced doesn't show all the values for each month. For instance, look at February, it should have Contract = 404,600 (green), Definite = 94,450 (light green), T1 = 726,000 (blue) and T2 = 121,850 (light blue) for a total of $1,346,900. But, it only shows the blue (T1) and light blue (T2) values and the y axis should be capable of showing that value.

enter image description here

I used matplotlib to create the same graph and this shows how the graph should look. The only reason I'm not using this, is I can't seem to get the y axis to convert to normal dollar values and the y axis label to show the value for "Total $'s".

enter image description here

Thanks for any help.

like image 649
Rob Brooks Avatar asked Feb 01 '26 06:02

Rob Brooks


1 Answers

Creating stacked plots is usually easier in pandas/matplotlib. I have given the code for this below...

colors = ['green', 'lightgreen', 'blue', 'lightblue', 'red'] ## Colors for the bars
graph_df.set_index('Month').plot(kind='bar', stacked=True, color=colors) ## Plot
plt.ticklabel_format(style='plain', useOffset=False, axis='y') ## No offset
plt.gca().set_ylabel("Total $'s") ## Set Y-axis

enter image description here

like image 115
Redox Avatar answered Feb 03 '26 19:02

Redox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!