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 kaggle PMFBY Coverage.csv.
Here is my code.
# Top Crop by year
plt.figure(figsize=(12, 6))
df_crg_[df_crg_.year==2018].groupby('cropName').size().nlargest(5).plot(kind='barh', color='red', label='2018')
df_crg_[df_crg_.year==2019].groupby('cropName').size().nlargest(5).plot(kind='barh', color='green', label='2019')
df_crg_[df_crg_.year==2020].groupby('cropName').size().nlargest(5).plot(kind='barh', color='blue', label='2020')
df_crg_[df_crg_.year==2021].groupby('cropName').size().nlargest(5).plot(kind='barh', color='maroon', label='2021')
plt.legend(loc="upper right")
plt.xlabel('Total Production Time')
plt.title('Top Crop by year')
plt.show()
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 solution for this question but I don't know how to apply it, as it is assigning plotting commands to one variable but in my case, there are four plotting commands.
Only this answer would do, but if know and can answer any other method of extracting top produced crop by year then that would be great. If you notice here I am manually going through each year then extracting that year's top crop. I tried doing it with groupby but I wasn't able to get the answer.
Thanks