animation.cpp 752 B

123456789101112131415161718192021222324252627282930313233343536
  1. #define _USE_MATH_DEFINES
  2. #include <cmath>
  3. #include "../matplotlibcpp.h"
  4. namespace plt = matplotlibcpp;
  5. int main()
  6. {
  7. int n = 1000;
  8. std::vector<double> x, y, z;
  9. for(int i=0; i<n; i++) {
  10. x.push_back(i*i);
  11. y.push_back(sin(2*M_PI*i/360.0));
  12. z.push_back(log(i));
  13. if (i % 10 == 0) {
  14. // Clear previous plot
  15. plt::clf();
  16. // Plot line from given x and y data. Color is selected automatically.
  17. plt::plot(x, y);
  18. // Plot a line whose name will show up as "log(x)" in the legend.
  19. plt::named_plot("log(x)", x, z);
  20. // Set x-axis to interval [0,1000000]
  21. plt::xlim(0, n*n);
  22. // Add graph title
  23. plt::title("Sample figure");
  24. // Enable legend.
  25. plt::legend();
  26. // Display plot continuously
  27. plt::pause(0.01);
  28. }
  29. }
  30. }