import matplotlib.pyplot as plt
# Data for population density and total population for each continent
continents = ['North America', 'South America', 'Europe']
population_density = [22.9, 23.8, 72.9] # People per square km (approx.)
total_population = [579024000, 430759000, 748219000] # Total population (approx.)
# Create the chart
fig, ax1 = plt.subplots(figsize=(8, 5))
# Plotting Population Density
ax1.bar(continents, population_density, color='skyblue', alpha=0.6, label="Population Density (per km²)")
ax1.set_xlabel("Continent")
ax1.set_ylabel("Population Density (per km²)", color='skyblue')
ax1.tick_params(axis='y', labelcolor='skyblue')
# Create a second y-axis for Total Population
ax2 = ax1.twinx()
ax2.plot(continents, total_population, color='orange', marker='o', label="Total Population (in billions)")
ax2.set_ylabel("Total Population (in billions)", color='orange')
ax2.tick_params(axis='y', labelcolor='orange')
# Add a title and show the chart
plt.title("Population Density and Total Population by Continent")
fig.tight_layout()
# Save the chart as an image file
plt.savefig("population_chart.png", dpi=300)
# Show the chart
plt.show()