+91 81900 98289

Matplotlib Intro

This Matplotlib Intro lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

Matplotlib draws charts from sequences of numbers. A line chart needs an x sequence and a y sequence of the same length.

The runner does not import matplotlib. The example prepares the pairs a plot would use.

Example 1: Start here

Locally, import matplotlib.pyplot as plt, then plt.plot(hours, scores) and plt.show().

hours = [1, 2, 3, 4] # a list; the values stay in this order scores = [40, 55, 70, 82] # a list; the values stay in this order print(list(zip(hours, scores))) # zip keeps each pair together

Example 2: List the points

Matplotlib Intro draws points. Printing each pair lets you check the data before a chart library plots it.

points = [(1, 19), (2, 12), (3, 18)] # a list; the values stay in this order for x, y in points: # repeat the indented lines once per value print(x, y) # a comma prints each value, with a space between

Example 3: Find the highest bar

The tallest bar is the largest number. max does that lookup.

heights = [19, 12, 18] # a list; the values stay in this order print(max(heights)) # the largest value

Example 4: Label each point

A chart label belongs with its number. zip keeps each day next to its height.

labels = ["Mon", "Tue", "Wed"] # a list; the values stay in this order heights = [19, 12, 18] # a list; the values stay in this order for label, height in zip(labels, heights): # repeat the indented lines once per value print(label, height) # a comma prints each value, with a space between
Locally, import matplotlib.pyplot as plt, then plt.plot(hours, scores) and plt.show().
Call Now 81900 98289