Artificial Intelligence · Class 10 · Practical File · Program 4 of 15
Charts with MatplotlibFrom the CBSE curriculum list⏱️ 6 min read
Program 4: A Line Chart from (2, 5) to (9, 10)
Write a program to display a line chart from (2, 5) to (9, 10).
Download
All 15 programs with output, in one PDF1Aim
Write a program to display a line chart from (2, 5) to (9, 10).
Python 3Matplotlib
2Program
line_chart.py
# Program 4: Display a line chart from (2, 5) to (9, 10)
import matplotlib.pyplot as plt
x = [2, 9] # x values of the two points
y = [5, 10] # y values of the two points
plt.plot(x, y, color="green", marker="o", linewidth=2)
plt.title("Line chart from (2, 5) to (9, 10)")
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.grid(True)
plt.show()
# The slope of the line: how much y rises for each step in x.
slope = (y[1] - y[0]) / (x[1] - x[0])
print("The line rises", round(slope, 3), "in y for every 1 in x")3Output
The output of a real run, exactly as it appeared.
Output
The line rises 0.714 in y for every 1 in x
the window opened by plt.show()
4How it works
x = [2, 9] and y = [5, 10]Matplotlib takes all the x values in one list and all the y values in another. The two points are (2, 5) and (9, 10).
plt.plot(x, y, color="green", marker="o")Draws a line through the points; marker="o" puts a dot on each point.
plt.title / plt.xlabel / plt.ylabelA chart without a title and axis labels loses marks — always add them.
plt.show()Displays the chart. Nothing appears on screen without it.
5Try this
- Change the colour to red and the line style to dashed (linestyle="--").
- Add a third point, (12, 4).
- Draw a second line from (2, 10) to (9, 5) on the same chart.
6Viva questions
▸Which function draws a line chart?
plt.plot().
▸How are the points (2, 5) and (9, 10) given to plt.plot()?
As two lists: x = [2, 9] and y = [5, 10].
▸What does plt.show() do?
It displays the chart.
Tip
In your practical file, write the aim, the program and the output for each program — run it yourself and copy your own output. See The Practical File for how to set the file out.