Matplotlib's pyplot module is a collection of functions, and is makes pyplot work like MATLAB. Various states are preserved across function calls, and this keeps track of things like the current figure, plotting area, and the plotting functions are directed to the current axes.


In [26]:
import matplotlib.pyplot as plt
import numpy as np

plt.plot([1,2,3,4])
plt.show()


If you pass a single array into the plot function it will assume that each value is a y value. However if you pass two arrays it will assume that value at the corresponding index of the array is an x,y value, for instance:


In [27]:
# the first array ix the x value and the second array is the y value
plt.plot([0,4,10],[1,2,20])
plt.show()


If you pass in a matrix containing matching length arrays, each corresponding index will be considered a variable. For instance below the array length is three, and three lines are drawn, with the starting value as the value in the first array, and the ending value in the second array.


In [28]:
plt.plot([[1,2,10],[2,4,6]])
plt.show()


Here is a matrix of a different size:


In [29]:
plt.plot([[1,2,10,2],[2,4,6,6],[2,4,6,10]])
plt.show()


You can change the plot line markers like so:


In [30]:
plt.plot([[1,2,10],[2,4,6]],'ro')
plt.show()


The example above makes more sense when we break up the matrix in the same way pyplot does behind the scences then show markers for each variable:


In [31]:
plt.plot([1,2],'ro',[2,4],'bo',[10,6],'g^')
plt.plot([1,2],'r--',[2,4],'b--',[10,6],'g--')
plt.show()


Pyplot allows you to plot using dictionaries and passing that dictionary to the data parameter. For example:


In [45]:
data = {'y_a':np.random.randn(50)}
plt.plot('y_a',data=data)
plt.ylabel('y_a')
plt.show()



In [46]:
names = ['group_a', 'group_b', 'group_c']
values = [1, 10, 100]

plt.figure(figsize=(9, 3))

plt.subplot(131)
plt.bar(names, values)
plt.subplot(132)
plt.scatter(names, values)
plt.subplot(133)
plt.plot(names, values)
plt.suptitle('Categorical Plotting')
plt.show()



In [ ]: