# Matplotlib sharex 引數

Salman Mehmood 2023年1月30日

## Matplotlib 中的 `subplot()` 函式

`subplot()` 函式表示單個圖中的多個圖，我們在同一圖中繪製圖。這就是為什麼我們可以在一個圖中有多個圖。

``````subplot(r, c, i)
``````

`r` 表示行。`c` 參數列示一列，`i` 引數是繪圖的索引。使用這些引數將幫助你指定繪圖單元，我們可以按降序調整繪圖位置。

## 使用 `sharex` 引數從多個子圖中共享軸

``````import matplotlib.pyplot as plt
import numpy as np
``````

`x``y` 值建立資料，然後我們為 `"311"` 繪製子圖，其中 `"3"` 是一行，`"1"` 是一列。 `"1"` 是一個索引。

`tick_params()` 方法將刻度設定到給定的軸。在我們的例子中，我們只是為 x 軸設定它。

``````axis1 = plt.subplot(311)
plt.plot(range_samples, sine)
plt.tick_params("x", labelsize=6)
``````

``````axis2 = plt.subplot(312, sharex=axis1)
``````

``````axis3 = plt.subplot(313, sharex=axis1, sharex=axis1)
plt.plot(range_samples, sine2)
plt.xlim(0.01, 5.0)
``````

``````import matplotlib.pyplot as plt
import numpy as np

range_samples = np.arange(0.01, 5.0, 0.01)
sine = np.sin(2 * np.pi * range_samples)
inverse = np.exp(-range_samples)
sine2 = np.sin(4 * np.pi * range_samples)

axis1 = plt.subplot(311)
plt.plot(range_samples, sine)
plt.tick_params("x", labelsize=6)

# This line only share x
axis2 = plt.subplot(312, sharex=axis1)
plt.plot(range_samples, inverse)
# make these tick labels invisible
plt.tick_params("x", labelbottom=False)

# This lin share x and y axes
axis3 = plt.subplot(313, sharex=axis1, sharey=axis1)
plt.plot(range_samples, sine2)
plt.xlim(0.01, 5.0)
plt.show()
``````

Hello! I am Salman Bin Mehmood(Baum), a software developer and I help organizations, address complex problems. My expertise lies within back-end, data science and machine learning. I am a lifelong learner, currently working on metaverse, and enrolled in a course building an AI application with python. I love solving problems and developing bug-free software for people. I write content related to python and hot Technologies.