回答編集履歴

1

コードの追加

2022/10/13 05:35

投稿

ps_aux_grep
ps_aux_grep

スコア1581

test CHANGED
@@ -1,3 +1,48 @@
1
1
  現状のデータとしては,三次元空間の等高線を縦にぶった切って得られた断面の高さ情報しか持ってないので,ここから等高線を描くとしたら,断面から三次元空間の物体を予測して描くしかないです.
2
2
 
3
3
  代替案としては,`plt.scatter()`を使って2Dで描くならカラーマップを`z`で割り当てる,または3Dでそのまま散布図を描画する方法があります.
4
+
5
+ 厳密に,`x`と`y`が周期的に一致して次のようになっていれば,`np.meshgrid()`を適用した`plt.contor()`が使えます.
6
+ ```Python
7
+ import numpy as np
8
+ from itertools import product
9
+ from matplotlib import pyplot as plt
10
+
11
+ def del_duplicates(x): # to ordered monotonically
12
+ return sorted(set(x), key = x.index)
13
+
14
+ x = [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3] # x is not ordered by monotonically
15
+ y = [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3]
16
+ z = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
17
+
18
+ dx, dy = map(del_duplicates, (x, y))
19
+ X, Y = np.meshgrid(dx, dy)
20
+ Z = np.zeros_like(X)
21
+
22
+ for n, (i, j) in enumerate(product(range(len(dx)), range(len(dy)))):
23
+ Z[i, j] = z[n]
24
+
25
+ print(Z)
26
+
27
+ fig, ax = plt.subplots(1, 3)
28
+ ax[0].scatter(x, y, c = z)
29
+ ax[0].set_xlabel('x')
30
+ ax[0].set_ylabel('y')
31
+ ax[0].set_title('scatter')
32
+
33
+ ax[1].tricontour(x, y, z)
34
+ ax[1].set_xlabel('x')
35
+ ax[1].set_ylabel('y')
36
+ ax[1].set_title('tricontour')
37
+
38
+ ax[2].contour(X, Y, Z)
39
+ ax[2].set_xlabel('x')
40
+ ax[2].set_ylabel('y')
41
+ ax[2].set_title('contour')
42
+
43
+ plt.show()
44
+ ```
45
+ ただ,このような変換ができるということは当然`plt.tricontour()`を使うこともできるということになりますね.
46
+
47
+ 結果は次のようになります.
48
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-10-13/27058dcb-fa08-4919-b49a-1a31ed71c3fa.png)