如何使用matplotlib在3d图形上标记特定区域?

g2ieeal7  于 2023-03-03  发布在  其他
关注(0)|答案(1)|浏览(161)

在一项作业中,我必须绘制一张地球表面温度T与反照率A和发射率Epsilon的三维图,作业的第二部分要求我在图上标出温度在270-300之间的区域。
我该怎么做呢?我试过几种方法,主要是尝试在第一个图上覆盖另一个图,但我无法让它工作。
这是我的作业代码。

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

# Graph of Temperature vs Emissivity
Sigma=5.67e-8
A=0.29
Epsilon=np.arange(0,1,0.02) #atmosphereic emissivity
Fo=1367#flux from sun
do=1
d=1
F=Fo*((do/d)**2)
T_Earth=(((1-A)*F)/(4*Sigma*(1-(1/2*Epsilon))))**(1/4)
#print("The temperature of the Earth is {:.2f}".format(T_Earth))
# plt.scatter(Epsilon,T_Earth)
# plt.savefig("Emissivity")

# Graph of Temperature vs Albedo
A1=np.arange(0,1,0.02)
Epsilon1=0.76 #atmosphereic emissivity
Fo=1367#flux from sun
do=1
d=1
F=Fo*((do/d)**2)
T_Earth1=(((1-A1)*F)/(4*Sigma*(1-(1/2*Epsilon1))))**(1/4)
#print("The temperature of the Earth is {:.2f}".format(T_Earth))
# plt.scatter(A1,T_Earth1)
# plt.savefig("Albedo")

#plt.style.use("_mpl-gallery")
#3D Graph
A2=np.arange(0,1,0.01)
Epsilon2=np.arange(0,1,0.01)
# for A2 in np.arange(0,0.84,0.01)):
#   Epsilon3=np.arange(0,1,0.01)
# for Epsilon4 in np.arange(0,1,0.01):
#   A2=np.arange(0,0.84,0.01)


A2, Epsilon2 = np.meshgrid(A2, Epsilon2)
T_Earth2=(((1-A2)*F)/(4*Sigma*(1-(1/2*Epsilon2))))**(1/4)
fig, ax=plt.subplots(subplot_kw={"projection": "3d"})
surf=ax.plot_surface(A2,Epsilon2, T_Earth2, vmin=T_Earth2.min()*2, cmap=cm.coolwarm, ec="gray", lw=0.2)
plt.colorbar(surf)
# ax.set(xticklabels=[], yticklabels=[], zticklabels=[])
ax.set_xlabel('A')
ax.set_ylabel('E')
ax.set_zlabel('T')

plt.savefig("3D")



# Make data
X = np.arange(-3, 3, 0.25)
Y = np.arange(-3, 3, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

# Plot the surface
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.plot_surface(X, Y, Z, vmin=Z.min() * 2, cmap=cm.viridis)

ax.set(xticklabels=[],
       yticklabels=[],
       zticklabels=[])

X1 = np.arange(-5, 5, 0.25)
Y1 = np.arange(-5, 5, 0.25)
X1, Y1 = np.meshgrid(X1, Y1)
R1 = np.sqrt(X1**2 + Y1**2)
Z1 = np.sin(R1)

# Plot the surface
ax.plot_surface(X1, Y1, Z1, cmap=cm.Blues)
ax.set(xticklabels=[], yticklabels=[], zticklabels=[])
plt.savefig("test2")

这只是让我绘制的第二个图完全取代第一个图,而不是让它们重叠,而不是让第一个图的一部分在另一个图的边缘之外可见。

sczxawaw

sczxawaw1#

如果您想在一个单独的绘图中一起绘制所有要素,您只需要一组figureax。在您的代码中,第一组图形和轴在创建第二组时闭合,因此,先前绘制的要素消失,您得到的是创建新图形/轴后绘制的要素。
以下是修改后的代码和输出,它在单个图中显示了所有要素。

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

# Graph of Temperature vs Emissivity
Sigma=5.67e-8
A=0.29
Epsilon=np.arange(0,1,0.02) #atmosphereic emissivity
Fo=1367#flux from sun
do=1
d=1
F=Fo*((do/d)**2)
T_Earth=(((1-A)*F)/(4*Sigma*(1-(1/2*Epsilon))))**(1/4)

# Graph of Temperature vs Albedo
A1=np.arange(0,1,0.02)
Epsilon1=0.76 #atmosphereic emissivity
Fo=1367#flux from sun
do=1
d=1
F=Fo*((do/d)**2)
T_Earth1=(((1-A1)*F)/(4*Sigma*(1-(1/2*Epsilon1))))**(1/4)

#3D Graph
A2=np.arange(0,1,0.01)
Epsilon2=np.arange(0,1,0.01)

A2, Epsilon2 = np.meshgrid(A2, Epsilon2)
T_Earth2=(((1-A2)*F)/(4*Sigma*(1-(1/2*Epsilon2))))**(1/4)
fig, ax=plt.subplots(subplot_kw={"projection": "3d"})
surf=ax.plot_surface(A2,Epsilon2, T_Earth2, vmin=T_Earth2.min()*2, cmap=cm.coolwarm, ec="gray", lw=0.2)
cb = plt.colorbar(surf, shrink=0.65)

# ax.set(xticklabels=[], yticklabels=[], zticklabels=[])
ax.set_xlabel('A')
ax.set_ylabel('E')
ax.set_zlabel('T')

# Make data
X = np.arange(-3, 3, 0.25)
Y = np.arange(-3, 3, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

# Plot the surface on the same figure
# Just go ahead and plot

# Dont need this line:-
# fig, ax = plt.subplots(subplot_kw={"projection": "3d"})  # <------------ remove
ax.plot_surface(X, Y, Z, vmin=Z.min() * 2, cmap=cm.viridis)

ax.set(xticklabels=[],
       yticklabels=[],
       zticklabels=[])

X1 = np.arange(-5, 5, 0.25)
Y1 = np.arange(-5, 5, 0.25)
X1, Y1 = np.meshgrid(X1, Y1)
R1 = np.sqrt(X1**2 + Y1**2)
Z1 = 50*np.sin(R1)  # <----- Scale it for visualization

# Plot the surface
ax.plot_surface(X1, Y1, Z1, cmap=cm.Blues, alpha=0.5)
ax.set(xticklabels=[], yticklabels=[], zticklabels=[])
plt.show()

相关问题