Bar chart comparing populations in millions, with GDP in millions, formatted for Jupyter Notebook.
import pandas as pd from matplotlib import pyplot as plt
data = pd.read_csv('countries.csv') data.head()
data_2007 = data[data.year == 2007] top10 = data_2007.sort_values('population', ascending=False).head(10)
print(top10)
import numpy as np
x = np.arange(10)
fig, ax1 = plt.subplots()
width = 0.3 # This is the width of each bar in the bar plot. plt.xticks(x, top10.country, rotation=45) population = ax1.bar(x, top10.population / 10**6, width, color='lightgreen') plt.ylabel('Population', fontsize='14')
ax2 = ax1.twinx() gdp = ax2.bar(x + width, top10.gdpPerCapita * top10.population / 10**9, width, color='hotpink') plt.ylabel('GDP', fontsize='14') plt.legend([population, gdp], ['Population in Millions', 'GDP in Billions']) figure = plt.gcf() # get current figure
plt.title('Comparing GDP & Population of the World's Most Populous Countries in 2007', color='mediumseagreen', fontweight='bold', fontsize=15) plt.show()