2017-08-11 7 views
2

サーフェスプロットを生成する次のコードがありますが、これは私が期待するものではありません。Matplotlibサーフェスプロット直観的でない三角形分割

xx, yy = np.meshgrid(dealer_sums, player_sums) 
    def getter(dealer_sum, player_sum): 
     state = (dealer_sum, player_sum) 
     return self.get_value(state) 
    z = np.vectorize(getter) 
    zz = z(xx,yy) 

    fig = plt.figure() 
    ax = fig.add_subplot(111, projection='3d') 
    ax.plot_wireframe(xx,yy, zz) 

FYIでは、xx、yy、zzの形状はすべて同じで2Dです。これについては他の記事(surface plots in matplotlib; Simplest way to plot 3d surface given 3d points)を見てから

それは一般的な問題のように見えるが、xとy座標が不規則であるということですが、私が正しく理解しているならば、私は私はすでにnp.meshgridへの呼び出しを通じて正則ていると思います?

私は、データが表面せずにどのように見えるかを示すために、以下の散布図を提供しています enter image description here

、これはplot_wireframeへの呼び出しは次のようになります。私は数の上に描かれている
enter image description here 私が期待していなかったライン。私の質問は、これらの線を取り除き、このような表面を作ることが可能なのでしょうか? enter image description here

ありがとうございました。

編集:ここではそれが規則的であることを示す、XYグリッドの散布図である: enter image description here

答えて

1

dealer_sumsplayer_sumsがそうでなければmeshgrid、 を呼び出す前にソートされ、ワイヤフレームに接続されているポイントはまた、意志を確認してください順不同であること:左側に

import numpy as np 
import matplotlib.pyplot as plt 
import mpl_toolkits.mplot3d.axes3d as axes3d 

def z(xx, yy): 
    return -xx + (yy-18)**2 

dealer_sums = [1, 5, 9] 
player_sums = [14, 17, 21, 14] 

fig = plt.figure() 
ax = fig.add_subplot(1, 2, 1, projection='3d') 
ax2 = fig.add_subplot(1, 2, 2, projection='3d') 

xx, yy = np.meshgrid(dealer_sums, player_sums) 
zz = z(xx, yy) 
ax.plot_wireframe(xx, yy, zz) 

xx2, yy2 = np.meshgrid(np.unique(dealer_sums), np.unique(player_sums)) 
zz2 = z(xx2, yy2) 

ax2.plot_wireframe(xx2, yy2, zz2) 
plt.show() 

enter image description here

dealer_sumsおよびplayer_sumsは未分類であった。 右側に並べ替えられています。


np.uniqueは、ソート順に一意の値を返します。上では、実際に最も重要な並べ替えですが、重複する座標でグリッドを作成する必要はないので、一意性が追加の利点です。


meshgridは、必ずしも通常のグリッドを返すわけではありません。 dealer_sumsおよび/またはplayer_sumsが不規則な場合は、xxおよび/またはyyとなります。

In [218]: xx, yy = np.meshgrid([0,2,1], [0, .5, 10]) 

In [219]: xx 
Out[219]: 
array([[0, 2, 1], 
     [0, 2, 1], 
     [0, 2, 1]]) 

In [220]: yy 
Out[220]: 
array([[ 0. , 0. , 0. ], 
     [ 0.5, 0.5, 0.5], 
     [ 10. , 10. , 10. ]]) 
+0

トリックを仕分けしました - ありがとうございました! –

関連する問題