Matplotlib Tutorial Part - 01

Matplotlib is one of the most well-known plotting libraries in python for generating graphs. Here we will go through the basics using some examples.

If you use a terminal or cmd, graphs are generally shown in a separate window. But if you need to use it for the Jupyter notebook, it's possible to display graphs in the notebook by running the %matplotlib inline magic command.

##### 1) Getting Started.
```matplot_lib.py
import matplotlib
%matplotlib inline
import matplotlib.pyplot as plt

plt.plot([1, 3, 4, 7, 5, 2])
plt.show()
```

![first graph.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/b3f1c9f7-3318-be67-5581-a970ca5e3370.png)


We can see that graph is generated by calling the plot function and show function. 
We used a single data array, so it is like passing the data to the Y-axis. And it seems like the X-axis data are automatically generated.

Now let's try passing two arrays with the same length.

```matplot_lib.py
plt.plot([-1, 0, 9, 5], [4, 2, 9, 1])
plt.show()
```

![2.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/5027fd5a-5156-46de-877e-aa96e1199876.png)

If we used two data arrays, the first is pass to the X-axis, and the second represents the Y-axis.


Let's assume that we need to change the minimum and maximum values of the x and y-axis. Then we can use the following command.

```matplot_lib.py
plt.plot([-1, 0, 9, 5], [4, 2, 9, 1])
plt.axis([-2, 10, -2, 10])
plt.show()
```

![3.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/1840ee79-40c7-f9bd-aad2-9354131e41d6.png)

Now we have some basics knowledge of how the matplotlib works. Let's try to use a mathematical function to generate an array of data. Then plot it by using matplotlib.And also, let's try to find out how to create a title, axis labels, and grid.


```matplot_lib.py
import numpy as np
x = np.linspace(-4, 4, 100)
y = x**2
plt.plot(x, y)
plt.title("Square function")
plt.xlabel("x")
plt.ylabel("y = x**2")
plt.grid(True)
plt.show()
```

![44.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/90ed3454-e16e-98e8-7cb6-f5ed38a98f84.png)

##### 2) 3rd argument of the plot function.

```matplot_lib.py
plt.plot([0, 100, 100, 0, 0, 100, 0, 100], [0, 0, 100, 100, 0, 100, 100, 0])
plt.axis([-10, 110, -10, 110])
plt.show()
```
![5.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/cefb94b1-f06d-2757-bd9f-69e0e5a53c05.png)

We can give the 3rd argument to the plot function to change the line's color and style.
For instance, "r:" intends a red dotted line.

```matplot_lib.py
plt.plot([0, 100, 100, 0, 0, 100, 0, 100], [0, 0, 100, 100, 0, 100, 100, 0], "r:")
plt.axis([-10, 110, -10, 110])
plt.show()
```
![7.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/4c27f701-c073-29b2-9207-fd5bd7f54602.png)

And also, we can use style and colors in one graph. To do so, we can use  x1, y1, [style1], x2, y2, [style2],... pattern or simply call plot multiple times before calling show

```matplot_lib.py
plt.plot([0, 100, 100, 0, 0], [0, 0, 100, 100, 0], "r:", [0, 100, 0, 100], [ 0, 100, 100, 0], "b--")
plt.axis([-10, 110, -10, 110])
plt.show()
```
![8.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/7ebd2a02-9f74-256d-ccc7-d1ce3a65f3e1.png)

There are several patterns and colors; please refer to matplotlib documentation for more details.

##### 3) Lines attributes.

The plot function returns the list of line2D objects. We can easily set attributes using these objects. There are several attributes of lines; please refer to matplotlib documentation for more details.

```matplot_lib.py
x = np.linspace(-2, 2, 50)
line_1, line_2, line_3 = plt.plot(x, x, 'r--', x, x**2, 'g:', x, x**3, 'b^')
line_1.set_linewidth(5.0)
line_2.set_dash_capstyle("round")
line_3.set_alpha(0.1)
plt.show()
```

![9.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/e2115a72-21ba-558a-f553-6aea8f02decd.png)

Thats all for part 1.

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