1
1
#include " ../../include/cppyplot.hpp"
2
2
#include < algorithm>
3
+ #include < random>
3
4
4
5
int main ()
5
6
{
6
7
Cppyplot::cppyplot pyp;
7
8
8
9
std::vector<float > vec (500 , 0 .0F );
9
- std::vector<float > sine;
10
+ std::vector<float > sine, cosine ;
10
11
sine.reserve (500 );
12
+ cosine.reserve (500 );
11
13
std::iota (vec.begin (), vec.end (), 0 .0F );
14
+
15
+ std::random_device seed;
16
+ std::mt19937 gen (seed ());
17
+ std::uniform_real_distribution<float > uniform (0 .0F , 1 .0F );
18
+
12
19
std::transform (vec.begin (), vec.end (), std::back_inserter (sine),
13
- [](auto & elem){return std::sinf (elem * 3 .14159265F /180 .0F );});
20
+ [&](auto & elem){return uniform (gen)*std::sinf (elem * 3 .14159265F /180 .0F );});
21
+
22
+ std::transform (vec.begin (), vec.end (), std::back_inserter (cosine),
23
+ [&](auto & elem){return uniform (gen)*std::cosf (elem * 3 .14159265F /180 .0F );});
14
24
15
25
pyp.raw (R"pyp(
16
26
17
27
fig = plt.figure(figsize=(6,5))
18
28
ax = plt.axes(xlim=(0, 500), ylim=(-1.5,1.5))
19
- line, = ax.plot([], [], lw=1.5)
29
+ line1, = ax.plot([], [], 'r-o', markersize=1, linewidth=0.8)
30
+ line2, = ax.plot([], [], 'k-o', markersize=1, linewidth=0.8)
20
31
plt.grid(True)
21
32
plt.xlabel("Angle (deg)", fontsize=12)
22
- plt.ylabel("Sine", fontsize=12)
23
- plt.title("Sinusoidal",fontsize=14)
33
+ plt.title("Sine and Cosine",fontsize=14)
24
34
25
35
def anim_init():
26
- line.set_data([], [])
27
- return line,
36
+ line1.set_data([], [])
37
+ line2.set_data([], [])
38
+ return (line1, line2,)
28
39
29
40
def anim_update(frame):
30
- x = vec[0:frame]
31
- y = sine[0:frame]
32
- line.set_data(x,y)
33
- return line,
41
+ x1 = vec[0:frame]
42
+ y1 = sine[0:frame]
43
+ x2 = vec[-frame:-1]
44
+ y2 = cosine[-frame:-1]
45
+ line1.set_data(x1,y1)
46
+ line2.set_data(x2,y2)
47
+ return (line1,line2,)
34
48
35
49
anim = FuncAnimation(fig, anim_update, init_func=anim_init,
36
50
frames=500, interval=2, blit=True)
37
-
51
+ anim.save("misc/sin.gif")
38
52
plt.show()
39
- )pyp" , _p (vec), _p (sine));
53
+ )pyp" , _p (vec), _p (sine), _p (cosine) );
40
54
41
55
}
0 commit comments