【发布时间】:2017-02-04 00:21:52
【问题描述】:
如何从 csv 文件的第 4 列和第 5 列中提取数据?另外我如何只显示最新的数据点?旧点可能会消失。
以下是我目前所拥有的,以下是 csv 文件的示例。
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
def animate(i):
pullData = open("sampleText.csv","r").read()
dataArray = pullData.split('\n')
xar = []
yar = []
for eachLine in dataArray:
if len(eachLine)>1:
x,y = eachLine.split(',')
xar.append(int(x))
yar.append(int(y))
ax1.scatter(xar,yar)
ax1.set_xlim([-100, 100])
ax1.set_xlabel('X')
ax1.set_ylim([-100, 100])
ax1.set_ylabel('Y')
ani = animation.FuncAnimation(fig, animate, interval=100)
plt.show()
示例 CSV,文件不包含标题。第 4 列是“x”,第 5 列是“y”。
1,2,54,-34,54,23,65,
2,3,54,-34,-98,12,43,
3,6,54,34,56,76,89,
87,90,54,34,23,43,98,
44,98,54,-34,65,34,23,
23,79,54,34,-98,23,12,
9,3,54,34,65,34,32,
【问题讨论】:
-
你想画什么? csv 中的 x 和 y 是什么?
-
示例 csv 中的第 4 列和第 5 列。 4 是 x,5 是 y。
标签: python csv matplotlib scatter