簡體   English   中英

如何在python中繪制f(x,y)= sqrt(2x-y)

[英]how to plot in python f(x,y)=sqrt(2x-y)

我正在嘗試繪制以下多變量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_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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM