Pycharm中使用matplotlib绘制动态图形

Pycharm中使用matplotlib绘制动态图形

最近用pycharm学习D2L时发现官方在jupyter notebook交互式环境中能动态绘制图形,但是在pycharm脚本环境中只会在最终 plt.show() 后输出一张静态图像。于是有了下面这段自己折腾了一下午的代码,用来在pycharm中模仿jupyter交互式环境的动态绘制图像:

python 复制代码
import matplotlib
import matplotlib.pyplot as plt
from IPython.display import display, clear_output
import time

matplotlib.use('Qt5Agg')
plt.ion()
fig, ax = plt.subplots()
ax.set_title("ECG Signal")
plt.xlabel("time: s")
plt.ylabel("voltage")
#############################
# 1:直接绘制完整波形:
# ax.plot(signal)
# ax.legend(["MLII", "V5"])
# plt.show()
#############################
# 2:随时间动态输出波形:
y1 = []
y2 = []
t = []
sr = 360  # 采样率
plt.xticks(range(0, len(signal) // sr + 1))
for n in range(len(signal)):
    y1.append(signal[n, 0])
    y2.append(signal[n, 1])
    t.append(n / sr)
    if (n + 1) % 10 == 0 or (n + 1) == len(signal):
        time.sleep(0.01)
        ax.plot(t, y1, "-", color='deepskyblue', linewidth=1)      
        clear_output(wait=True)
        display(fig)
        plt.pause(0.01)
ax.legend(["MLII", "V5"])
plt.show()

最终效果

相关推荐
埃博拉酱6 分钟前
Pip/Conda 混用导致的 CRT 版本冲突问题:[WinError 1114] 动态链接库(DLL)初始化例程失败
windows·python
刘小八7 分钟前
LangGraph 人机交互实战:Interrupt、人工审批与工作流恢复
人工智能·python·人机交互
YMWM_15 分钟前
python-venv虚拟环境
linux·开发语言·python
Tbisnic32 分钟前
23.大模型开发:深度学习----CNN 卷积神经网络 与 RNN 循环神经网络
人工智能·python·rnn·深度学习·cnn·ai大模型
CoderYanger1 小时前
视频切割脚本(Python版)
linux·开发语言·windows·后端·python·程序人生·职场和发展
迷途呀1 小时前
新闻头条后端:新闻缓存模块
前端·redis·python·缓存·fastapi
想会飞的蒲公英1 小时前
逻辑回归为什么叫回归,却用来做分类
人工智能·python·分类·回归·逻辑回归
CoderYanger1 小时前
视频裁剪+缩放+自动添加水印脚本(Python版)
开发语言·后端·python·程序人生·职场和发展·音视频·学习方法
霍格沃兹测试开发学社测试人社区2 小时前
Node.js 浏览器引擎 + Python 大脑:Playwright 混合架构爬虫系统深度解析
python·架构·node.js
绘梨衣5472 小时前
求助帖:pdf复杂表格解析
爬虫·python·pdf