2013-05-20 32 views
0

我試圖繪製通過有限差分獲得的值網格。因此,所有示例都告訴我如何生成網格輸出xx,然後將這些輸入到f中生成網格計算f(xx,yy)將不起作用。Matplotlib預計算值的表面圖

如果我是在電網堵塞,正如我在填它在下面的例子中,我要求我的轉網格,以便爲它工作。這對我來說沒有任何意義。有人可以解釋嗎?

# Calculations 

import itertools 
import numpy 

x_array = numpy.linspace(0, 1, 5) 
y_array = numpy.linspace(0, 3, 20) 

num_x = len(x_array) 
num_y = len(y_array) 

heights = numpy.zeros((num_x, num_y)) 
for x, y in itertools.product(xrange(num_x), xrange(num_y)): 
    heights[x, y] = numpy.random.normal() + x + y 
    # actual usage is a complicated finite difference scheme, so cannot be made explicit in terms of x & y 

# Plotting 

import matplotlib; matplotlib.use("Qt4Agg") 
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 

fig = plt.figure() 
x_mesh, y_mesh = numpy.meshgrid(x_array, y_array) 
try: 
    ax = fig.add_subplot(211) 
    ax.pcolor(x_mesh, y_mesh, heights) 
except ValueError as E: 
    print "Error:", E 
try: 
    ax = fig.add_subplot(211) 
    ax.pcolor(x_mesh, y_mesh, heights.T) 
    ax = fig.add_subplot(212, projection="3d") 
    ax.plot_surface(x_mesh, y_mesh, heights.T, cmap=matplotlib.cm.coolwarm) 
    colorbar = matplotlib.cm.ScalarMappable(cmap=matplotlib.cm.coolwarm) 
    colorbar.set_array(heights) 
    fig.colorbar(colorbar) 
    print "No problems, but why should heights be transposed??" 
except Exception as E: 
    print "Error:", E 
plt.show() 

回答

0

所以要清楚,這工作但你會問,爲什麼你需要的轉置?

如果你看看你的陣列的形狀:

In [75]: x_mesh.shape 
Out[75]: (20, 5) 

In [76]: y_mesh.shape 
Out[76]: (20, 5) 

In [77]: heights.shape 
Out[77]: (5, 20) 

很顯然,他們匹配元件方面,需要進行轉你的高度。

您對陣列中的哪些方向是xy的說法與matplotlib具有的概念相反。可能與行主要矛頭和大副矛盾有關。