python - 为什么这个实时数据代码在 Google Colab 中不起作用?
问题描述
我对以下代码有疑问,该代码预计会在每次更新名为“example”的文本文件时显示一个 2D 图形。当您在 Google Colab 中运行它时,它在运行时没有任何错误显示。
您的帮助将不胜感激:)
[1] from google.colab import drive
drive.mount('/content/drive')
[2] import time
import matplotlib.pyplot as plt
[3] %matplotlib notebook
plt.rcParams['animation.html'] = 'jshtml'
[4] fig = plt.figure()
ax = fig.add_subplot(111)
fig.show()
[5] x, y = [], []
leng1 = 0
i = 0
while True:
pullData = open('/content/drive/My Drive/Colab Notebooks/example.txt', 'r').read()
leng2 = len(pullData)
pullData = pullData[leng1:leng2]
leng1 = leng2
dataArray = pullData.split('\n')
for eachLine in dataArray:
if len(eachLine)>1:
xar,yar = eachLine.split(',')
x.append(int(xar))
y.append(float(yar))
ax.plot(x, y, color='b')
fig.canvas.draw()
ax.set_xlim(left=max(0, i - 50), right=i + 50)
fig.show()
plt.pause(0.05)
i += 1
解决方案
推荐阅读
- angular - 带有 Angular 10 的 Roslib
- javascript - 单击链接而不下载时如何在新选项卡中显示pdf?
- amazon-web-services - 创建负载均衡器失败原因:必须指定至少两个不同可用区中的两个子网(服务:AmazonElasticLoadBalancing
- c# - 在 C#、.NET 项目中定位函数定义
- javascript - 在未处理的承诺拒绝中出现错误
- java - 尝试运行应用程序时出现 Micronaut NonUniqueBeanException
- android-studio - Flutter/Dart 查找未使用的文件或小部件
- python - Python:代码分析显示 OOP 中存在多个未定义错误
- c# - 为什么 [FromBody] 获取数据类型错误?
- excel - 基于多个条件计算唯一值 - BOOLEAN 不起作用