Home > Software design >  How to reverse the legends of stacked barplot in pandas
How to reverse the legends of stacked barplot in pandas

Time:01-28

I have a dataset with a few records about some crop production by year. So I am visualizing the top produced crop by each year in a stacked bar chart. Dataset I have used can be found in enter image description here

Now if you look at the graph you would notice the stacked bar chart legends are revered, it is showing 2021 status first instead of 2018. So I want to reverse this order of representation.

I found one sns.countplot example

With pandas, you can get stacked bars, but you need a bit more manipulation:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

df = pd.read_csv('PMFBY coverage.csv')
sns.set_style('white')
order = df.groupby('cropName').size().sort_values(ascending=False)[:5].index
df_5_largest = df[df['cropName'].isin(order)]
df_5_largest_year_count = df_5_largest.groupby(['cropName', 'year']).size().unstack('year').reindex(order)
ax = df_5_largest_year_count.plot.barh(stacked=True, figsize=(12, 5))
ax.invert_yaxis()
for bars in ax.containers:
    ax.bar_label(bars, fmt='%.0f', label_type='center', color='white', fontsize=16)
sns.despine()
plt.tight_layout()
plt.show()

pandas stacked bars

Now, compare this with how the bars would look like if you'd consider the 5 largest crops of each individual year. Notice how the crops and their order is different each year. How would you combine such information to a single plot?

sns.set_style('white')
fig, axs = plt.subplots(2, 2, figsize=(14, 8))

df[df.year == 2018].groupby('cropName').size().nlargest(5).plot(kind='barh', color='C0', title='2018', ax=axs[0, 0])
df[df.year == 2019].groupby('cropName').size().nlargest(5).plot(kind='barh', color='C1', title='2019', ax=axs[0, 1])
df[df.year == 2020].groupby('cropName').size().nlargest(5).plot(kind='barh', color='C2', title='2020', ax=axs[1, 0])
df[df.year == 2021].groupby('cropName').size().nlargest(5).plot(kind='barh', color='C3', title='2021', ax=axs[1, 1])
for ax in axs.flat:
    ax.bar_label(ax.containers[0], fmt='%.0f', label_type='edge', padding=2)
    ax.margins(x=0.1)
sns.despine()
plt.tight_layout()
plt.show()

bars for 5 largest crops for each year

  •  Tags:  
  • Related