简体   繁体   English

Python图3D表面绘图

[英]Python plot 3d surface drawing

I have a function F = 0.8*X+0.6*Y+0.9*Z But my problem is XY and Z related to each other. 我有一个函数F = 0.8*X+0.6*Y+0.9*Z但是我的问题是XY和Z相互关联。 X+Y+Z = 1 . X+Y+Z = 1 The values and intervals should be like: 值和间隔应类似于:

X = np.arange(0,1,0.01)
Y = np.arange(0,(1-X),0.01)
Z = 1-(X+Y)

Could you please give a simple way of doing this in Python. 您能否用Python给出一种简单的方法。 Thanks in advance. 提前致谢。

If you want to do a surface plot in matplotlib your x, y and z arrays should be 2D arrays. 如果要在matplotlib中进行表面绘图,则x,y和z数组应为2D数组。 If interval of your variables depends on each other, you can define your variables in terms of variables whose intervals is independent of each other like u and v in here. 如果变量的间隔彼此依赖,则可以根据变量的间隔来定义变量,这些变量的间隔彼此独立,如此处的u和v。

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.colors import Normalize
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

u = np.linspace(0, 1, 100)
v = np.linspace(1, 0, 100)

x = np.outer(np.ones_like(u), v)
y = np.outer(v, u)
z = 1 - x - y
f = 0.8*x + 0.6*y + 0.9*z

norm = Normalize (f.min(), f.max())
m = plt.cm.ScalarMappable()
m.set_array([])
fcolors = m.to_rgba(f)

ax.view_init(azim=45)
ax.plot_surface(x, y, z, norm=norm, rstride=2, cstride=2, facecolors=fcolors, shade=False)

plt.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM