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()
This exercise is part of the course
Intermediate Python
Level up your data science skills by creating visualizations using Matplotlib and manipulating DataFrames with pandas.
Data visualization is a key skill for aspiring data scientists. Matplotlib makes it easy to create meaningful and insightful plots. In this chapter, you’ll learn how to build various types of plots, and customize them to be more visually appealing and interpretable.
Exercise 1: Basic plots with MatplotlibExercise 2: Line plot (1)Exercise 3: Line Plot (2): InterpretationExercise 4: Line plot (3)Exercise 5: Scatter Plot (1)Exercise 6: Scatter plot (2)Exercise 7: HistogramExercise 8: Build a histogram (1)Exercise 9: Build a histogram (2): binsExercise 10: Build a histogram (3): compareExercise 11: Choose the right plot (1)Exercise 12: Choose the right plot (2)Exercise 13: CustomizationExercise 14: LabelsExercise 15: TicksExercise 16: SizesExercise 17: ColorsExercise 18: Additional CustomizationsExercise 19: InterpretationWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.