最后终于回答了我自己的问题。在
解决这一问题的一种强力方法是重复数组中的值,从而使“matplotlib”所做的值之间的插值,影响更小,更接近阶跃变化。
这可以通过使用numpy.repeat来实现。由于这是一个三维数组,我们必须在一个轴上迭代,而不是在另一个轴上迭代。否则,将重复展平数组并返回此平面数组。在
结果:
def ImPlot2D3D(img, cmap=plt.cm.jet, step=False, ratio=10):
if step:
img = (img.repeat(ratio, axis=0)).repeat(ratio, axis=1)
Z = img[::1, ::1]
fig = plt.figure(figsize=(14, 7))
# 2D Plot
ax1 = fig.add_subplot(1, 2, 1)
im = ax1.imshow(Z, cmap=cmap)
ax1.set_title('2D')
ax1.grid(False)
# 3D Plot
ax2 = fig.add_subplot(1, 2, 2, projection='3d')
X, Y = np.mgrid[:Z.shape[0], :Z.shape[1]]
ax2.plot_surface(X, Y, Z, cmap=cmap)
ax2.set_title('3D')
# Scale the ticks back down to original values
if step:
ticks_x = ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x / ratio))
ticks_y = ticker.FuncFormatter(lambda y, pos: '{0:g}'.format(y / ratio))
ax1.xaxis.set_major_formatter(ticks_x)
ax1.yaxis.set_major_formatter(ticks_y)
ax2.xaxis.set_major_formatter(ticks_x)
ax2.yaxis.set_major_formatter(ticks_y)
plt.show()
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
SubIm = np.load("Subtract_Image.npy")
ImPlot2D3D(SubIm, step=True)