+91 81900 98289

Matplotlib Get Started

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

Install Matplotlib into the same virtual environment as the project. Import pyplot, which is the module most examples call.

Check the install from a terminal with python -m pip show matplotlib. This page cannot install it for you.

Example 1: Start here

Run the install command in a terminal, not in the try-it editor.

print("python -m pip install matplotlib") # print writes this; the quotes are not shown print("import matplotlib.pyplot as plt")

Example 2: List the points

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

points = [(1, 35), (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 = [35, 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 = [35, 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
Run the install command in a terminal, not in the try-it editor.
Call Now 81900 98289