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)

原文地址:https://www.cnblogs.com/komean/p/10457796.html