basic.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #define _USE_MATH_DEFINES
  2. #include <iostream>
  3. #include <cmath>
  4. #include "../matplotlibcpp.h"
  5. namespace plt = matplotlibcpp;
  6. int main()
  7. {
  8. // Prepare data.
  9. int n = 5000;
  10. std::vector<double> x(n), y(n), z(n), w(n,2);
  11. for(int i=0; i<n; ++i) {
  12. x.at(i) = i*i;
  13. y.at(i) = sin(2*M_PI*i/360.0);
  14. z.at(i) = log(i);
  15. }
  16. // Set the size of output image = 1200x780 pixels
  17. plt::figure_size(1200, 780);
  18. // Plot line from given x and y data. Color is selected automatically.
  19. plt::plot(x, y);
  20. // Plot a red dashed line from given x and y data.
  21. plt::plot(x, w,"r--");
  22. // Plot a line whose name will show up as "log(x)" in the legend.
  23. plt::named_plot("log(x)", x, z);
  24. // Set x-axis to interval [0,1000000]
  25. plt::xlim(0, 1000*1000);
  26. // Add graph title
  27. plt::title("Sample figure");
  28. // Enable legend.
  29. plt::legend();
  30. // save figure
  31. const char* filename = "./basic.png";
  32. std::cout << "Saving result to " << filename << std::endl;;
  33. plt::save(filename);
  34. }