说明

默认情况下,在pycharm中用matplotlib绘制的图形在窗口内,是静态的:
在这里插入图片描述

弹出窗口设置

绘制动态图,需先进行相关设置:File–>Settings–>Tools–>Python Scientific–>Show plots in tool window(取消打勾):
在这里插入图片描述
在这里插入图片描述

代码

利用plt.ion() 开启一个画图的窗口,用于实时更新图表;利用plt.pause(0.5) 设置暂停时间,太快的话图表无法正常显示;利用plt.show()显示图片,防止闪退
示例如下:

# encoding: utf-8
# @author:Jackson
# @file:1.py
# @time:2021/2/24 22:21
import numpy as np
import matplotlib.pyplot as plt
from scipy.special import jn
from IPython.display import display, clear_output
import time

x = np.linspace(0,5)
f, ax = plt.subplots()
ax.set_title("Bessel functions")
plt.ion()   # 开启一个画图的窗口,用于实时更新图表
for n in range(1,10):
    time.sleep(1)
    ax.plot(x, jn(x,n))
    clear_output(wait=True)
    display(f)
    plt.pause(0.5)  # 设置暂停时间,太快的话图表无法正常显示
# plt.ioff()  # 关闭画图的窗口
plt.show()  # 显示图片,防止闪退

# close the figure at the end, so we don't get a duplicate
# of the last plot
plt.close()

在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