FAQ: Line Graphs in Matplotlib - Labeling the Axes


This community-built FAQ covers the “Labeling the Axes” exercise from the lesson “Line Graphs in Matplotlib”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

Data Visualization in Python

FAQs on the exercise Labeling the Axes

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why the page that show result of code and graphs don’t work?

How would the program know which title goes with which graph? What happens if we have two graphs and have only one plt.title() line?

If you’re using plt.title it relies on what it think is the “current axis” is. This would be the most recently created axis but certain operations and functions will change this.

However, if you are working with multiple figures and axes I’d highly suggest keeping a reference to each figure and axis, that way you can directly modify only the axis you want and there’s no need for you or for anyone reading your code to keep track of a hidden “current axis”, the target for any modifications is explicit instead of implicit.

For a quick example-

fig_a, axes_a = plt.subplots(1, 2)
axes_a[0].set_title("The first axis")
axes_a[1].set_title("The second...")

You can modify several settings at once if you like, e.g. ax[1].set(xlim=(0, 8), xticks=np.arange(1, 8), ylim=(0, 8), yticks=np.arange(1, 8)).