oreilly-intermediate-python
oreilly-intermediate-python copied to clipboard
My plots look linear, whereas yours look exponential
Hi Jessica,
I am following along, mirrored your code, downloaded the text files, they look the same. Here's the life expectancy example:
Thx,
...Eddie
Hi Eddie, I had the same problem and it can be fixed by adding type conversion in lines 11-13:
dates.append(int(date))
male_life_expectancies.append(float(male_life_expectancy))
female_life_expectancies.append(float(female_life_expectancy))
I have a similar problem with the world population plot. Jessica's axes look nice and neat and her curve is exponential. My axes look like a war zone and the plot is mostly linear. This is when I ran the code in her file with no changes, btw.
You can smooth the plot by removing the point:
pyplot.plot(dates, populations, "-") # instead of "o-"
The distribution is wrong because the points are evenly distributed. Matplotlib doesn't seem to interpret the points as numbers, and instead, prints the strings. I fixed this by converting them to int. Full example:
from matplotlib import pyplot
import datetime
data = open("data/world_population.txt", "r").readlines()
dates = []
populations = []
for point in data:
date, population = point.split()
dates.append(int(date))
populations.append(int(population))
pyplot.plot(dates, populations, "-")
pyplot.ylabel("World population in millions")
pyplot.xlabel("Year")
pyplot.yticks(populations)
pyplot.title("World population over time")
pyplot.show()
The result looks more like the one from the video:
About the ticks on the y-axis I am not sure how to distribute them properly and reduce the amount.