0
I’m trying to plot a bar graph with 2 different y axes and the same x-axis.
The closest I’ve come to success with the following code:
fig = plt.figure() # Create matplotlib figure
ax = fig.add_subplot(111) # Create matplotlib axes
ax2 = ax.twinx() # Create another axes that shares the same x-axis as ax.
width = 0.4
final_df['GDP Value'].plot(kind='bar', color='red', ax=ax, width=width, position=1)
final_df['Homicides per 100 people'].plot(kind='bar', color='blue', ax=ax2, width=width, position=0)
ax.set_ylabel('GDP Value')
ax2.set_ylabel('Homicides per 100 people')
plt.show()
But as you can see in the image below the x axis is the index of my DF, but I would like it to be a specific column, called 'Country Name'. I tried to specify it like this:
final_df['GDP Value'].plot(x=final_df['Country Name'],kind='bar', color='red', ax=ax, width=width, position=1)
But it didn’t work.
Here’s the structure of my DF:
Thank you in advance, you are the best.