1
0

basic.cpp 764 B

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