> For the complete documentation index, see [llms.txt](https://jen-hsuan-hsieh.gitbook.io/python/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jen-hsuan-hsieh.gitbook.io/python/chapter-2courses/21python-for-data-science-and-machine-learning-bootcamp/test/2162matplotlib.md).

# 2.1.6.2.Matplotlib

## 1. 使用library

```
import matplotlib.pyplot as plt
```

* 將matplotlib的圖表直接嵌入到Notebook之中

```
%matplotlib inline
```

## 2. 畫圖的基本操作

* 使用plt.plot及numpy array

```
import numpy as np
x = np.linspace(0, 5, 11)
y = x ** 2
plt.plot(x, y, 'r-')
plt.xlabel('X label')
plt.ylabel('Y label')
plt.title('Title')
#print
plt.show()
```

![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-13%20下午8.13.43.png)

## 3. 繪製多張圖

* 使用plt.subplot

```
plt.subplot(1, 2, 1)
plt.plot(x, y, 'r')

plt.subplot(1, 2, 2)
plt.plot(x, y, 'b')
```

![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-13%20下午8.14.50.png)

## 4.以Object Orient的概念進行繪圖操作

* 1.使用plt.figure及add\_axes

```
 #OO
fig = plt.figure()

axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])

axes.plot(x, y)
axes.set_xlabel('X label')
axes.set_ylabel('Y label')
axes.set_title('Title')
```

![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-13%20下午8.17.14.png)

* 2.figsize與dpi參數的使用

```
fig = plt.figure(figsize = (8, 2), dpi = 500)

ax = fig.add_axes([0, 0, 1, 1])
ax.plot(x, y)
```

![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-13%20下午8.59.23.png)

* 3.legend與位置 (location code, loc)
  * loc可以設定代碼或是給tuple

    ```
    fig = plt.figure()

    axes1 = fig.add_axes([0.1, 0.1, 0.8, 0.8])
    axes1.plot(x, x**2, label = 'X squared')

    axes1.plot(x, x**3, label = 'X Cubed')
    axes1.legend(loc = 10)
    ```

    ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-13%20下午9.12.13.png)
* 4.line與marker
  * color, width, style

    ```
    fig = plt.figure()
    ax = fig.add_axes([0, 0, 1, 1])
    ax.plot(x, y, color = 'orange')
    ax.plot(x, y * 2, color = '#000000')
    ax.plot(x, y * 3, color = 'purple', lw = 6)
    ax.plot(x, y * 4, color = 'purple', lw = 6, alpha = 0.5)
    ax.plot(x, y * 5, color = 'red', lw = 6, ls = '-.')
    ax.plot(x, y * 6, color = 'red', lw = 1, ls = '-', marker = '*', markersize = 20, 
      markerfacecolor = 'yellow', markeredgewidth = 3, markeredgecolor = 'green')
    ```

    ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-13%20下午9.44.15.png)
* 5.設定lower bound及upper bound

```
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
ax.plot(x, y * 3, color = 'purple', lw = 1)
ax.set_xlim([0, 1])
ax.set_ylim([0, 2])
```

![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-13%20下午9.45.50.png)

## 5.以Object Orient的概念進行多張圖的繪圖操作

* 1.使用plt.figure及add\_axes

```
    fig = plt.figure()

    axes1 = fig.add_axes([0.1, 0.1, 0.8, 0.8])
    axes2 = fig.add_axes([0.5, 0.15, 0.5, 0.5])

    axes1.plot(x, y)
    axes.set_title('LARGE PLOT')
    axes2.plot(y, x)
    axes.set_title('SMALLER PLOT')
```

![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-13%20下午8.32.10.png)

* 2.使用plt.subplots

```
    fig, axes = plt.subplots(nrows = 1, ncols = 2)

    for current_ax in axes:
        current_ax.plot(x, y)

    axes[0].plot(x, y)
    axes[0].set_title('FIRST PLOT')
    axes[1].plot(y, x)
    axes[1].set_title('SECOND PLOT')
```

![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-13%20下午8.48.50.png)

* 3.使用plt.subplots如果太擠可以用plt.tight\_layout()

```
    fig, axes = plt.subplots(nrows = 3, ncols = 3)
    plt.tight_layout()
```

![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-13%20下午8.50.46.png)

* 4.figsize與dpi參數的使用

```
fig, axes = plt.subplots(nrows = 2, ncols = 1, figsize = (8, 2))

axes[0].plot(x, y)
axes[1].plot(x, y)
plt.tight_layout()
```

![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-13%20下午9.01.15.png)

## 儲存圖檔

```
fig.savefig('my_picture.png', dpi = 100)
```
