__MatPlotLib Tutorial__

__MatPlotLib Tutorial__

**Introduction:** Matplotlib is a tool for data visualization and this tool built upon the Numpy and Scipy framework. It was developed by John Hunter in 2002. Matplotlib is a library for making 2D plots of arrays in Python. Matplotlib also able to create simple plots with just a few commands and along with limited 3D graphic support. It can provide quality graph/figure in interactive environment across platforms. It can also be used for animations as well. Below mentioned are some advantages of MatPlotLib:

- It could be used on any operating system via its array of backend.
- It has a familiar interface: one similar to MatLab it had a coherent vision: to do 2D graphics, and do them well.
- Great control of every element in a figure, including figure size and DPI.
- High-quality output in many formats, including PNG, PDF, SVG, EPS, and PGF.

**Line Plots**

We are going to make 3 python list that contain information about sales and advertisement medium (TV and Radio). We will use this list for making Line plots in python. First we import matplotlib library and give shortcut name as plt.

```
import matplotlib.pyplot as plt
y = [1, 4, 9, 16, 25,36,49, 64]
x1 = [1, 16, 30, 42,55, 68, 77,88]
x2 = [1,6,12,18,28, 40, 52, 65]
```

We have loaded matplotlib, and have some data for making line plot, we can start putting some simple code

```
# show() command for display figure
plt.plot(tv, sales)
plt.show()
```

We can also give name of axis (axis label name) and create title of plot. Here's the syntax to create the required plot:

```
plt.plot(tv, sales)
plt.xlabel('TV')
plt.ylabel('Sales')
plt.title('Advertisement effect on sales')
plt.show()
```

**Learn Data Science by working on interesting Data Science Projects for just $9**

We saw above as how to make label on axis and make title. Matplotlib offers a variety of options for color, linestyle, and marker and we will use code in next figure. These are:

Colour Code | Colour Displayed |
---|---|

R | Red |

B | Blue |

G | Green |

C | Cyan |

M | Magneta |

Y | Yellow |

K | Black |

W | White |

Marker Code | Marker Displayed |
---|---|

+ | Plus Sign |

. | Dot |

o | Circle |

* | Star |

P | Pentagon |

S | Square |

X | X Character |

D | Diamond |

H | Hexagon |

^ | Triangle |

Linestyle Code | Linestyle Displayed |
---|---|

- | Solid Line |

-- | Dashed Line |

:::: | Dotted Line |

-.-.-. | Dash Dotted Line |

None | No Connecting Lines |

We are going to see how both advertisement medium (TV and radio) apply on one figure against sales.

```
plt.plot(tv,sales, marker='o', linestyle='--', color='r',label='tv')
plt.plot(radio,sales, marker='*', linestyle='-', color='g', label='raddio')
plt.xlabel('Advertisement medium')
plt.ylabel('Sales')
plt.title('Advertisement effect on sales')
plt.legend(loc='lower right')
plt.show()
```

The above output figure code included legend also. We gave label name within plot() and using marker code, linestyle code and color code. You can also change line width with lw() function in plot.

**Scatter Plot**

We generate random number for making scatter plot and we will make multiple plot in one figure (subplots). First we import numpy library for creating random number.

```
## create Random numbers
import numpy as np
x = np.random.randn(1, 50)
y = np.random.randn(1,50)
plt.scatter(x1, y, color='red', s=30) ## here s is size of point in scatter plot
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.title('Scatter Plot')
plt.show()
```

We can also use different size and color together and make grid in plot. Let’s see how to make:

```
fig = plt.figure()
## here 1 show number of row, 2 show number of column and 1 show number of subplot
##Left plot
img1 = fig.add_subplot(121)
N=50
x = np.random.randn(N)
y = np.random.randn(N)
colors = np.random.rand(N)
size =(10 * np.random.rand(N))**2
plt.scatter(x, y, s=size, c=colors, alpha=0.5)
## right plot
img2 = fig.add_subplot(122)
N=100
x1 = np.random.randn(N)
y1 = np.random.randn(N)
area= (15 * np.random.rand(N))**2
colors = ['red', 'blue', 'green', 'yellow']
plt.scatter(x1, y1, s=area, c=colors, alpha=0.2)
img2.grid(True)## show grid in plot
plt.show()
```

**Histogram**

We will use data as 1000 random number drawn from a Gaussian distribution. This is normal distribution or you can say bell curved data. We will use a Gaussian centered about 2.0, with a standard deviation of 3.0 (by default Gaussian distribution mean is zero and standard deviation is 1).

