如何在 Python 中绘制从域区域到辅域区域的函数?
How can I draw a function that from domain region to codomain region in Python?
我要计算绘制的图就是上图
from matplotlib import pyplot as plt
from numpy import *
def f(z):
return (1/z)
#r is radius, p is radian
#below is strange part
for r in linspace(0,1,50):
for p in linspace(0,2*pi,50):
Z1=array([complex(r*cos(p),r*sin(p))])
W1 = f(Z1)
plt.figure(figsize=(8,8))
plt.xlim(-pi/2,pi,2);plt.ylim(-pi/2,pi/2)
for i in range(0,2499):
plt.plot(real(W1[i]), imag(W1[i]), c='b', lw=1.5, ls='-')
plt.axvline(x=0,color='k', lw=1)
plt.axhline(y=0,color='k', lw=1)
plt.show()
code是圆心为(0,0),半径为1的圆
我是新手Python,所以代码还不完整,
但是有些地方很奇怪,所以请修复它。
或者如果有更好的方法,请告诉我。
可以从这里对您的代码进行一些编辑。
我使用 numpy 向量化操作重写了 for 循环。另外,我不确定将 rs
设置为 linspace
是否正确。由于您正在尝试绘制一个圆,因此半径应该相同,因此您可以改用 np.full
。
from matplotlib import pyplot as plt
from numpy import *
def f(z):
return (1/z)
#r is radius, p is radian
# for r in linspace(0,1,50):
# for p in linspace(0,2*pi,50):
# Z1=array([complex(r*cos(p),r*sin(p))])
# W1 = f(Z1)
# rewriting the above code in a vectorized style
rs = linspace(0, 1, 50)
ps = linspace(0, 2*pi, 50)
Z1 = rs * cos(ps) + 1j * rs * sin(ps)
W1 = f(Z1)
# gussing that you want to plot a circle of radius 1 centered at (1, 0)
# in this case, rs should be constant
rs = full(50, 1.0)
Z1 = rs * cos(ps) + 1j * rs * sin(ps)
# shift the real component
Z1.real += 1
W1 = f(Z1)
plt.figure(figsize=(8,8))
plt.xlim(-pi/2,pi,2);plt.ylim(-pi/2,pi/2)
plt.plot(Z1.real, Z1.imag, c='b', lw=1.5, ls="-")
plt.plot(W1.real, W1.imag, c='r', lw=1.5, ls="-")
# for i in range(0,2499):
# plt.plot(real(W1[i]), imag(W1[i]), c='b', lw=1.5, ls='-')
plt.axvline(x=0,color='k', lw=1)
plt.axhline(y=0,color='k', lw=1)
plt.show()
我要计算绘制的图就是上图
from matplotlib import pyplot as plt
from numpy import *
def f(z):
return (1/z)
#r is radius, p is radian
#below is strange part
for r in linspace(0,1,50):
for p in linspace(0,2*pi,50):
Z1=array([complex(r*cos(p),r*sin(p))])
W1 = f(Z1)
plt.figure(figsize=(8,8))
plt.xlim(-pi/2,pi,2);plt.ylim(-pi/2,pi/2)
for i in range(0,2499):
plt.plot(real(W1[i]), imag(W1[i]), c='b', lw=1.5, ls='-')
plt.axvline(x=0,color='k', lw=1)
plt.axhline(y=0,color='k', lw=1)
plt.show()
code是圆心为(0,0),半径为1的圆
我是新手Python,所以代码还不完整,
但是有些地方很奇怪,所以请修复它。
或者如果有更好的方法,请告诉我。
可以从这里对您的代码进行一些编辑。
我使用 numpy 向量化操作重写了 for 循环。另外,我不确定将 rs
设置为 linspace
是否正确。由于您正在尝试绘制一个圆,因此半径应该相同,因此您可以改用 np.full
。
from matplotlib import pyplot as plt
from numpy import *
def f(z):
return (1/z)
#r is radius, p is radian
# for r in linspace(0,1,50):
# for p in linspace(0,2*pi,50):
# Z1=array([complex(r*cos(p),r*sin(p))])
# W1 = f(Z1)
# rewriting the above code in a vectorized style
rs = linspace(0, 1, 50)
ps = linspace(0, 2*pi, 50)
Z1 = rs * cos(ps) + 1j * rs * sin(ps)
W1 = f(Z1)
# gussing that you want to plot a circle of radius 1 centered at (1, 0)
# in this case, rs should be constant
rs = full(50, 1.0)
Z1 = rs * cos(ps) + 1j * rs * sin(ps)
# shift the real component
Z1.real += 1
W1 = f(Z1)
plt.figure(figsize=(8,8))
plt.xlim(-pi/2,pi,2);plt.ylim(-pi/2,pi/2)
plt.plot(Z1.real, Z1.imag, c='b', lw=1.5, ls="-")
plt.plot(W1.real, W1.imag, c='r', lw=1.5, ls="-")
# for i in range(0,2499):
# plt.plot(real(W1[i]), imag(W1[i]), c='b', lw=1.5, ls='-')
plt.axvline(x=0,color='k', lw=1)
plt.axhline(y=0,color='k', lw=1)
plt.show()