Răsfoiți Sursa

Add animation example

Yuma.M 7 ani în urmă
părinte
comite
ba9343f483
3 a modificat fișierele cu 39 adăugiri și 1 ștergeri
  1. 3 1
      Makefile
  2. 36 0
      examples/animation.cpp
  3. BIN
      examples/animation.gif

+ 3 - 1
Makefile

@@ -1,4 +1,4 @@
-examples: minimal basic modern
+examples: minimal basic modern animation
 
 minimal: examples/minimal.cpp matplotlibcpp.h
 	cd examples && g++ -DWITHOUT_NUMPY minimal.cpp -I/usr/include/python2.7 -lpython2.7 -o minimal -std=c++11
@@ -9,3 +9,5 @@ basic: examples/basic.cpp matplotlibcpp.h
 modern: examples/modern.cpp matplotlibcpp.h
 	cd examples && g++ modern.cpp -I/usr/include/python2.7 -lpython2.7 -o modern -std=c++11
 
+animation: examples/animation.cpp matplotlibcpp.h
+	cd examples && g++ animation.cpp -I/usr/include/python2.7 -lpython2.7 -o animation -std=c++11

+ 36 - 0
examples/animation.cpp

@@ -0,0 +1,36 @@
+#define _USE_MATH_DEFINES
+#include <cmath>
+#include "../matplotlibcpp.h"
+
+namespace plt = matplotlibcpp;
+
+int main()
+{
+	int n = 1000;
+	std::vector<double> x, y, z;
+
+	for(int i=0; i<n; i++) {
+		x.push_back(i*i);
+		y.push_back(sin(2*M_PI*i/360.0));
+		z.push_back(log(i));
+
+		if (i % 10 == 0) {
+			// Clear previous plot
+			plt::clf();
+			// Plot line from given x and y data. Color is selected automatically.
+			plt::plot(x, y);
+			// Plot a line whose name will show up as "log(x)" in the legend.
+			plt::named_plot("log(x)", x, z);
+
+			// Set x-axis to interval [0,1000000]
+			plt::xlim(0, n*n);
+
+			// Add graph title
+			plt::title("Sample figure");
+			// Enable legend.
+			plt::legend();
+			// Display plot continuously
+			plt::pause(0.01);
+		}
+	}
+}

BIN
examples/animation.gif