In this post, we will learn some features of matplotlib API through demos. This API collects functions that make matplotlib work like MATLAB.
This post only focuses on features that are often used in Machine learning. It will be continued updating.
1. Plot parabola function y = (x-5)2 with requirements:
2. Axis points are text
3. Multiple figures and subplot
4. Dynamically updating plot
5. Plot 3D surface and its contour
6. Plot contour with gradient vectors
7. Plot scatter
1. Plot parabola function y = (x-5)2 with requirements:
+ '-': solid line style (refer to this)
+ 'r': red
+ grid(True): show grid
+ xlabel('x'): x axis named 'x'
+ ylabel('(x-5)^2'): y axis named '(x-5)^2'
+ plt.axis([0, 10, 0, 20]): set axis limit xmin=0, xmax=10, ymin=0, ymax=20
+ show values of (x, y) at plotted points
Source code:
In machine learning, sometimes we want to plot training set to see how it looks like. We can plot them as markers or scatter (mention later). In order to plot markers, we use the plot() function to plot the points without the lines that connect those points. We reuse the code above but using markers (just replace 'g-' by 'ro'):
2. Axis points are text
The X-axis values are texts [Pecan, Pumpkin, Chess] instead of numbers.
It has the form like below:
Source code:
3. Multiple figures and subplot
We use figure(id) to indicate the figure with id that we want to operate on it. And subplot(nrows, ncols, index) to divide the figure into nrows and ncols areas. And index is to indicate which area that we want to plot on it. E.g: subplot(2, 2, 1) or subplot(221): there is 4 areas and we focus on the top-left area.
subplot(2, 2, 4) or subplot(224): there is 4 areas and we focus on the bottom-right area
Let 's make a demo that using 3 figures:
+ In figure (1), there are 2 plotting areas that plot functions y=x and y=2*x
+ In figure (2), there are 2 plotting areas that plot functions y=3*x and y=4*x
+ In figure (3), there are 4 plotting areas that plot functions y=x, y=2*x, y=3*x and y=4*
Source code:
4. Dynamically updating plot
In machine learning, when the algorithm minimizes the cost function (represents the error between estimated and expected value), we want to monitor the current value comparing to the previous value of the cost function in each step to know the effectiveness of the algorithm. So the graph (X is labeled step, Y is labeled error) will continue updating when the algorithm is running. We make a demo for this requirement. In demo, our cost function is an exponential function
Source code:
5. Plot 3D surface and its contour<
In case we can view the shape of data in 3D mode. We make a demo to plot a 3D Paraboloid and its contour:
$z = \frac{x^{2}}{64} + \frac{y^{2}}{64}$
Source code:
6. Plot contour with gradient vectors
In case It it is not easy too view 3D plot, we can plot contour and gradient vectors. We make a demo to plot the square root function and its gradient:
$z = \sqrt{x^{2} + y^{2}}$
Source code
7. Plot scatter
A scatter plot is to display values for typically two variables for a data set. we make a demo to plot scatter of a 2D Paraboloid function:
$y = x^{2} - 10x + 25$
Source code
This post only focuses on features that are often used in Machine learning. It will be continued updating.
1. Plot parabola function y = (x-5)2 with requirements:
2. Axis points are text
3. Multiple figures and subplot
4. Dynamically updating plot
5. Plot 3D surface and its contour
6. Plot contour with gradient vectors
7. Plot scatter
1. Plot parabola function y = (x-5)2 with requirements:
+ '-': solid line style (refer to this)
+ 'r': red
+ grid(True): show grid
+ xlabel('x'): x axis named 'x'
+ ylabel('(x-5)^2'): y axis named '(x-5)^2'
+ plt.axis([0, 10, 0, 20]): set axis limit xmin=0, xmax=10, ymin=0, ymax=20
+ show values of (x, y) at plotted points
Source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #import matplotlib and pyplot api import matplotlib.pyplot as plt #import numpy to generate (x-5)^2 import numpy as np #just plot x values in range 0, 10 step 0.5 x = np.arange(0, 10, 0.5) #generate y values y = np.power(x-5, 2) #'-': solid line style (refer to this) 'r': red plt.plot(x, y, 'g-') #y axis named '(x-5)^2' plt.ylabel('(x-5)^2') #x axis named 'x' plt.xlabel('x') #set axis limit xmin=0, xmax=10, ymin=0, ymax=20 plt.axis([0, 10, 0, 20]) #show grid plt.grid(True) #show values of (x, y) at plotted points for tx in x: ty = np.power(tx-5, 2) plt.text(tx, ty, '(' + str(tx) + ',' + str(ty) + ')') #now show the graph plt.show() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #import matplotlib and pyplot api import matplotlib.pyplot as plt #import numpy to generate (x-5)^2 import numpy as np #just plot x values in range 0, 10 step 0.5 x = np.arange(0, 10, 0.5) #generate y values y = np.power(x-5, 2) #'-': solid line style (refer to this) 'r': red plt.plot(x, y, 'ro') #y axis named '(x-5)^2' plt.ylabel('(x-5)^2') #x axis named 'x' plt.xlabel('x') #set axis limit xmin=0, xmax=10, ymin=0, ymax=20 plt.axis([0, 10, 0, 20]) #show grid plt.grid(True) #show values of (x, y) at plotted points for tx in x: ty = np.power(tx-5, 2) plt.text(tx, ty, '(' + str(tx) + ',' + str(ty) + ')') #now show the graph plt.show() |
The X-axis values are texts [Pecan, Pumpkin, Chess] instead of numbers.
It has the form like below:
Source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import matplotlib.pyplot as plt #names and values of points names = ['Pecan', 'Pumpkin', 'Chess'] values = [500, 300, 400] #set up figure (1) plt.figure(1) #set locations of names x = [1, 2, 3] #plot bar chart plt.bar(x, values) #map the locations and labels of names plt.xticks(x, names) #set title plt.title('Flavor') #show graph plt.show() |
We use figure(id) to indicate the figure with id that we want to operate on it. And subplot(nrows, ncols, index) to divide the figure into nrows and ncols areas. And index is to indicate which area that we want to plot on it. E.g: subplot(2, 2, 1) or subplot(221): there is 4 areas and we focus on the top-left area.
subplot(2, 2, 4) or subplot(224): there is 4 areas and we focus on the bottom-right area
Let 's make a demo that using 3 figures:
+ In figure (1), there are 2 plotting areas that plot functions y=x and y=2*x
+ In figure (2), there are 2 plotting areas that plot functions y=3*x and y=4*x
+ In figure (3), there are 4 plotting areas that plot functions y=x, y=2*x, y=3*x and y=4*
Source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import matplotlib.pyplot as plt #figure (1) has 2 areas (1 row, 2 cols, idx=1 or 2) plt.figure(1) #focus on the top-left area w index=1 of figure (1) plt.subplot(121) plt.plot([1, 2, 3], [1, 2, 3]) plt.title('y=x') #figure (2) has 2 areas (2 rows, 1 col, idx=1 or 2) plt.figure(2) #focus on the top-left area w index=1 of figure (2) plt.subplot(211) plt.plot([1, 2, 3], [3, 6, 9]) plt.title('y=3*x') #focus on the bottom-left area w index=1 of figure (2) plt.subplot(212) plt.plot([1, 2, 3], [4, 8, 12]) plt.title('y=4*x') #re-active figure (1) to continue plotting plt.figure(1) #focus on the top-right area w index=2 of figure (1) plt.subplot(122) plt.plot([1, 2, 3], [2, 4, 6]) plt.title('y=2*x') # the figure (3) has 4 areas (2 rows, 2 cols, idx=1,2,3,4) plt.figure(3) #focus on top-left area idx=1 plt.subplot(221) plt.plot([1, 2, 3], [1, 2, 3]) plt.title('y=x') #focus on top-right area idx=2 plt.subplot(222) plt.plot([1, 2, 3], [2, 4, 6]) plt.title('y=2*x') #focus on bottom-left area idx=3 plt.subplot(223) plt.plot([1, 2, 3], [3, 6, 9]) plt.title('y=3*x') #focus on bottom-right area idx=4 plt.subplot(224) plt.plot([1, 2, 3], [4, 8, 12]) plt.title('y=4*x') plt.show() |
In machine learning, when the algorithm minimizes the cost function (represents the error between estimated and expected value), we want to monitor the current value comparing to the previous value of the cost function in each step to know the effectiveness of the algorithm. So the graph (X is labeled step, Y is labeled error) will continue updating when the algorithm is running. We make a demo for this requirement. In demo, our cost function is an exponential function
Source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import matplotlib.pyplot as plt import numpy as np #prepare the plot plt.figure(1) ax = plt.gca() ax.set_autoscale_on(True) #label of axes plt.ylabel('error') plt.xlabel('step') g, = plt.plot([], []) def update_line(g, x, y): g.set_xdata(np.append(g.get_xdata(), x)) g.set_ydata(np.append(g.get_ydata(), y)) ax.relim() ax.autoscale_view(True,True,True) plt.draw() plt.pause(0.1) #end #step data steps = np.linspace(-1, 2, 100) for step in steps: #just call update_line and pass new data to it update_line(g, step, np.exp((-1)*step)) plt.show() |
In case we can view the shape of data in 3D mode. We make a demo to plot a 3D Paraboloid and its contour:
$z = \frac{x^{2}}{64} + \frac{y^{2}}{64}$
Source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt from matplotlib import cm import numpy as np #setup 3D graph fig = plt.figure() #get axes ax = fig.gca(projection='3d') #create data xlist = ylist = np.linspace(-30.,30.,30) X, Y = np.meshgrid(xlist, ylist) #plot Z = X^2 + Y^2 Z = X*X/64 + Y*Y/64 #setup axes ax.set_xlabel('X') ax.set_xlim(-33, 33) ax.set_ylabel('Y') ax.set_ylim(-33, 33) ax.set_zlabel('Z') ax.set_zlim(-33, 35) #plot 3D surface ax.plot_surface(X, Y, Z, rstride=2, cstride=2, alpha=0.3) #plot contour ax.contourf(X, Y, Z, zdir='z', offset=-33, cmap=cm.coolwarm) plt.show() |
In case It it is not easy too view 3D plot, we can plot contour and gradient vectors. We make a demo to plot the square root function and its gradient:
$z = \sqrt{x^{2} + y^{2}}$
Source code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import numpy as np import matplotlib.pyplot as plt #setup data xlist = ylist = np.linspace(-10.,10.,11) X, Y = np.meshgrid(xlist, ylist) #calculate Z=X^2 + Y^2 Z = np.sqrt(X**2 + Y**2) #calculate gradient dhdx, dhdy = np.gradient(Z) #setup graph plt.figure() plt.title('Filled Contours Plot') plt.xlabel('x (cm)') plt.ylabel('y (cm)') #plot contour cp = plt.contourf(X, Y, Z) plt.colorbar(cp) #plot gradient vectors plt.quiver(X,Y,dhdx,dhdy) plt.show() |
A scatter plot is to display values for typically two variables for a data set. we make a demo to plot scatter of a 2D Paraboloid function:
$y = x^{2} - 10x + 25$
Source code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #import matplotlib and pyplot api import matplotlib.pyplot as plt #import numpy to generate (x-5)^2 import numpy as np #just plot x values in range 0, 10 step 0.5 x = np.arange(0, 10, 0.5) #generate y values y = np.power(x-5, 2) #use scatter function to plot plt.scatter(x, y, marker=r'$\clubsuit$') #y axis named '(x-5)^2' plt.ylabel('(x-5)^2') #x axis named 'x' plt.xlabel('x') #set axis limit xmin=0, xmax=10, ymin=0, ymax=20 plt.axis([0, 10, 0, 20]) #show grid plt.grid(True) #now show the graph plt.show() |
0 Comments