Matplotlib 3D Scatter Animate顺序数据

时间:2017-04-12 18:34:13

标签: python animation matplotlib plot scatter3d

我已经制作了一些加速度计数据的3D散点图:

3D Scatter plot

这是非常基本的,但我对它的看法很满意,因为这是我第一次尝试使用Python。这是我为编写此可视化而编写的代码:

// The list of words to delete ("stop words")
var stopWords = new List<string> { "remove", "these", "words" };

// The list of files to check - you can get this list in other ways
var filesToCheck = new List<string>
{
    @"f:\public\temp\temp1.txt",
    @"f:\public\temp\temp2.txt",
    @"f:\public\temp\temp3.txt"
};

// This list will contain all the unique words from all
// the files, except the ones in the "stopWords" list
var uniqueFilteredWords = new List<string>();

// Loop through all our files
foreach (var fileToCheck in filesToCheck)
{
    // Read all the file text into a varaible
    var fileText = File.ReadAllText(fileToCheck);

    // Split the text into distinct words (splitting on null 
    // splits on all whitespace) and ignore empty lines
    var fileWords = fileText.Split(null)
        .Where(line => !string.IsNullOrWhiteSpace(line))
        .Distinct();

    // Add all the words from the file, except the ones in 
    // your "stop list" and those that are already in the list
    uniqueFilteredWords.AddRange(fileWords.Except(stopWords)
        .Where(word => !uniqueFilteredWords.Contains(word)));
}

数据按时间戳编制索引,如下所示:

enter image description here

我接下来要做的是采用上面的情节,让每个点一次进入一个。有一个简单的方法吗?查看matplotlib中的示例,看起来他们正在使用随机生成的数据,然后为这些行设置动画。我不确定如何编写更新数据每行数字的函数。

如果有人能指导我做一些类似的事情的例子,我真的很感激。到目前为止,我的搜索只给出了一些示例,其中数据是由函数生成的,或者是随机生成的。

1 个答案:

答案 0 :(得分:3)

此问题中有一个3D散点图示例:Matplotlib 3D scatter animations

为了让点逐一出现,您可以绘制从索引0开始直到当前动画索引i的数据帧中的数据。

(df.x.values[:i], df.y.values[:i], df.z.values[:i])

一个完整的例子:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation

x = np.random.normal(size=(80,3))
df = pd.DataFrame(x, columns=["x","y","z"])


fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
sc = ax.scatter([],[],[], c='darkblue', alpha=0.5)

def update(i):
    sc._offsets3d = (df.x.values[:i], df.y.values[:i], df.z.values[:i])

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_xlim(-3,3)
ax.set_ylim(-3,3)
ax.set_zlim(-3,3)

ani = matplotlib.animation.FuncAnimation(fig, update, frames=len(df), interval=70)

plt.tight_layout()
plt.show()

enter image description here