+91 81900 98289

Matplotlib Line

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

A line style changes the stroke, not the data. Solid, dashed, and dotted are the usual choices.

Pick a style that still reads in black and white. Do not rely on colour alone.

Example 1: Start here

linestyle="--" is the dashed form of plt.plot.

x = [1, 2, 3] # a list; the values stay in this order y = [2, 4, 6] # a list; the values stay in this order print("dashed", list(zip(x, y))) # zip keeps each pair together

Example 2: List the points

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

points = [(1, 23), (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 = [23, 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 = [23, 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
linestyle="--" is the dashed form of plt.plot.
Call Now 81900 98289