Matplotlib Tutorial Part - 02

This article is continuation from the previous article [Matplotlib Tutorial Part - 01](https://qiita.com/qualitia_cdev/items/3b4ed661364b42e691fa).

##### 4) Subplots (Multiple graphs).
It's possible to generate a figure that contains multiple graphs. It's called subplot.
These multiple graphs are organized in a grid.  To form a subplot, call the subplot function, define the number of rows and columns in the figure, and the index of the subplot you want to draw on (beginning from 1, left to right, and top to bottom).

```matplot_lib.py
x = np.linspace(-2, 2, 50)
plt.subplot(2, 2, 1) #  1st subplot = top left
plt.plot(x, x)
plt.subplot(2, 2, 2) # 2nd subplot = top right
plt.plot(x, x**2)
plt.subplot(2, 2, 3) # 3rd subplot = bottow left
plt.plot(x, x**3)
plt.subplot(2, 2, 4) #  4th subplot = bottom right
plt.plot(x, x**5)
plt.show()
```
![download.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/99c7469d-047b-69f9-9ed9-3c2f68a94175.png)


When we need more complex subplot positioning, we can use subplot2grid function. We can define the number of rows and columns in the grid, then  position of the subplot’s in that grid (ex: top-left = (0,0)), and optionally how many rows and(or) columns it traverses. 

```matplot_lib.py
plt.subplot2grid((3,3), (0, 0), rowspan=2, colspan=2)
plt.plot(x, x**1)
plt.subplot2grid((3,3), (0, 2))
plt.plot(x, x**2)
plt.subplot2grid((3,3), (1, 2), rowspan=2)
plt.plot(x, x**3)
plt.subplot2grid((3,3), (2, 0), colspan=2)
plt.plot(x, x**4)
plt.show()
```


![download (1).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/e6fd99aa-053c-bf97-676a-1a0ac0c9a540.png)

##### 5) Inserting Text

On some occasions, we need to add text inside the figures. We can add text in any location by defining the horizontal and vertical coordinates.

```matplot_lib.py
import numpy as np
x = np.linspace(-4, 4, 100)
y = x**2

point_x = 3
point_y = point_x**2

plt.plot(x, y,"b-")
plt.plot(point_x, point_y, "ro")

plt.text(0, 15, "Square function\n$y = x^2$", fontsize=10, color='blue', horizontalalignment="right",verticalalignment="top")
plt.text(point_x - 0.05, point_y, "Point", ha="right", weight="heavy",)
plt.text(point_x, point_y, "x = %0.2f\ny = %0.2f"%(point_x, point_y), rotation=50, color='red')

plt.show()
```
![post.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/36db2dca-f293-2a2c-c0be-c5a3ffb591f5.png)

Let's say that we need to emphasize the point of interest. We can do it by adding an arrow.

```matplot_lib.py
import numpy as np
x = np.linspace(-4, 4, 100)
y = x**2

point_x = 3
point_y = point_x**2

plt.plot(x, y,"b-")
plt.plot(point_x, point_y, "ro")
plt.annotate("Point of interest", xy=(point_x, point_y), xytext=(point_x-5.3,point_y+5.5),
             color="magenta", weight="light", fontsize=12,
             arrowprops={"facecolor": "brown"})

plt.show()
```

![poi.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/58a9200d-9153-ddb7-2f92-7373173a4551.png)

##### 6) Inserting Legend
When I was studying in university, I always forget to add legend to figures.  Legend is a crucial part when presenting the statistics. Let's see how we can insert a legend into a graph.


```matplot_lib.py
x = np.linspace(-2, 2, 50)
plt.plot(x, x, 'r--', label="Linear function")
plt.plot(x, x**2, 'g:',  label="Square function")
plt.legend(loc="best")
plt.show()
```

![legend.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/02707ba9-02fe-a45c-8b3c-f2086680e022.png)

This is the end of part 2.

*本記事は @qualitia_cdevの中の一人、@nuwanさんが書いてくれました。
*This article is written by @nuwan a member of @qualitia_cdev.