[英]Python, Matplotlib: Stack multiple heatmaps on top of each other along z-axis in 3D
[英]How do I remove overflow along the z-axis for a 3D matplotlib surface?
我正在嘗試用 matplotlib 繪制 3d 網格表面並限制圖形的限制。 X 和 Y 軸被正確約束,但 Z 軸有溢出。
我錯過了什么? 這是我的代碼:
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits import mplot3d
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(10,10))
x = np.linspace(-6,6,100)
y = np.linspace(-6,6,100)
X,Y = np.meshgrid(x,y)
def f(x,y):
return x**2 + 3*y
Z = f(X,Y)
ax = plt.axes(projection = '3d')
ax.plot_surface(X,Y,Z,cmap='viridis')
ax.title.set_text("z=x**2+3y")
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_zlim3d(zmin=-3,zmax=5)
ax.set_xlim3d(xmin=-6,xmax=6)
ax.set_ylim3d(ymin=-6,ymax=6)
plt.show()
圖表:
您的數據在軸邊界之外。 嘗試旋轉視圖,您會注意到。
z = x**2 + 3*y
如果您只想顯示數據的定義區域,您可以在 Z 數據上添加一個 max() min() 限制,以排除您想要的限制之外的數據。
Z = f(X,Y)
z_tmp = np.maximum(np.minimum(5,Z),-3)
ax = plt.axes(projection = '3d')
ax.plot_surface(X,Y,z_tmp,cmap='viridis')
我不確定 matplotlib 在默認情況下的行為是否正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.