python繪制動態圖


1、需要注意的問題

  • 解決 MatplotlibDeprecationWarning: Using default event loop until function specific to this GUI is implemented warnings.warn(str, mplDeprecation 問題
import warnings
warnings.filterwarnings("ignore",".*GUI is implemented.*")
  • 運行結束時,閃退,使用
plt.pause(0)

2、具體代碼test.py:

import matplotlib.pyplot as plt
from matplotlib.patches import Circle
import numpy as np
import math

import warnings
# 解決(MatplotlibDeprecationWarning: Using default event loop until function specific to this GUI is implemented warnings.warn(str, mplDeprecation)
warnings.filterwarnings("ignore",".*GUI is implemented.*")


fig = plt.figure()
# 連續性的畫圖
ax = fig.add_subplot(1, 1, 1)
# 設置圖像顯示的時候XY軸比例
ax.axis("equal")
# 開啟一個畫圖的窗口
plt.ion()
print('開始仿真')
try:
    for i in range(20):
        x = np.random.randn()
        y = np.random.randn()
        if x < 0.5:
            ax.scatter(x, y, c='r', marker='v')  # 散點圖
        else:
            ax.scatter(x, y, c='b', marker='.')  # 散點圖
        plt.axvline(0.5)  # 畫出豎線 (y=0.5)
        plt.axvline(-0.5)  # 畫出豎線 (y=-.5)
        # 停頓時間
        plt.pause(0.1)
except Exception as err:
    print(err)
# 防止運行結束時閃退
plt.pause(0)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM