teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

サンプル追加

2020/06/23 00:55

投稿

magichan
magichan

スコア15898

answer CHANGED
@@ -15,4 +15,62 @@
15
15
  特に`plot_surface()`に拘らないのであれば、`plot_trisurf()`でも良いかもしれません。
16
16
  こちらは三角分割方を使ったサーフェースグラフを描画するもので、`scatter()`と同様に x,y,z に一次元データを渡すことができますので、そのまま `scatter()` を置き換えることができるはずです。(データが無いので未検証です)
17
17
 
18
- [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)
18
+ [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)
19
+
20
+ **【追記】**
21
+
22
+ 修正例
23
+
24
+ ```Python
25
+ import numpy as np
26
+ import matplotlib.pyplot as plt
27
+ from mpl_toolkits.mplot3d import Axes3D
28
+ import matplotlib.animation as animation
29
+ import serial
30
+
31
+ arduinoData = serial.Serial("COM4" , 9600)
32
+
33
+ def update_lines(num):
34
+ arduinoString = arduinoData.readline().decode("ascii")
35
+ dataArray = arduinoString.split(' ')
36
+ if len(dataArray) == 3:
37
+ angle , r , time = [int(dataArray[0]) , float(dataArray[1]) , float(dataArray[2]) ]
38
+ text.set_text("{:d}: [{:.0f},{:.0f},{:.0f}]".format(num, angle, r, time)) # for debugging
39
+ theta = (360 - angle) * 2 * np.pi / 360
40
+ dx = r * np.cos(theta)
41
+ dy = r * np.sin(theta)
42
+ dz = time
43
+
44
+ x.append(dx)
45
+ y.append(dy)
46
+ z.append(dz)
47
+
48
+ if len(x) > 3:
49
+ # ここでは描画処理を行う(描画前に画面をクリアする)
50
+ plt.cla()
51
+ ax.plot_trisurf(x, y, z, alpha = 0.5)
52
+ else:
53
+ pass
54
+
55
+ x = []
56
+ y = []
57
+ z = []
58
+
59
+ fig = plt.figure(figsize=(5, 5))
60
+ ax = fig.add_subplot(111, projection="3d")
61
+
62
+ # ここでは描画しない
63
+ # ax.plot_trisurf(x, y, z, alpha = 0.5)
64
+ text = fig.text(0, 1, "TEXT", va='top') # for debugging
65
+
66
+ ax.set_xlim3d(-20, 20)
67
+ ax.set_ylim3d(-20, 20)
68
+ ax.set_zlim3d(0, 50)
69
+ ax.set_xlabel("Radius_x(cm)")
70
+ ax.set_ylabel("Radius_y(cm)")
71
+ ax.set_zlabel("Time(sec)")
72
+
73
+ # Creating the Animation object
74
+ ani = animation.FuncAnimation(fig, update_lines, frames=200, interval=5000, blit=False)
75
+ plt.show()
76
+ ```