.config
.gitignore
data.csv
main.py
Packager files
.pythonlibs
poetry.lock
pyproject.toml
Config files
.replit
replit.nix
import csv
import matplotlib.pyplot as plt
def generate_population_data_dictionary_from_csv(filename):
"""
creating a dictionary from a csv file and returning a dictionary with the following structure:
{
"Africa": {"population": [100, 200, 300], "year": [1990, 1995, 2000]},
"Asia": {"population": [100, 200, 300], "year": [1990, 1995, 2000]}
}
"""
population_data = {}
with open(filename, "r") as csvfile:
reader = csv.DictReader(csvfile)
for line in reader:
continent = line["continent"]
year = int(line["year"])
population = int(line["population"])
if continent not in population_data:
population_data[continent] = {"population": [], "years": []}
population_data[continent]["population"].append(population)
population_data[continent]["years"].append(year)
return population_data
def generate_plots_from_population_data_dictionary(data_dictionary):
"""
generating plots of internet users by continent and year, one plot per continent
"""
for continent in data_dictionary:
years = data_dictionary[continent]["years"]
population = data_dictionary[continent]["population"]
plt.plot(years, population, label=continent, marker="o", alpha=0.5)
plt.title("Internet Users per Continent")
plt.xlabel("Year")
plt.ylabel("Internet users (in billions)")
plt.grid(True)
plt.tight_layout()
plt.legend()
plt.show()
filename = "data.csv"
population_per_continent = generate_population_data_dictionary_from_csv(filename)
generate_plots_from_population_data_dictionary(population_per_continent)