See how one simple graph can replace dozens of confusing charts and save you hours!
Why Plotting multiple lines in Matplotlib? - Purpose & Use Cases
Imagine you have sales data for several products over a year. You want to see how each product performed month by month. Trying to draw each product's sales trend by hand on paper or using separate charts is confusing and time-consuming.
Manually drawing or creating separate charts for each product makes it hard to compare trends side by side. It takes a lot of time, and mistakes easily happen when copying numbers or aligning points. You lose the big picture.
Plotting multiple lines on the same graph lets you see all product trends together clearly. With just a few lines of code, you can draw each product's sales line in different colors, making comparison easy and fast.
plt.plot(months, sales_product1) plt.show() plt.plot(months, sales_product2) plt.show()
plt.plot(months, sales_product1, label='Product 1') plt.plot(months, sales_product2, label='Product 2') plt.legend() plt.show()
You can quickly compare multiple data trends in one clear, colorful chart to make smarter decisions.
A store manager compares monthly sales of different products on one graph to spot which items are growing or declining, helping decide what to stock more.
Manual plotting is slow and error-prone for multiple lines.
Plotting multiple lines together shows clear comparisons.
It saves time and reveals insights at a glance.
Practice
matplotlib?Solution
Step 1: Understand plotting multiple lines
In matplotlib, to plot multiple lines on the same graph, you callplt.plot()for each line's data before showing the plot.Step 2: Identify correct plotting function
plt.scatter(),plt.bar(), andplt.hist()are for scatter plots, bar charts, and histograms, not line plots.Final Answer:
Callplt.plot()multiple times with different data sets beforeplt.show()-> Option CQuick Check:
Multipleplt.plot()calls = multiple lines [OK]
- Using plt.scatter() instead of plt.plot() for lines
- Calling plt.show() after each line instead of once
- Using bar or histogram functions for line plots
Solution
Step 1: Check order of plotting and showing
To plot multiple lines, callplt.plot()for each line, then callplt.show()once at the end.Step 2: Identify correct snippet
plt.plot([1, 2, 3], [4, 5, 6]) plt.plot([1, 2, 3], [6, 5, 4]) plt.show() callsplt.plot()twice thenplt.show(), which is correct. plt.plot([1, 2, 3], [4, 5, 6]) plt.show() plt.plot([1, 2, 3], [6, 5, 4]) callsplt.show()too early, splitting the plots. plt.show() plt.plot([1, 2, 3], [4, 5, 6]) plt.plot([1, 2, 3], [6, 5, 4]) callsplt.show()before plotting, so nothing shows. plt.plot([1, 2, 3], [4, 5, 6]) plt.plot([1, 2, 3], [6, 5, 4]) missesplt.show()entirely.Final Answer:
Two plt.plot() calls followed by plt.show() -> Option AQuick Check:
Plot lines first, show once after [OK]
- Calling plt.show() before all plots
- Forgetting plt.show() to display plot
- Plotting after plt.show() call
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, label='Line 2') plt.legend() plt.show()
Solution
Step 1: Analyze plotting commands
The code plots two lines withplt.plot()and assigns labels 'Line 1' and 'Line 2'.Step 2: Understand legend usage
Callingplt.legend()displays the legend with the labels for both lines.Final Answer:
A graph with two lines labeled 'Line 1' and 'Line 2' -> Option BQuick Check:
Labels + plt.legend() = labeled lines [OK]
- Forgetting plt.legend() so labels don't show
- Assigning labels incorrectly
- Plotting only one line
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, label='Line 2') plt.legend plt.show()
Solution
Step 1: Check function calls
The code callsplt.legendwithout parentheses, so the legend function is not executed.Step 2: Understand effect of missing parentheses
Without callingplt.legend(), the legend does not appear, so labels won't show.Final Answer:
Missing parentheses after plt.legend() call -> Option AQuick Check:
Call plt.legend() with () to show legend [OK]
- Forgetting () after plt.legend
- Assuming legend shows without calling function
- Mixing up order of plt.show()
Solution
Step 1: Understand color and style shorthand
In matplotlib, shorthand like 'r-' means red solid line, 'g--' green dashed, 'b:' blue dotted.Step 2: Check label usage and legend
plt.plot(x, y1, 'r-', label='Red solid') plt.plot(x, y2, 'g--', label='Green dashed') plt.plot(x, y3, 'b:', label='Blue dotted') plt.legend() plt.show() assigns labels to all lines and callsplt.legend()to show them. plt.plot(x, y1, color='red', linestyle='solid', label='Red solid') plt.plot(x, y2, color='green', linestyle='dashed', label='Green dashed') plt.plot(x, y3, color='blue', linestyle='dotted', label='Blue dotted') plt.legend() plt.show() uses invalid linestyle names ('solid', 'dashed', 'dotted' are not valid; correct are '-', '--', ':'). plt.plot(x, y1, 'red', 'solid', label='Red solid') plt.plot(x, y2, 'green', 'dashed', label='Green dashed') plt.plot(x, y3, 'blue', 'dotted', label='Blue dotted') plt.legend() plt.show() uses positional strings incorrectly. plt.plot(x, y1, 'r-', label='Red solid') plt.plot(x, y2, 'g--') plt.plot(x, y3, 'b:') plt.legend() plt.show() misses labels for second and third lines.Final Answer:
Shorthand format strings like 'r-', 'g--', 'b:' for each plt.plot() -> Option DQuick Check:
Use shorthand 'color-style' and label each line [OK]
- Using invalid linestyle names like 'solid'
- Forgetting labels on all lines
- Not calling plt.legend() to show labels
Ready to prove it?
Pass the test and this pattern moves unknown
