matplotlib-cpp icon indicating copy to clipboard operation
matplotlib-cpp copied to clipboard

3D Scatter Plot animation

Open Sean340927 opened this issue 3 years ago • 2 comments

Hi Is there a way to update the data in a 3D scatter plot in order to produce an animation? (My intention is to plot 3D data in real time). I have checked both the animation.cpp and update.cpp examples, but had no luck. Any help would be appreciated. Many thanks.

Sean340927 avatar May 11 '22 16:05 Sean340927

Hi
Have you found a solution? if have, Can you share with me...

Hummel-Wang avatar Aug 17 '22 03:08 Hummel-Wang

Hi, I figured out a way to solve this problem. To make an animation of the 3d plot, you have to specify the figure handle. The header get it done by default for the 2d plot, but not for 3d. So here is a possible way for doing this:

#include "matplotlibcpp.h"
#include <vector>
#include <map>
#include <cmath>

namespace plt = matplotlibcpp;
using namespace std;
const long fg = plt::figure(); // Define the figure handle number here

int main()
{
    std::map<std::string, std::string> kwargs;
    kwargs["marker"] = "o";
    kwargs["linestyle"] = "-";
    kwargs["linewidth"] = "1";
    kwargs["markersize"] = "12";
    vector<double> x, y, z;
    x = {1, 2, 3, 4};
    y = {1, 2, 2, 1};
    z = {1, 2, 3, 4};

    int loop_num = 0;
    while (loop_num <= 50)
    {
        for (int j = 0; j < x.size(); j++)
        {
            x[j] = x[j] + cos(2 * M_PI / (loop_num + 1));
        }
        plt::plot3(x, y, z, kwargs, fg); // Specify the number of the figure
        plt::xlim(0.0, 10.0);
        plt::ylim(0.0, 10.0);
        plt::pause(0.05);
        plt::clf();
        // plt::close();
        loop_num++;
    }

    return 0;
}
    

jmz3 avatar Mar 05 '23 19:03 jmz3