blob: 2dc34c74e243725b073d10275d4a7ecf67915c57 [file] [log] [blame]
Austin Schuh6c8ec4c2018-01-23 11:18:57 -08001#define _USE_MATH_DEFINES
2#include <iostream>
3#include <cmath>
4#include "../matplotlibcpp.h"
5
6namespace plt = matplotlibcpp;
7
8int main()
9{
Austin Schuhab802d52020-07-03 18:11:11 -070010 // Prepare data.
11 int n = 5000;
12 std::vector<double> x(n), y(n), z(n), w(n,2);
13 for(int i=0; i<n; ++i) {
14 x.at(i) = i*i;
15 y.at(i) = sin(2*M_PI*i/360.0);
16 z.at(i) = log(i);
17 }
18
19 // Set the size of output image = 1200x780 pixels
20 plt::figure_size(1200, 780);
Austin Schuh6c8ec4c2018-01-23 11:18:57 -080021
Austin Schuhab802d52020-07-03 18:11:11 -070022 // Plot line from given x and y data. Color is selected automatically.
23 plt::plot(x, y);
Austin Schuh6c8ec4c2018-01-23 11:18:57 -080024
Austin Schuhab802d52020-07-03 18:11:11 -070025 // Plot a red dashed line from given x and y data.
26 plt::plot(x, w,"r--");
Austin Schuh6c8ec4c2018-01-23 11:18:57 -080027
Austin Schuhab802d52020-07-03 18:11:11 -070028 // Plot a line whose name will show up as "log(x)" in the legend.
29 plt::named_plot("log(x)", x, z);
30
31 // Set x-axis to interval [0,1000000]
32 plt::xlim(0, 1000*1000);
33
34 // Add graph title
35 plt::title("Sample figure");
36
37 // Enable legend.
38 plt::legend();
39
40 // save figure
41 const char* filename = "./basic.png";
42 std::cout << "Saving result to " << filename << std::endl;;
43 plt::save(filename);
Austin Schuh6c8ec4c2018-01-23 11:18:57 -080044}