回答編集履歴

1

サンプル追加

2020/06/23 00:55

投稿

magichan
magichan

スコア15898

test CHANGED
@@ -33,3 +33,119 @@
33
33
 
34
34
 
35
35
  [https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#mpl_toolkits.mplot3d.Axes3D.plot_trisurf](https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#mpl_toolkits.mplot3d.Axes3D.plot_trisurf)
36
+
37
+
38
+
39
+ **【追記】**
40
+
41
+
42
+
43
+ 修正例
44
+
45
+
46
+
47
+ ```Python
48
+
49
+ import numpy as np
50
+
51
+ import matplotlib.pyplot as plt
52
+
53
+ from mpl_toolkits.mplot3d import Axes3D
54
+
55
+ import matplotlib.animation as animation
56
+
57
+ import serial
58
+
59
+
60
+
61
+ arduinoData = serial.Serial("COM4" , 9600)
62
+
63
+
64
+
65
+ def update_lines(num):
66
+
67
+ arduinoString = arduinoData.readline().decode("ascii")
68
+
69
+ dataArray = arduinoString.split(' ')
70
+
71
+ if len(dataArray) == 3:
72
+
73
+ angle , r , time = [int(dataArray[0]) , float(dataArray[1]) , float(dataArray[2]) ]
74
+
75
+ text.set_text("{:d}: [{:.0f},{:.0f},{:.0f}]".format(num, angle, r, time)) # for debugging
76
+
77
+ theta = (360 - angle) * 2 * np.pi / 360
78
+
79
+ dx = r * np.cos(theta)
80
+
81
+ dy = r * np.sin(theta)
82
+
83
+ dz = time
84
+
85
+
86
+
87
+ x.append(dx)
88
+
89
+ y.append(dy)
90
+
91
+ z.append(dz)
92
+
93
+
94
+
95
+ if len(x) > 3:
96
+
97
+ # ここでは描画処理を行う(描画前に画面をクリアする)
98
+
99
+ plt.cla()
100
+
101
+ ax.plot_trisurf(x, y, z, alpha = 0.5)
102
+
103
+ else:
104
+
105
+ pass
106
+
107
+
108
+
109
+ x = []
110
+
111
+ y = []
112
+
113
+ z = []
114
+
115
+
116
+
117
+ fig = plt.figure(figsize=(5, 5))
118
+
119
+ ax = fig.add_subplot(111, projection="3d")
120
+
121
+
122
+
123
+ # ここでは描画しない
124
+
125
+ # ax.plot_trisurf(x, y, z, alpha = 0.5)
126
+
127
+ text = fig.text(0, 1, "TEXT", va='top') # for debugging
128
+
129
+
130
+
131
+ ax.set_xlim3d(-20, 20)
132
+
133
+ ax.set_ylim3d(-20, 20)
134
+
135
+ ax.set_zlim3d(0, 50)
136
+
137
+ ax.set_xlabel("Radius_x(cm)")
138
+
139
+ ax.set_ylabel("Radius_y(cm)")
140
+
141
+ ax.set_zlabel("Time(sec)")
142
+
143
+
144
+
145
+ # Creating the Animation object
146
+
147
+ ani = animation.FuncAnimation(fig, update_lines, frames=200, interval=5000, blit=False)
148
+
149
+ plt.show()
150
+
151
+ ```