使用黑线跟踪3d图,其中Z = 0? [英] Trace a 3d graph with a black line where Z = 0?
问题描述
我有一个可以正常工作的3d图形,但是我想在图形上画出z = 0时的轨迹线.
I have a functional 3d graph, but I want to make a trace line on the graph for when z = 0.
我尝试将z> = 0和z< 0时的图进行拆分,但这并不能清楚地表示出来,如注释出的代码所示.我想用另一种颜色描绘这条线.另一种解决方案是使图的一部分z> = 0为一种颜色,而z< 0为另一种颜色,但是我对此也不断遇到错误.
I tried to split up the graphs for when z>=0 and z<0 but this does not make a clear representation, as shown in code commented out. I want to trace this line in a different color. Another solution would be to have part of the graph z>=0 be one color and z<0 be another color, but I keep getting an error for this as well.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
def equation(delta=0.05):
#for F=0.5
x = np.arange(0,1,delta)
y = np.arange(2,6,delta)
X,Y = np.meshgrid(x,y)
Z = (X*Y-X-0.5*Y**2+2*0.5*Y)**2-4*(0.5*Y**2-0.5*Y)*(X-X*Y+Y-0.5*Y)
return X, Y, Z
#x = P
#y = K
fig = plt.figure()
ax = Axes3D(fig)
#set labels for graph
ax.set_xlabel('P')
ax.set_ylabel('K')
ax.set_zlabel('Z')
#set colors about and below 0
#c = (Z<=0)
#ax.plot_surface(x,y,z,c=c,cmap='coolwarm')
#ax.plot_surface(x,y,z,c= z<0)
c = z=0
x,y,z = equation(0.01)
surf=ax.plot_surface(x,y,z)
#surf=ax.plot_surface(x,y,z<0)
#surf=ax.plot_surface(x,y,z>=0)
#surf =ax.plot_surface(x,y,z, rstride=5, cstride=5)
#surf = ax.plot_trisurf(x,y,z,cmap=cm.jet,linewidth=0.1,vmin=-15, vmax=100)
#surf = ax.plot_surface(x,y,z,rstride = 5, cstride #=5,cmap=cm.RdBu,linewidth=0, antialiased=False)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
#fig.colorbar(surf, shrink= 0.5, aspect=5)
#ax.view_init(elev=25,azim=-120)
plt.show()
推荐答案
仅突出显示Z = 0线时,您需要记住,此时您不再具有曲面而是2D平面.然后,您想找到2D平面等于零的位置.您想使用Poolka建议的ax.contour(x,y,z,[0])
.我建议更改绘图中的透明度(alpha
),以使该线更清晰可见.
When just highlighting the Z=0 line you need to remember that at that point you no longer have a surface but a 2D plane. You then want to find where that 2D plane is equal to zero. You want to use what Poolka suggested which is ax.contour(x,y,z,[0])
. I would suggest changing the transparency (alpha
) in the plots to make that line more visible.
您还可以通过创建自定义颜色图并使vmin
和vmax
居中于零为中心,使这2个区域以零两种不同的颜色分开.
You can also make those 2 regions separated at zero 2 different colors by creating a custom colormap and making your vmin
and vmax
centered around zero.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
import matplotlib.colors
def equation(delta=0.05):
x = np.arange(0,1,delta)
y = np.arange(2,6,delta)
X,Y = np.meshgrid(x,y)
Z = (X*Y-X-0.5*Y**2+2*0.5*Y)**2-4*(0.5*Y**2-0.5*Y)*(X-X*Y+Y-0.5*Y)
return X, Y, Z
fig = plt.figure()
ax = Axes3D(fig)
#set labels for graph
ax.set_xlabel('P')
ax.set_ylabel('K')
ax.set_zlabel('Z')
#Create custom colormap with only 2 colors
colors = ["blue","red"]
cm1 = LinearSegmentedColormap.from_list('my_list', colors, N=2)
x,y,z = equation(0.01)
surf=ax.plot_surface(x,y,z,alpha=.7,cmap=cm1,vmin=-150,vmax=150) #use custom colormap
#Use a contour plot to isolate Z=0 since it is a line and no longer a surface
ax.contour(x,y,z,[0],colors='k',linewidths=3)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
plt.show()
这篇关于使用黑线跟踪3d图,其中Z = 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!