10000 Reduced example for dynamic plot class. · hypercode-go/matplotlib-cpp@c48ed30 · GitHub
[go: up one dir, main page]

Skip to content

Commit c48ed30

Browse files
author
Benno Evers
committed
Reduced example for dynamic plot class.
Removed timing and comparison code paths from the example for the dynamic plot class, to make it easier to spot the essential usage to the casual reader.
1 parent 07a73fa commit c48ed30

File tree

1 file changed

+18
-70
lines changed

1 file changed

+18
-70
lines changed

examples/update.cpp

Lines changed: 18 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -20,93 +20,41 @@ void update_window(const double x, const double y, const double t,
2020

2121
int main()
2222
{
23-
24-
bool use_dynamic_plot = false;
25-
bool timeit = true;
26-
2723
size_t n = 1000;
2824
std::vector<double> x, y;
2925

3026
const double w = 0.05;
3127
const double a = n/2;
3228

33-
for(size_t i=0; i<n; i++) {
29+
for (size_t i=0; i<n; i++) {
3430
x.push_back(i);
3531
y.push_back(a*sin(w*i));
3632
}
3733

3834
std::vector<double> xt(2), yt(2);
3935

40-
plt::title("Sample figure");
41-
42-
std::chrono::time_point<std::chrono::system_clock> start, end;
43-
start = std::chrono::system_clock::now();
44-
45-
if(use_dynamic_plot)
46-
{
47-
plt::xlim(x.front(), x.back());
48-
plt::ylim(-a,a);
49-
plt::axis("equal");
50-
51-
// plot sin once and for all
52-
plt::named_plot("sin", x, y);
53-
54-
// prepare plotting the tangent
55-
plt::Plot plot("tangent");
56-
57-
plt::legend();
58-
59-
for(size_t i=0; i<n; i++) {
36+
plt::title("Tangent of a since curve");
37+
plt::xlim(x.front(), x.back());
38+
plt::ylim(-a, a);
39+
plt::axis("equal");
6040

61-
if (i % 10 == 0) {
62-
{
63-
update_window(x[i], y[i], a*w*cos(w*x[i]), xt, yt);
64-
// just update data for this plot
65-
plot.update(xt, yt);
66-
}
41+
// Plot sin once and for all.
42+
plt::named_plot("sin", x, y);
6743

68-
// Display plot continuously
69-
if(!timeit)
70-
plt::pause(0.1);
71-
}
72-
}
73-
}
74-
75-
else
76-
{
77-
for(size_t i=0; i<n; i++) {
78-
79-
if (i % 10 == 0) {
80-
{
81-
plt::clf();
44+
// Prepare plotting the tangent.
45+
plt::Plot plot("tangent");
8246

83-
plt::named_plot("sin", x, y);
47+
plt::legend();
8448

85-
update_window(x[i], y[i], a*w*cos(w*i), xt, yt);
86-
plt::named_plot("tangent", xt, yt);
49+
for (size_t i=0; i<n; i++) {
50+
if (i % 10 == 0) {
51+
update_window(x[i], y[i], a*w*cos(w*x[i]), xt, yt);
8752

88-
// we have to control axis size
89-
plt::xlim(x.front(), x.back());
90-
plt::ylim(-a,a);
91-
plt::axis("equal");
92-
plt::legend();
53+
// Just update data for this plot.
54+
plot.update(xt, yt);
9355

94-
}
95-
96-
// Display plot continuously
97-
if(!timeit)
98-
plt::pause(0.1);
99-
}
56+
// Small pause so the viewer has a chance to enjoy the animation.
57+
plt::pause(0.1);
10058
}
101-
}
102-
103-
end = std::chrono::system_clock::now();
104-
double elapsed_seconds = std::chrono::duration_cast<std::chrono::microseconds>
105-
(end-start).count();
106-
if(use_dynamic_plot)
107-
std::cout << "dynamic";
108-
else
109-
std::cout << "static";
110-
111-
std::cout << " : " << elapsed_seconds/1000 << " ms\n";
59+
}
11260
}

0 commit comments

Comments
 (0)
0