創建後如何動態共享 Matplotlib 中子圖的 x 軸?

DDD
發布: 2024-10-25 05:58:02
原創
578 人瀏覽過

How do I dynamically share the x-axis of subplots in Matplotlib after creation?

動態共享子圖的 X 軸

問題:建立兩個子圖後共用它們的 x 軸。

解決方案:

共享軸通常是在建立軸時使用 sharex 參數完成的。但是,在已經建立子圖的情況下,可以使用 ax2.sharex(ax1) 共用它們的 x 軸。

這裡有一個Python 程式碼範例來說明這個方法:

<code class="python">import matplotlib.pyplot as plt

t = np.arange(1000) / 100
x = np.sin(2 * np.pi * 10 * t)
y = np.cos(2 * np.pi * 10 * t)

fig = plt.figure()
ax1 = plt.subplot(211)  # Create subplot 1
ax2 = plt.subplot(212)  # Create subplot 2

# Plot data in the subplots
ax1.plot(t, x)
ax2.plot(t, y)

# Share the x-axes between the subplots
ax2.sharex(ax1)

# Disable tick labels for one of the subplots to avoid duplication
ax1.set_xticklabels([])

plt.show()</code>
登入後複製

在這個程式碼中,建立子圖後,我們使用ax2.sharex(ax1) 連結兩個子圖的x 軸。為了防止重複的刻度標籤,我們手動停用 ax1。

或者,您可以使用循環來共享子圖列表的 x 軸,例如:

<code class="python">axes = [ax1, ax2, ax3]

for ax in axes[1:]:
    ax.sharex(axes[0])</code>
登入後複製

以上是創建後如何動態共享 Matplotlib 中子圖的 x 軸?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!