0
0
Matplotlibdata~10 mins

Plotting multiple lines in Matplotlib - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to plot two lines on the same graph using matplotlib.

Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3]
y1 = [2, 4, 6]
y2 = [1, 3, 5]
plt.plot(x, y1)
plt.[1](x, y2)
plt.show()
Drag options to blanks, or click blank then click option'
Aplot
Bscatter
Cbar
Dhist
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.scatter or plt.bar instead of plt.plot for lines.
Forgetting to call plt.plot for the second line.
2fill in blank
medium

Complete the code to add labels to each line in the plot.

Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3]
y1 = [2, 4, 6]
y2 = [1, 3, 5]
plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, [1]='Line 2')
plt.legend()
plt.show()
Drag options to blanks, or click blank then click option'
Atitle
Blabel
Cxlabel
Dylabel
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'title' or 'xlabel' instead of 'label' for line names.
Forgetting to call plt.legend() to display labels.
3fill in blank
hard

Fix the error in the code to plot multiple lines with different colors.

Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3]
y1 = [2, 4, 6]
y2 = [1, 3, 5]
plt.plot(x, y1, color='red')
plt.plot(x, y2, color='[1]')
plt.show()
Drag options to blanks, or click blank then click option'
Ayellowish
B3
Cblue
Dgreenish
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid color names or numbers for the color argument.
Forgetting quotes around color names.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each x to its y1 squared, but only if y1 is greater than 3.

Matplotlib
result = {x: y1[1]2 for x, y1 in zip(x, y1) if y1 [2] 3}
Drag options to blanks, or click blank then click option'
A**
B>
C<
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' instead of '**' for power.
Using '<' instead of '>' in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each line label to the sum of its y values if the sum is greater than 5.

Matplotlib
data = {'Line 1': y1, 'Line 2': y2}
result = [1]: sum([2]) for [1], [2] in data.items() if sum([2]) > 5}
Drag options to blanks, or click blank then click option'
Alabel
Bvalues
Cy
Dline
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Not using the variable names from the options pool.