Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animating two functions #21

Open
tmiethlinger opened this issue Feb 21, 2022 · 1 comment
Open

Animating two functions #21

tmiethlinger opened this issue Feb 21, 2022 · 1 comment

Comments

@tmiethlinger
Copy link

Hello,

I wanted to ask if it's possible to animate two functions? I tried to expand the "demo_animation" in example-misc.cc, but that didn't work.

I tried the following code, among others:

#include
#include
#include
#include
#include
#include

#include "gnuplot-iostream.h"

#define M_PI 3.14159265358979323846

#include <unistd.h>
inline void mysleep(unsigned millis)
{
::usleep(millis * 1000);
}

void animation()
{
Gnuplot gp;

std::cout << "Press Ctrl-C to quit (closing gnuplot window doesn't quit)." << std::endl;

gp << "set yrange [-1:1]\n";

const int N = 1000;
std::vector<double> f1(N);
std::vector<double> f2(N);

double theta = 0;
while(true)
{
    for(int i=0; i<N; i++)
    {
        double alpha = (static_cast<double>(i)/N - 0.5) * 10;
        f1[i] = sin(alpha*8.0 + theta) * exp(-alpha*alpha/2.0);
        f2[i] = sin(alpha*8.0 + theta) * exp(-alpha*alpha/1.0);
    }

    // 1 function: works
    gp << "plot" << " '-' binary" << gp.binFmt1d(f1, "array") << "with lines title 'f1(x)'\n";
    gp.sendBinary1d(f1);
    gp.flush();

    // 2 functions: doesn't work
    //gp << "plot" << " '-' binary" << gp.binFmt1d(f1, "array") << "with lines notitle";
    //gp.sendBinary1d(f1);
    //gp << "plot" << " '-' binary" << gp.binFmt1d(f2, "array") << "with lines notitle\n";
    //gp.sendBinary1d(f2);
    //gp.flush();

    mysleep(10);
    theta += 0.05;
}

}

int main(int argc, char *argv[])
{
animation();
}

How can I include more than one function in the animation? And is it necessary to use binary here, or could I also use other functions like add_plot1d?

Thank you for your help,
Thomas

@dstahlke
Copy link
Owner

Try it like this:

gp << "plot" << " '-' binary" << gp.binFmt1d(f1, "array") << "with lines notitle";
gp << ", '-' binary" << gp.binFmt1d(f2, "array") << "with lines notitle\n";
gp.sendBinary1d(f1);
gp.sendBinary1d(f2);
gp.flush();

And yes, you can use add_plot1d. Though it's not an animation, example-data-1d.cc gives an example of plotting many functions at once. To animate, you'd just have to call it in a loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants