2017-08-11 13 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) 

仅供参考,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_sumsplayer_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

这是排序的伎俩 - 非常感谢你! –