+91 81900 98289

Matplotlib Subplot

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

A subplot is one chart inside a figure that holds several charts. Give each subplot its own title.

The example keeps two series apart instead of drawing them on one axis.

Start here

plt.subplot(1, 2, 1) selects the first cell of a one-by-two figure.

morning = [70, 75, 80] # a list; the values stay in this order afternoon = [82, 78, 90] # a list; the values stay in this order print("morning", morning) # a comma prints each value, with a space between print("afternoon", afternoon) # a comma prints each value, with a space between

List the points

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

points = [(1, 9), (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

Find the highest bar

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

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

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 = [9, 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
plt.subplot(1, 2, 1) selects the first cell of a one-by-two figure.