Skip to content

Commit 7db9d2f

Browse files
committed
exemple folder reverted
1 parent b09ac02 commit 7db9d2f

31 files changed

+608
-0
lines changed

examples/.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
animation
2+
bar
3+
basic
4+
fill
5+
fill_inbetween
6+
imshow
7+
minimal
8+
modern
9+
nonblock
10+
quiver
11+
subplot
12+
surface
13+
update
14+
xkcd

examples/animation.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#define _USE_MATH_DEFINES
2+
#include <cmath>
3+
#include "../matplotlibcpp.h"
4+
5+
namespace plt = matplotlibcpp;
6+
7+
int main()
8+
{
9+
int n = 1000;
10+
std::vector<double> x, y, z;
11+
12+
for(int i=0; i<n; i++) {
13+
x.push_back(i*i);
14+
y.push_back(sin(2*M_PI*i/360.0));
15+
z.push_back(log(i));
16+
17+
if (i % 10 == 0) {
18+
// Clear previous plot
19+
plt::clf();
20+
// Plot line from given x and y data. Color is selected automatically.
21+
plt::plot(x, y);
22+
// Plot a line whose name will show up as "log(x)" in the legend.
23+
plt::named_plot("log(x)", x, z);
24+
25+
// Set x-axis to interval [0,1000000]
26+
plt::xlim(0, n*n);
27+
28+
// Add graph title
29+
plt::title("Sample figure");
30+
// Enable legend.
31+
plt::legend();
32+
// Display plot continuously
33+
plt::pause(0.01);
34+
}
35+
}
36+
}

examples/animation.gif

32.5 KB
Loading

examples/bar.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#define _USE_MATH_DEFINES
2+
3+
#include <iostream>
4+
#include <string>
5+
#include "../matplotlibcpp.h"
6+
namespace plt = matplotlibcpp;
7+
8+
int main(int argc, char **argv) {
9+
std::vector<int> test_data;
10+
for (int i = 0; i < 20; i++) {
11+
test_data.push_back(i);
12+
}
13+
14+
plt::bar(test_data);
15+
plt::show();
16+
17+
return (0);
18+
}

examples/bar.png

11.7 KB
Loading

examples/basic.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#define _USE_MATH_DEFINES
2+
#include <iostream>
3+
#include <cmath>
4+
#include "../matplotlibcpp.h"
5+
6+
namespace plt = matplotlibcpp;
7+
8+
int main()
9+
{
10+
// 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);
21+
22+
// Plot line from given x and y data. Color is selected automatically.
23+
plt::plot(x, y);
24+
25+
// Plot a red dashed line from given x and y data.
26+
plt::plot(x, w,"r--");
27+
28+
// 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);
44+
}

examples/basic.png

36.5 KB
Loading

examples/colorbar.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#define _USE_MATH_DEFINES
2+
#include <cmath>
3+
#include <iostream>
4+
#include "../matplotlibcpp.h"
5+
6+
using namespace std;
7+
namespace plt = matplotlibcpp;
8+
9+
int main()
10+
{
11+
// Prepare data
12+
int ncols = 500, nrows = 300;
13+
std::vector<float> z(ncols * nrows);
14+
for (int j=0; j<nrows; ++j) {
15+
for (int i=0; i<ncols; ++i) {
16+
z.at(ncols * j + i) = std::sin(std::hypot(i - ncols/2, j - nrows/2));
17+
}
18+
}
19+
20+
const float* zptr = &(z[0]);
21+
const int colors = 1;
22+
23+
plt::title("My matrix");
24+
PyObject* mat;
25+
plt::imshow(zptr, nrows, ncols, colors, {}, &mat);
26+
plt::colorbar(mat);
27+
28+
// Show plots
29+
plt::show();
30+
plt::close();
31+
Py_DECREF(mat);
32+
}

examples/contour.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "../matplotlibcpp.h"
2+
3+
#include <cmath>
4+
5+
namespace plt = matplotlibcpp;
6+
7+
int main()
8+
{
9+
std::vector<std::vector<double>> x, y, z;
10+
for (double i = -5; i <= 5; i += 0.25) {
11+
std::vector<double> x_row, y_row, z_row;
12+
for (double j = -5; j <= 5; j += 0.25) {
13+
x_row.push_back(i);
14+
y_row.push_back(j);
15+
z_row.push_back(::std::sin(::std::hypot(i, j)));
16+
}
17+
x.push_back(x_row);
18+
y.push_back(y_row);
19+
z.push_back(z_row);
20+
}
21+
22+
plt::contour(x, y, z);
23+
plt::show();
24+
}

examples/fill.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#define _USE_MATH_DEFINES
2+
#include "../matplotlibcpp.h"
3+
#include <cmath>
4+
5+
using namespace std;
6+
namespace plt = matplotlibcpp;
7+
8+
// Example fill plot taken from:
9+
// https://matplotlib.org/gallery/misc/fill_spiral.html
10+
int main() {
11+
// Prepare data.
12+
vector<double> theta;
13+
for (double d = 0; d < 8 * M_PI; d += 0.1)
14+
theta.push_back(d);
15+
16+
const int a = 1;
17+
const double b = 0.2;
18+
19+
for (double dt = 0; dt < 2 * M_PI; dt += M_PI/2.0) {
20+
vector<double> x1, y1, x2, y2;
21+
for (double th : theta) {
22+
x1.push_back( a*cos(th + dt) * exp(b*th) );
23+
y1.push_back( a*sin(th + dt) * exp(b*th) );
24+
25+
x2.push_back( a*cos(th + dt + M_PI/4.0) * exp(b*th) );
26+
y2.push_back( a*sin(th + dt + M_PI/4.0) * exp(b*th) );
27+
}
28+
29+
x1.insert(x1.end(), x2.rbegin(), x2.rend());
30+
y1.insert(y1.end(), y2.rbegin(), y2.rend());
31+
32+
plt::fill(x1, y1, {});
33+
}
34+
plt::show();
35+
}

0 commit comments

Comments
 (0)