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()