-2
有沒有一種方法,最好使用matplotlib繪製python中的雙變量函數f(x,y); 謝謝,提前。Python3:繪製f(x,y),最好使用matplotlib
有沒有一種方法,最好使用matplotlib繪製python中的雙變量函數f(x,y); 謝謝,提前。Python3:繪製f(x,y),最好使用matplotlib
Z
您可以生成網狀的表達,並呼籲surface_plot
您有一個對應Z的expresson:
#!/usr/bin/python3
import sys
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import numpy
from numpy.random import randn, shuffle
from scipy import linspace, meshgrid, arange, empty, concatenate, newaxis, shape
# =========================
## generating ordered data:
N = 32
x = sorted(randn(N))
y = sorted(randn(N))
X, Y = meshgrid(x, y)
Z = X**2 + Y**2
# ======================================
## reference picture (X, Y and Z in 2D):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet, linewidth=0)
fig.colorbar(surf)
title = ax.set_title("plot_surface: given X, Y and Z as 2D:")
title.set_y(1.01)
ax.xaxis.set_major_locator(MaxNLocator(5))
ax.yaxis.set_major_locator(MaxNLocator(6))
ax.zaxis.set_major_locator(MaxNLocator(5))
fig.tight_layout()
fig.savefig('3D-constructing-{}.png'.format(N))
結果:
surface_plot
以上功能僅用於accepts X, Y and Z as 2D arrays。如果沒有Z表達式,那麼這是不可能的 - 但只是將數據存儲在列表列表中:[[x1, y1, z1],[x2,y2,z2],...]
。在這種情況下,您可以使用plot_trisurf。
在下面我構造X,Y和Z中的2D碼,然後重新塑造數據具有X,Y和Z在圖1D中,將它洗,並用plot_trisurf
繪製相同的數據:
#!/usr/bin/python3
import sys
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import numpy
from numpy.random import randn, shuffle
from scipy import linspace, meshgrid, arange, empty, concatenate, newaxis, shape
# =========================
## generating ordered data:
N = 128
x = sorted(randn(N))
y = sorted(randn(N))
X, Y = meshgrid(x, y)
Z = X**2 + Y**2
# =======================
## re-shaping data in 1D:
# flat and prepare for concat:
X_flat = X.flatten()[:, newaxis]
Y_flat = Y.flatten()[:, newaxis]
Z_flat = Z.flatten()[:, newaxis]
DATA = concatenate((X_flat, Y_flat, Z_flat), axis=1)
shuffle(DATA)
Xs = DATA[:,0]
Ys = DATA[:,1]
Zs = DATA[:,2]
# ====================================================
## plotting surface using X, Y and Z given as 1D data:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_trisurf(Xs, Ys, Zs, cmap=cm.jet, linewidth=0)
fig.colorbar(surf)
title = ax.set_title("plot_trisurf: takes X, Y and Z as 1D")
title.set_y(1.01)
ax.xaxis.set_major_locator(MaxNLocator(5))
ax.yaxis.set_major_locator(MaxNLocator(6))
ax.zaxis.set_major_locator(MaxNLocator(5))
fig.tight_layout()
fig.savefig('3D-reconstructing-{}.png'.format(N))
結果:
你們都相當fast.Thank你! – user3906518 2014-09-10 12:09:27