0

I have a dataset having gdp of countries and their biofuel production named "Merging2". I am trying to plot a bar chart of top 5 countries in gdp and in the same plot have the bar chart of their biofuel_production.

I plotted the top gdp's using :

yr=Merging2.groupby(by='Years')
access1=yr.get_group(2019)
sorted=access1.sort_values(["rgdpe"], ascending=[False]) #sorting it
highest_gdp_2019=sorted.head(10) # taking the top 5 rgdpe
fig, ax=plt.subplots()
plt.bar(highest_gdp_2019.Countries,highest_gdp_2019.rgdpe, color ='black',width = 0.8,alpha=0.8)
ax.set_xlabel("Countries")
ax.set_ylabel("Real GDP")
plt.xticks(rotation = 90)

Is there a way to do that in Python ?

Aroune
  • 47
  • 5

1 Answers1

0

Do you want two subplots, or do you want both bars next to each other? Either way, check out this other thread which should give you the answer to both. In the second case, you would want a secondary Y-axis (as illustrated in the post)

ViggoTW
  • 36
  • 8
  • my issue is that the countries that are in the top 5 GDP are not the same as top biofuel production. Is there a way I can access those exact countries that are in top 5 gdp and get their biofuel production ? Thank you – Aroune Dec 02 '22 at 23:30
  • `df.sort_values("GDP")["biofuel"].head(5)`. Is this what you are looking for? – ViggoTW Dec 04 '22 at 05:49