```
## Generate gaussian number
# mean = 2.0
# std = 3.0
# number of points = 1000
data = np.random.normal(2.0, 3.0, 1000)
plt.hist(data)
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.show()
```

The above plot represents 1000 data point in 10 bins. The bins look small. Let’s try to change bins in above histogram and convert frequency (y axis) to probability (in percentage). You also want to histogram plot as unfilled, stepped line rather than filled bars. Let’s try to show two plots in one figure.

```
fig = plt.figure()
## Generate gaussian number
# mean = 2.0
# std = 3.0
# number of points = 1000
##Left plot
img1 = fig.add_subplot(121)
data = np.random.normal(2.0, 3.0, 1000)
plt.hist(data, bins=20, normed=True)
plt.xlabel('Value')
plt.ylabel('Probability')
plt.title('Histogram')
## right plot
img2 = fig.add_subplot(122)
data = np.random.normal(2.0, 3.0, 1000)
plt.hist(data, bins=20, histtype='step')
plt.xlabel('Value')
plt.ylabel('Probability')
plt.title('Histogram')
plt.show()
```

**Customization**

Customizing plots with specific ticks, lables and colours might come in handy. MatPlotLib has various functions to acheive that. In this section, we will check out the syntaxes for those customizations.

**Ticks-**We are going to change x axis label ticks and y label ticks

```
fig = plt.figure()
## Left plot
img1 = fig.add_subplot(1, 2, 1)
img1.set_xticks([0.1, 0.3, 0.6, 0.9])
img1.set_yticks([0.2, 0.4, 0.5, 0.8])
plt.xlabel('X range')
plt.ylabel('Y range')
plt.title('Numeric ticks')
## Right plot
img2 = fig.add_subplot(1, 2, 2)
img2.set_xticks([0.1, 0.3, 0.6, 0.9])
img2.set_xticklabels(['A','B','C','D'])
img2.set_yticks([0.2, 0.4, 0.5, 0.8])
img2.set_yticklabels(['One','Two','Three','Four'])
plt.xlabel('X range')
plt.ylabel('Y range')
plt.title('Character Ticks')
plt.show()
```

We can also hide x axis or y axis from plot and we can change the font size of label and style of label.

```
fig = plt.figure()
img1 = fig.add_subplot(1, 1, 1)
img1.set_xticks([0.1, 0.3, 0.6, 0.9])
img1.set_xticklabels([])
img1.set_yticks([0.2, 0.4, 0.5, 0.8])
# Set the font size
img1.set_title("Label Plot", fontsize='large', color='blue')
# set properties of font style
for tick in img1.yaxis.get_ticklabels():
tick.set_fontsize('large')
tick.set_fontname('Times New Roman')
tick.set_color('Red')
tick.set_weight('bold')
plt.show()
```

We are already shown how to use color in scatter plot above. Let’s try colorbar on scatter plot.

```
fig = plt.figure()
img1 = fig.add_axes([0.1, 0.1, 0.6, 0.8])
N=500
x = np.random.randn(N)
y = np.random.randn(N)
colors = np.random.rand(N)
size =(10 * np.random.rand(N))**2
scat = img1.scatter(x, y, color=colors, s=size, edgecolor='none')
img1.set_xlim(0., 1.)
img1.set_ylim(0., 1.)
clrbar_img = fig.add_axes([0.7, 0.1, 0.05, 0.8])
fig.colorbar(scat, cax=clrbar_img)
plt.show()
```

We can also change font size as you want. Let’s see:

```
### rcParams are the default parameters for matplotlib
import matplotlib as mpl
plt.rcParams['font.size'] = 11.
plt.rcParams['font.family'] = 'Comic Sans MS'
plt.rcParams['axes.labelsize'] = 15.
plt.rcParams['xtick.labelsize'] = 10.
plt.rcParams['ytick.labelsize'] = 10.
y = [1, 4, 9, 16, 25,36,49, 64]
x1 = [1, 16, 30, 42,55, 68, 77,88]
x2 = [1,6,12,18,28, 40, 52, 65]
plt.plot(x1,y, marker='s', linestyle='-', color='y',label='tv')
plt.plot(x2,y, marker='o', linestyle='--', color='g', label='raddio')
plt.xlabel('advertisement medium')
plt.ylabel('Sales')
plt.title('Advertisement effect on sales')
plt.legend(loc='lower right')
plt.show()
```