多圖合併顯示

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from matplotlib import animation
"""均勻圖中圖
plt.figure()

plt.subplot(2,2,1)
plt.plot([0,1],[0,1])

plt.subplot(2,2,2)
plt.plot([0,1],[0,2])

plt.subplot(223)
plt.plot([0,1],[0,3])

plt.subplot(224)
plt.plot([0,1],[0,4])
"""

"""不均勻圖中圖
plt.figure()

plt.subplot(2,1,1)
plt.plot([0,1],[0,1])

plt.subplot(2,3,4)
plt.plot([0,1],[0,2])

plt.subplot(235)
plt.plot([0,1],[0,3])

plt.subplot(236)
plt.plot([0,1],[0,4])
"""

"""分格顯示
"""
'''subplot2grid---------------------------------
plt.figure()
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
ax1.plot([1, 2], [1, 2])    # 畫小圖
ax1.set_title('ax1_title')  # 設置小圖的標題

ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
ax4 = plt.subplot2grid((3, 3), (2, 0))
ax5 = plt.subplot2grid((3, 3), (2, 1))

ax4.scatter([1, 2], [2, 2])
ax4.set_xlabel('ax4_x')
ax4.set_ylabel('ax4_y')
'''
'''GridSpec------------------------------------------
plt.figure()
gs = gridspec.GridSpec(3, 3)  # 將整個圖像窗口分成3行3列

ax6 = plt.subplot(gs[0, :])
ax7 = plt.subplot(gs[1, :2])
ax8 = plt.subplot(gs[1:, 2])
ax9 = plt.subplot(gs[-1, 0])
ax10 = plt.subplot(gs[-1, -2])
'''

'''subplots-------------------------------------------------------------------
# 建立一個2行2列的圖像窗口,sharex=True表示共享x軸座標, sharey=True表示共享y軸座標
f, ((ax11, ax12), (ax13, ax14)) = plt.subplots(2, 2, sharex=True, sharey=True)
ax11.scatter([1, 2], [1, 2])
plt.tight_layout()  # 緊湊顯示圖像
'''
'''添加圖中圖
fig = plt.figure()
# 創建數據
x = [1, 2, 3, 4, 5, 6, 7]
y = [1, 3, 4, 2, 5, 8, 6]
left, bottom, width, height = 0.1, 0.1, 0.8, 0.8

ax1 = fig.add_axes([left, bottom, width, height])
ax1.plot(x, y, 'r')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_title('title')

left, bottom, width, height = 0.2, 0.6, 0.25, 0.25
ax2 = fig.add_axes([left, bottom, width, height])  # 繪製左上角的小圖
ax2.plot(y, x, 'b')
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_title('title inside 1')


plt.axes([0.6, 0.2, 0.25, 0.25])
plt.plot(y[::-1], x, 'g')  # 注意對y進行了逆序處理
plt.xlabel('x')
plt.ylabel('y')
plt.title('title inside 2')
'''

'''次座標軸(同鏡面效果)
x = np.arange(0, 10, 0.1)
y1 = 0.05 * x**2
y2 = -1 * y1

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()  # 生成如同鏡面效果
ax1.plot(x, y1, 'g-')   # green, solid line
ax2.plot(x, y2, 'b-')  # blue

ax1.set_xlabel('X data')
ax1.set_ylabel('Y1 data', color='g')
ax2.set_ylabel('Y2 data', color='b')
'''

fig, ax = plt.subplots()
x = np.arange(0, 2 * np.pi, 0.01)
line, = ax.plot(x, np.sin(x))

# 構造自定義動畫函數


def animate(i):
    line.set_ydata(np.sin(x + i / 10.0))
    return line,

# 構造開始幀函數


def init():
    line.set_ydata(np.sin(x))
    return line,


ani = animation.FuncAnimation(
    fig=fig,  # 進行動畫繪製的figure
    func=animate,  # 自定義動畫函數
    frames=520,  # 動畫長度,一次循環包含的幀數
    init_func=init,  # 自定義開始幀
    interval=30,  # 更新頻率,以ms計
    blit=False)  # 選擇更新所有點,還是僅更新產生變化的點

plt.show()

發佈了47 篇原創文章 · 獲贊 3 · 訪問量 7834
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章