In this post we will learn some basics features of matplotlib pyplot API through demos. This API collects functions that make matplotlib work like MATLAB. It supports: creates a figure, plots lines in a plotting area, labeled plot, ...

The X-axis values are texts [Pecan, Pumpkin, Chess] instead of numbers.

It has the form like below:

We use

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*

**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 x*_{min}=0, x_{max}=10, y_{min}=0, y_{max}=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() |

**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:*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() |

**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:*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() |