简体   繁体   English

在python中用多个参数绘制f(X,Y)

[英]Plot f(X, Y) with multiple parameters in python

I have pretty specific task here. 我在这里有非常具体的任务。 Lets say I have function of 5 variables. 可以说我有5个变量的函数。 I want to make a 3d plot, where Ill take 2 variables as variables and 3 variables as fixed parameters. 我想制作一个3D图,其中Ill将2个变量作为变量,将3个变量作为固定参数。 Here is an example: 这是一个例子:

import matplotlib.pyplot as plt
import matplotlib
from mpl_toolkits.mplot3d import axes3d

import numpy

#desired function is for example f(x, y, {a,b,c}) = x*a/((y+b)*(y+c))
a = 3
b = 8.5
c = 3.1
x = [i for i in range(-10,10,1)]
y = x

X, Y = numpy.meshgrid(x, y)

#this part of code is wrong
Z = X*a / ((Y+b)*(Y+c))

Lets plot it as in here . 让我们像这里一样绘制它。

fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
surf = ax1.plot_surface(X, Y, Z, rstride = 5, cstride = 5, 
       cmap = matplotlib.cm.jet)
fig.colorbar(surf)
plt.show()

Thank you for any hints or helps! 感谢您的任何提示或帮助!

You have an error on ax1 which is undefined. 您在ax1上有一个未定义的错误。 This edited code should work: 修改后的代码应该可以工作:

import matplotlib.pyplot as plt
import matplotlib
from mpl_toolkits.mplot3d import axes3d
import numpy

#desired function is for example f(x, y, {a,b,c}) = x*a/((y+b)*(y+c))
a = 3
b = 8.5
c = 3.1
x = [i for i in range(-10,10,1)]
y = x

X, Y = numpy.meshgrid(x, y)
Z = X*a / ((Y+b)*(Y+c))

fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
surf = ax.plot_surface(X, Y, Z, rstride = 5, cstride = 5, \
       cmap = matplotlib.cm.jet)

fig.colorbar(surf)
plt.show()

在此处输入图片说明

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

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