英文:
Plotting imaginary numbers on a complex plane
问题
我正在尝试在复平面上绘制欧拉公式的图形(e^(ix)),最好使用matplotlib来实现圆形图形(半径为i)。
是否有一种方法可以做到这一点?
到目前为止,我只能在实平面上绘制它,以获得以下代码形式的图形e^(kx):
import math
import numpy as np
import matplotlib.pyplot as plt
i = np.emath.sqrt(-1).imag
e = math.e
x = np.linspace(0, 10, 1000)
plt.scatter(x, (e**(i*x)))
# plt.scatter(x, (np.cos(x) + (i*np.sin(x))))
plt.show()
英文:
I'm trying to plot the graph of Euler's formula (e^(ix)) on a complex plane (preferably with matplotlib) to achieve the circular graph (radius i).
Is there a way I can do this?
So far I've only managed to plot it on a real plane to get a graph in the form e^(kx) with the following code:
import numpy as np
import matplotlib.pyplot as plt
i = np.emath.sqrt(-1).imag
e = math.e
x = np.linspace(0, 10, 1000)
plt.scatter(x, (e**(i*x)))
# plt.scatter(x, (np.cos(x) + (i*np.sin(x))))
plt.show()
答案1
得分: 4
你可以计算一些x的复数函数值,然后在x轴和y轴上分别绘制实部和虚部。请确保不要混淆你所命名的变量x
和图表上的x轴。我会使用t
来避免混淆。
import numpy as np
import matplotlib.pyplot as plt
# 输入参数。
n = 9
t = 2 * np.pi * np.arange(n) / n
# 复数结果。
z = np.exp(1j * t)
fig, ax = plt.subplots()
ax.scatter(np.real(z), np.imag(z))
ax.set_aspect("equal")
绘图结果:
英文:
You can compute the complex-valued function for some values of x and then plot the real and imaginary components on the x- and y-axes, respectively. Make sure not to mix up the variable you're naming x
and the x-axis on the plot. I'll use t
to avoid that confusion.
import numpy as np
import matplotlib.pyplot as plt
# Input parameter.
n = 9
t = 2 * np.pi * np.arange(n) / n
# Complex valued result.
z = np.exp(1j * t)
fig, ax = plt.subplots()
ax.scatter(np.real(z), np.imag(z))
ax.set_aspect("equal")
Plot result:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论