Python - 3D 绘图、水平线缺失和不正确的渐变显示

Python - 3D Plotting, horizontal lines missing and incorrect gradients showing

我是 3D 表面图的新手,我正在尝试使用以下方法绘制 3D 温度图作为距离和时间的函数:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm    

t = np.arange(0,60,1)
z = np.arange(5,85,5)

fig=plt.figure(1)
ax = fig.gca(projection='3d')
X, Y = np.meshgrid(z, t)
surface=ax.plot_surface(X,Y,T1, linewidth=1,cmap=cm.coolwarm, antialiased=False)
fig.colorbar(surface, shrink=0.5, aspect=5)
ax.view_init(ax.elev, ax.azim+90)
ax.set_title("Temperature Distribution 1")
ax.set_xlabel('z (cm)')
ax.set_ylabel('Time (min)')
ax.set_zlabel('Temperature ($^\circ$C)')
ax.set_xticks(np.arange(0,80, 15))
plt.savefig("3D_1.png",format='png',dpi=1000,bbox_inches='tight')
plt.show()

T1是二维数据。这会产生以下内容:

在60cm左右只有1条水平线显示,但是我想要每5cm有一条水平线(数据是沿着每5cm采集的)。似乎沿距离轴的图只有 2 个部分。颜色渐变以大块显示,而不是显示为沿长度的温度函数。

例如在距离 0~40cm 的 50-60 分钟内,温度从 ~180 度变为 ~20 度,但该块的颜色一直是暗红色,而不是它应该从暗红色开始并减少到蓝色.如何让温度沿整个长度轴显示正确的梯度。

此外,温度图例以 % 为单位,而不是以度为单位的温度值,我该如何解决这个问题?

查看 documentation of surface 我们发现

The rstride and cstride kwargs set the stride used to sample the input data to generate the graph. If 1k by 1k arrays are passed in, the default values for the strides will result in a 100x100 grid being plotted. Defaults to 10.

因此,使用

surface=ax.plot_surface(X,Y,T1, rstride=8, cstride=8)

你得到

正在使用

surface=ax.plot_surface(X,Y,T1, rstride=5, cstride=1)

你得到


这是如何为这种情况创建可重现数据的示例:

t = np.arange(0,60,1)
z = z=np.arange(5,85,5)
f = lambda z, t, z0, t0, sz, st: 180.*np.exp(-(z-z0)**2/sz**2 -(t-t0)**2/st**2)
X, Y = np.meshgrid(z, t)
T1 =f(X,Y,-20.,56, 40.,30.)