FAQ: Line Graphs in Matplotlib - Review

This community-built FAQ covers the “Review” 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 Review

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!

Say I have plotted one line for temperature change over time and another for the number of ice cream cones sold over the same time period in the same chart, how can I make use of two different y-axis scales, one on each side of the chart, using matplotlib?

1 Like

When I create the lists below, I get the error at the bottom. Why is CodeCademy insisting my data be of equal lengths?

x = list(range(1999, 2010))
y1 = [80, 100, 120, 140]
y2 = [0, 2, 4, 6]

When I Run the code above - ScriptPy returns the following error:
x-values and y-values need to have the same length in order to create ordered pairs of points. x has 11 elements and y1 has 4 elements.

1 Like

Hey @array9703050324!

I’m assuming the exercise is using the x list as the x coordinates and y list as the y coordinates. If that’s the case, then the reason the lists must be the same size is because each x coordinate needs a matching y coordinate. And if x has 11 elements and y has four elements, this would not be met

1 Like

Steven,

Thank you for the reply. I was attempting to use the data from one of the use cases linked in the lesson. If I were to create this outside of script.py would this work?

I also searched about the same issue, and there seems to be a method .twinx().

ax1 = plt.subplot()
ax2 = ax1.twinx()
ax1.plot(x, y1)
ax2.plot(x, y2)
1 Like