oreilly-intermediate-python icon indicating copy to clipboard operation
oreilly-intermediate-python copied to clipboard

My plots look linear, whereas yours look exponential

Open OrbitalNet opened this issue 5 years ago • 3 comments

Hi Jessica,

I am following along, mirrored your code, downloaded the text files, they look the same. Here's the life expectancy example:

MyPlotIsLinear

Thx,

...Eddie

OrbitalNet avatar Oct 22 '19 17:10 OrbitalNet

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))

slavarobotam avatar Dec 16 '19 13:12 slavarobotam

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.

population_plot

lindapescatore avatar Mar 04 '20 09:03 lindapescatore

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: image

About the ticks on the y-axis I am not sure how to distribute them properly and reduce the amount.

jay-dizzale avatar Feb 04 '21 14:02 jay-dizzale