1
0

modern.cpp 787 B

123456789101112131415161718192021222324252627282930
  1. #include "../matplotlibcpp.h"
  2. #include <cmath>
  3. using namespace std;
  4. namespace plt = matplotlibcpp;
  5. int main()
  6. {
  7. // plot(y) - the x-coordinates are implicitly set to [0,1,...,n)
  8. //plt::plot({1,2,3,4});
  9. // Prepare data for parametric plot.
  10. int n = 5000; // number of data points
  11. vector<double> x(n),y(n);
  12. for(int i=0; i<n; ++i) {
  13. double t = 2*M_PI*i/n;
  14. x.at(i) = 16*sin(t)*sin(t)*sin(t);
  15. y.at(i) = 13*cos(t) - 5*cos(2*t) - 2*cos(3*t) - cos(4*t);
  16. }
  17. // plot() takes an arbitrary number of (x,y,format)-triples.
  18. // x must be iterable (that is, anything providing begin(x) and end(x)),
  19. // y must either be callable (providing operator() const) or iterable.
  20. plt::plot(x, y, "r-", x, [](double d) { return 12.5+abs(sin(d)); }, "k-");
  21. // show plots
  22. plt::show();
  23. }