1
0

basic.cpp 739 B

12345678910111213141516171819202122232425262728293031323334
  1. #include "../matplotlibcpp.h"
  2. #include <cmath>
  3. namespace plt = matplotlibcpp;
  4. int main()
  5. {
  6. // Prepare data.
  7. int n = 5000;
  8. std::vector<double> x(n), y(n), z(n), w(n,2);
  9. for(int i=0; i<n; ++i) {
  10. x.at(i) = i*i;
  11. y.at(i) = sin(2*M_PI*i/360.0);
  12. z.at(i) = log(i);
  13. }
  14. // Plot line from given x and y data. Color is selected automatically.
  15. plt::plot(x, y);
  16. // Plot a red dashed line from given x and y data.
  17. plt::plot(x, w,"r--");
  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, 1000*1000);
  22. // Add graph title
  23. plt::title("Sample figure");
  24. // Enable legend.
  25. plt::legend();
  26. // save figure
  27. plt::save("./basic.png");
  28. }