说我有一个网格
xGrid = np.linspace(0.1, 1, 10)
yGrid = np.linspace(5, 10, 5)
以及该网格上的一些数据:
X, Y = np.meshgrid(xGrid, yGrid, indexing='ij')
Z = X*Y + 1
我现在可以轻松地绘制Z(x, y)
。现在,有一个转换t(x, y)
:
T = X+1+Y/2
,我想绘制Z(t(x, y), y)
。为此,我需要将Z
数据投影到t(x,y)-y
平面上。最好的方法是什么?
由于我最终想要绘制数据并且不再做任何进一步的工作,因此也接受在matplotlib
中执行此操作的直接方法(但实际上是绘制正确的新坐标,而不仅仅是重新标记刻度线)
答案 0 :(得分:1)
You can use interpolation to compute the values in the projection, for example with scipy.interpolate.RectBivariateSpline
:
import numpy as np
import scipy.interpolate
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
xGrid = np.linspace(0.1, 1, 10)
yGrid = np.linspace(5, 10, 5)
X, Y = np.meshgrid(xGrid, yGrid, indexing='ij')
Z = X * Y + 1
T = X + 1 + Y / 2
# Interpolate values
interp = scipy.interpolate.RectBivariateSpline(xGrid, yGrid, Z)
Zt = interp.ev(T.ravel(), Y.ravel()).reshape(Z.shape)
# Plot
fig = plt.figure(figsize=(8, 10))
ax1 = fig.add_subplot(211, projection='3d')
ax1.set_title('Original')
ax1.plot_surface(X, Y, Z)
ax2 = fig.add_subplot(212, projection='3d')
ax2.set_title('Projected')
ax2.plot_surface(T, Y, Zt)
fig.tight_layout()
Output:
答案 1 :(得分:1)
如果我了解您的问题,则可以使用pcolormesh
来用于非常规网格
In [8]: import numpy as np
...: import matplotlib.pyplot as plt
...: from matplotlib.collections import PatchCollection, QuadMesh
...: from matplotlib.patches import Rectangle
...:
...: np.random.seed(2018)
...: xGrid = np.linspace(0.1, 1, 10)
...: yGrid = np.linspace(5, 10, 6)
...: X, Y = np.meshgrid(xGrid, yGrid, indexing='ij')
...: Z = X*Y + 1
...: T = X+1+Y/2
...: Zt = T*Y + 1
...: plt.pcolormesh(T, Y, Zt)
...: plt.colorbar()
Out[8]: <matplotlib.colorbar.Colorbar at 0x7fda83cd4ef0>
产生
如果乐队太丑陋,请使用plt.pcolormesh(T, Y, Zt, shading='gouraud')