Options in matplotlib.pyplot

The pyplot simple examples page shows some of the quickest code examples for getting you started. Start there.

When you have tried those, these examples will show you how to add some options to your charts.

Further details can be found in the documentation at https://matplotlib.org/stable/index.html.

Bar chart with colors and titles:

import matplotlib.pyplot as plt
pets = ['cats','dogs','fish']
sizes = [50, 40, 10]
index = range(len(sizes))
c = ['red', 'blue', 'purple']
plt.title('Fancy Bar Chart', fontsize=30)
plt.xlabel('X axis label here', fontsize=20)
plt.ylabel('Y axis label here')
plt.bar(index, sizes, color=c)
plt.xticks(index, pets)
plt.legend()
plt.savefig('bar_options.png')

Bar chart with two sets of bars:

import matplotlib.pyplot as plt
griff = [20, 50, 30, 10]
slyth = [60, 10, 5, 2]
labels = ['Sep', 'Oct', 'Nov', 'Dec']

index_g = [0, 2, 4, 6]
index_s = [0.5, 2.5, 4.5, 6.5]
index_L = [0.25, 2.25, 4.25, 6.25]

plt.bar(index_g, griff, width=0.5, color='red', label='Griff')
plt.bar(index_s, slyth, width=0.5, color='green', label='Slyth')
plt.title('House Cup Points')
plt.xticks(index_L, labels)
plt.legend()
plt.savefig('housecup.png')

Line graph with options:

import matplotlib.pyplot as plt
x = [2  , 5,  10, 20, 24]
y = [100, 90, 75, 22, 5]
z = [40,  60, 50, 30, 70]
plt.plot(x, y, 'b--', label="y_data")
plt.plot(x, z, 'r-', label="z_data")
plt.legend()
plt.title('Use plt.plot() to make Line Graph')
plt.xlabel('Time on this axis')
plt.ylabel('Changing Variable')
plt.savefig('filename.png')
plt.show()

Scatter Plot with Color and Size Data

import matplotlib.pyplot as plt

population = [5000, 2000, 15000, 7000]
weather =    [20.0,    40.0,    75.0,   65.0]
income =     [3000, 5000, 4200,  7100]
age =        [32.0, 40.5, 70.8, 55.2]
#Divide each pop. value by 100 for sizing to scale it more nicely
sizes = [p/100 for p in population]
plt.scatter(age, income, s=sizes, c=weather, cmap='jet')
plt.title('Graph with Color and Size Data')
plt.xlabel('Average Age')
plt.ylabel('Average Income')
plt.colorbar()
plt.savefig('colorgraph.png')

Scatter plot with two data sets:

import matplotlib.pyplot as plt
x = [1, 2, 5, 10, 17, 26]
y = [1, 4, 25, 100, 289, 676]
z = [208, 226, 280, 370, 496, 658]

plt.scatter(x, y, c='red', label='Apple')
plt.scatter(x, z, c='yellow', label='Banana')
plt.title('Arbitrary Fruits vs Arbitrary Numbers')
plt.legend()
plt.show()
plt.savefig('output.png')

Histogram with some options:

# First 4 lines generates some random data to use
import random
vals = []
for i in range(100):
    vals += [random.gauss(12, 3.5)]

import matplotlib.pyplot as plt
r = (2, 18) # set min and max range of bins
plt.hist(vals, width=0.5, bins=5, range=r)
plt.savefig('regular.png')

#Try it with cumulative=True and see the difference:
plt.hist(vals, width=0.5, bins=5, range=r, cumulative=True)
plt.savefig('cumulative.png')

Plot individual points

import matplotlib.pyplot as plt

xvals = [3,   4,  5,  6, 10]
yvals = [20, 18, 40, 32, 11]
labels = ['abc', 'xyz', '2013', 'Fred', 'potato']

# zip combines lists conveniently for a loop
for x, y, label in zip(xvals, yvals, labels):
    # plot each point as a blue X
    plt.plot(x, y, 'bx')
    plt.text(x, y+0.8, label)

# plot one additional point with a red marker
plt.plot(8, 25, 'ro')
plt.text(8.5, 25, 'Wow!')

# plt.show()
plt.savefig('wow.png')