如何在python中绘制f(x,y)=sqrt(2x-y)
我试图绘制以下多变量f(x,y)=sqrt(2x-y) 但是不能让它与numpy和matplotlib一起工作 我一直在尝试定义函数,但仍然无法使其工作如何在python中绘制f(x,y)=sqrt(2x-y),python,Python,我试图绘制以下多变量f(x,y)=sqrt(2x-y) 但是不能让它与numpy和matplotlib一起工作 我一直在尝试定义函数,但仍然无法使其工作 from numpy import exp,arange from pylab import meshgrid,cm,imshow,contour,clabel,colorbar,axis,title,show from math import sqrt # the function that I'm going to plot def z_
from numpy import exp,arange
from pylab import meshgrid,cm,imshow,contour,clabel,colorbar,axis,title,show
from math import sqrt
# the function that I'm going to plot
def z_func(x,y):
return (sqrt(2*x - y))
X,Y = meshgrid(x, y) # grid of point
Z = z_func(X, Y) # evaluation of the function on the grid
im = imshow(Z,cmap=cm.RdBu) # drawing the function
# adding the Contour lines with labels
cset = contour(Z,arange(-1,1.5,0.2),linewidths=2,cmap=cm.Set2)
clabel(cset,inline=True,fmt='%1.1f',fontsize=10)
colorbar(im) # adding the colobar on the right
# latex fashion title
title('my plot')
show()
为了绘制整个函数,需要有更多的数据。 请参考以下代码
import numpy as np
import math
import matplotlib.pyplot as plt
def z_func(x,y):
return (math.sqrt(2*x - y))
x = [10,20,30,40,50]
y =[2,4,6,8,11]
Z = []
for i in range(len(x)):
Z.append(z_func(x[i],y[i]))
plt.plot(Z)
错误是什么?
但无法使其工作
-这是什么意思?
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
# the function that I'm going to plot.
# Vectorize so we don't need to loop through
# grid points.
@np.vectorize
def z_func(x, y):
return (np.sqrt(2*x - y))
# define the range where you evaluate
# the function
extent = (0, 10, 0, 10)
x = np.arange(0, 10.1, .1)
y = np.arange(0, 10.1, .1)
# create grid
X, Y = np.meshgrid(x, y)
# evaluate over grid
Z = z_func(X, Y)
# plot contour image
fig = plt.figure()
im = plt.imshow(Z, origin='image', cmap=cm.RdBu, extent=extent)
cset = plt.contour(Z, np.arange(-1,1.5,0.2),linewidths=2,cmap=cm.Set2, extent=extent)
plt.clabel(cset,inline=True, fmt='%1.1f',fontsize=10)
plt.colorbar(im)
plt.show()