-1

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.

Rob Brooks
  • 19
  • 4
  • 1
    Can you please provide sample data? (not in an image please). – thmslmr Mar 27 '23 at 23:00
  • Seaborn doesn't support stacked bar plots. Note that `ax.yaxis.set_major_formatter(...)` is a pure matplotlib function, not depending on seaborn. You can do the same with a bar stacked plot, which you could create via pandas' plot interface. Just like seaborn, pandas also returns the `ax` on which the plot was created. – JohanC Mar 28 '23 at 10:50
  • Thanks! This helped me get the result I wanted. – Rob Brooks Mar 28 '23 at 12:44

1 Answers1

0

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

Redox
  • 9,321
  • 5
  • 9
  • 26