Pyplot Simple Examples

Line Graph: plot() function

import matplotlib.pyplot as plt
x = [2  ,5, 10, 20, 24]
y = [100,90,75, 22, 5]
plt.plot(x, y)
plt.savefig('filename.png')
plt.show()

Scatter Plot: scatter()

import matplotlib.pyplot as plt
x = [2  , 5,10, 20, 24]
y = [100,90,75, 22, 5]
plt.scatter(x, y)
plt.savefig('filename.png')
plt.show()

Pie Chart: pie() function

import matplotlib.pyplot as plt
pets = ['cats','dogs','fish']
sizes = [50, 40, 10]
plt.pie(sizes, labels=pets)
plt.savefig('filename.png')
plt.show()

Bar Chart: bar() function

import matplotlib.pyplot as plt
pets = ['cats','dogs','fish']
sizes = [50, 40, 10]
#index: just a list of numbers
index = range(len(sizes))
plt.bar(index, sizes)
plt.xticks(index, pets)
plt.savefig('filename.png')
plt.show()

Histogram: hist() function

import matplotlib.pyplot as plt
data = [1,2,5,3,2,4,2,5,7,9,0,3,9,4,6,4,8,1,5,0,7,5,3,2,0,8]
plt.hist(data, bins=5)
plt.savefig('filename.png')
plt.show()

If your Python environment doesn’t allow “plt.show()”, you can instead save it as a file to view the graphic. Replace the “plt.show()” command with something like this in each example:

plt.savefig('filename.png')