Plotting with Matplotlib

May 19, 2019 2 min read Machine Learning Pyplot

Matplotlib is a Python library used for visualizing data. It is an extensive library and sub-modules like pyplot provides functionality equivalent to Matlab.

Let’s start by understanding few basic components :-

Figure Figure is the top level window or container that everything is drawn upon.

Axes Axes is the area on which we plot the data and will have associated labels and ticks.

import matplotlib
matplotlib.use('TkAgg')
import\ matplotlib.pyplot as plt

fig = plt.fig()
ax = plt.axes()
plt.show()```

Above code will create a new container (figure) and add an axes. We use the call plt.show() to visualize the plot. If you notice, it will be an empty bounding box with ticks (this empty box is axes). A figure can have multple axes. Axes can have 2 (X-Axis and Y-Axis) or 3 Axis objects. (3 in case of 3D)

# AxisAxis is a number line object which takes care of graph limits and generating the ticks.

A typical plot will start with a figure, then axes will be added to it followed by a call to one of the plotting method. There are two commonly used methods - plot and scatter. Plot draw points with line connecting them, while scatter draws unconnected points.

*Plot Example*

```python
import matplotlib
matplotlib.use('TkAgg')
import\ matplotlib.pyplot as plt

fig = plt.figure()
## Add axes using add_subplot - 1 row and 1 column on grid
ax = fig.add_subplot(111)

## Use plot method from Axes object to plot data passed using X & Y (linear data)
ax.plot([0,1,2,3,4], [0,10,20,30,40], color='blue',  linewidth=2)

## Set the limit, label on X & Y Axis
ax.set_xlim([-1,5])
ax.set_ylim([-5,50])
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_title('Example Plot')
plt.show()```

[![Blogger Image](../assets/images/plotting-with-matplotlib-Image_1_Matplotlib.png)](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhnUZ1Jmxyi7yRAxEzgTBJnYDze2GXJ4seUSOZNsaWeBbIYrZQjpY5KrZTlPRUZ5keMCVLVe3NMLsIPxKf2d7pSkkW_izCYhXfstWLYfdjF08EEVPObzEAec0X9gHbISKysJ0ZfKG5iJz4r/s1600/Image_1_Matplotlib.png)

*Scatter Example*

In this example scatter method is called from the axis object, which draws unconnected points.

```python
import matplotlib
matplotlib.use('TkAgg')
import\ matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter([0,1,2,3,4], [0,10,20,30,40])
plt.show()```

[![Blogger Image](../assets/images/plotting-with-matplotlib-Image_2_Matplotlib.png)](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRTUccCloSBm88VwEL9S6ZUduOdE_lA7njQOoilVWXjX-HTRboCBkGhCDAz2tgyBVhC550CvBqvob7bw7UsJL2XhpFHrnXeGxztFUryR1Vp8nY_MNHa7l0XQ7W-FKuiX0hja31kUjdnmEX/s1600/Image_2_Matplotlib.png)

# Pyplot methods & Axes methods

In our examples above, we have used the methods from Axes object, whereas we can also use the methods from Pyplot as well. Infact, all the Axes methods exist with Pyplot and behind the scenes will be tranformed to call one of the method from Axes object.

In the example below, we are achieving plotting the same plot using pyplot methods.

```python
import matplotlib
matplotlib.use('TkAgg')
import\ matplotlib.pyplot as plt

plt.plot([0,1,2,3,4], [0,10,20,30,40], color='red',  linewidth=2)
plt.xlim(-1,5)
plt.ylim(-5,50)
plt.scatter([0,1,2,3,4], [0,10,20,30,40])
plt.show()```

[![Blogger Image](../assets/images/plotting-with-matplotlib-Image_3_Matplotlib.png)](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7qP5nop5PouJLrA92BtPubr-3eVEyyGsRp_-ixMvZOYFQlLDyLenCyC574EGpTpou75gYkjZCrddQNvPgO4PSfhEY61TUh0qQ7Vcahp0zo8-ni0dVuiLcVrpHYYh30niw9tdTtXRVIb8V/s1600/Image_3_Matplotlib.png)

Comments