Labels
It's time to customize your own plot. This is the fun part, you will see your plot come to life!
You're going to work on the scatter plot with world development data: GDP per capita on the x-axis (logarithmic scale), life expectancy on the y-axis. The code for this plot is available in the script.
As a first step, let's add axis labels and a title to the plot. You can do this with the xlabel()
, ylabel()
and title()
functions, available in matplotlib.pyplot
. This sub-package is already imported as plt
.
This is a part of the course
“Intermediate Python”
Exercise instructions
- The strings
xlab
andylab
are already set for you. Use these variables to set the label of the x- and y-axis. - The string
title
is also coded for you. Use it to add a title to the plot. - After these customizations, finish the script with
plt.show()
to actually display the plot.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Basic scatter plot, log scale
plt.scatter(gdp_cap, life_exp)
plt.xscale('log')
# Strings
xlab = 'GDP per Capita [in USD]'
ylab = 'Life Expectancy [in years]'
title = 'World Development in 2007'
# Add axis labels
# Add title
# After customizing, display the plot
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.