0

This is my script to display with Seaborn a barplot Stacked:

# Initialize the plot
sns.set(style="whitegrid")
fig, ax = plt.subplots(figsize=(20,12))

# Create the stacked bar chart
sns.barplot(data=df_sorted, y='SalesRepFullName', x='SaleOrderTotal', palette='viridis', label='SaleOrderTotal')

# Add annotations for DaysSalesRep values
for index, row in df_sorted.iterrows():
    plt.text(row['SaleOrderTotal'] + 1000, index, f"{row['DaysSalesRep']} days", color='black', va='center',fontsize=9)

# Set the plot title and labels
plt.title('Sales Data: DaysSalesRep vs. SaleOrderTotal')
plt.xlabel('Sale Order Total')
plt.ylabel('Sales Representative')

# Formatting the x-axis labels as USD dollars
formatter = ticker.StrMethodFormatter('US {x:.0f}')
ax.xaxis.set_major_formatter(formatter)

# Formatting the x-axis labels
plt.xticks(rotation=45, ha='center')

# Display the plot
fig.tight_layout()
plt.show()

Why my labels are not displaying properly?

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Simon GIS
  • 1,045
  • 2
  • 18
  • 37
  • 2
    You didn't correctly use `ax` in the plot call. It should be `sns.barplot(..., ax=ax)`, and then [`ax.bar_label(ax.containers[0], padding=3)`](https://stackoverflow.com/a/67561982/7758804). [code and plot](https://i.stack.imgur.com/O7gza.png) – Trenton McKinney Jun 01 '23 at 23:51

0 Answers0