【问题标题】:Python24: Maplotlib Animation connecting the first and the last pointPython 24:连接第一个点和最后一个点的 Matplotlib 动画
【发布时间】:2018-05-28 00:34:34
【问题描述】:

我是 matplotlib 的新手,我正在使用这个库来绘制 csv 文件中的数据。不使用动画功能,图表看起来是正确的,但是当我尝试使用动画时,图表连接了第一个点和最后一个点。我查了一些东西,但我不知道如何解决这个问题。有谁知道如何解决这个问题?下面是我的代码。提前致谢!

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import csv

x = []
y = []

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

def animate(i):

    with open("example.txt", "r") as csvfile:
        plots = csv.reader(csvfile, delimiter=',')

        for row in plots:
            x.append(int(row[0]))
            y.append(int(row[1]))

    ax1.clear()
    ax1.plot(x,y)

ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()

【问题讨论】:

    标签: python-2.7 animation matplotlib


    【解决方案1】:

    您一遍又一遍地将所有相同的点附加到要绘制的列表中。所以说 csv 文件包含数字 1,2,3 你正在做的是读取它们,将它们附加到列表中,绘制它们,然后再次读取它们并附加它们等等。

    所以 x 包含在 第一步:1,2,3 第二步:1,2,3,1,2,3 第三步:1,2,3,1,2,3,1,2,3

    因此,从第 2 步开始,第 3 步和第 1 步之间将建立联系。

    我不知道这个动画的目的是什么,因为动画所有相同的点是没有用的。因此,除了根本不制作动画外,没有直接的解决方案。

    【讨论】:

    • 我正在制作动画,因为我将更改我的 csv 文件中的值,并且我希望每当我更改 csv 文件中的值时,图表都会发生变化。
    • csv 文件中的值会在更新时被替换,还是会附加新值?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多