gnuplot-iostream
gnuplot-iostream copied to clipboard
Animating two functions
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
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.