Line plot (1)
With matplotlib, you can create a bunch of different plots in Python. The most basic plot is the line plot. A general recipe is given here.
import matplotlib.pyplot as plt
plt.plot(x,y)
plt.show()
In the video, you already saw how much the world population has grown over the past years. Will it continue to do so? The world bank has estimates of the world population for the years 1950 up to 2100. The years are loaded in your workspace as a list called year
, and the corresponding populations as a list called pop
.
This course touches on a lot of concepts you may have forgotten, so if you ever need a quick refresher, download the Python Cheat Sheet and keep it handy!
This is a part of the course
“Intermediate Python”
Exercise instructions
print()
the last item from both theyear
and thepop
list to see what the predicted population for the year 2100 is. Use twoprint()
functions.- Before you can start, you should import
matplotlib.pyplot
asplt
.pyplot
is a sub-package ofmatplotlib
, hence the dot. - Use
plt.plot()
to build a line plot.year
should be mapped on the horizontal axis,pop
on the vertical axis. Don't forget to finish off with theplt.show()
function to actually display the plot.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Print the last item from year and pop
# Import matplotlib.pyplot as plt
# Make a line plot: year on the x-axis, pop on the y-axis
# Display the plot with plt.show()