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!
Diese Übung ist Teil des Kurses
Intermediate Python
Anleitung zur Übung
print()the last item from both theyearand thepoplist to see what the predicted population for the year 2100 is. Use twoprint()functions.- Before you can start, you should import
matplotlib.pyplotasplt.pyplotis a sub-package ofmatplotlib, hence the dot. - Use
plt.plot()to build a line plot.yearshould be mapped on the horizontal axis,popon the vertical axis. Don't forget to finish off with theplt.show()function to actually display the plot.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# 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()