gpt4 book ai didi

python - 将复数单位根绘制为复平面上的箭头向量

转载 作者:太空宇宙 更新时间:2023-11-03 15:13:48 24 4
gpt4 key购买 nike

我想使用 matplotlib 绘制 n 个单位根,每个单位根作为不同颜色的箭头。

它应该看起来像一个星形,箭头等距地指向外在单位圆上。

matplotlib 有一个绘制箭头的函数,但是有什么方法可以使用复数来绘制箭头,还是我必须转换为真正的笛卡尔坐标?

此外,是否存在一组库存颜色,以便无论我希望显示多少个根,它都会给我一组不同的颜色? (而不是说七个几乎相同的红色阴影)

最佳答案

import numpy as np
import pylab as plt
import itertools

n = 13
roots = np.roots( [1,] + [0,]*(n-1) + [-1,] )
colors = itertools.cycle(['r', 'g', 'b', 'y'])

plt.figure(figsize=(6,6))

for root in roots:
plt.arrow(0,0,root.real,root.imag,ec=colors.next())


plt.xlim(-1.5,1.5)
plt.ylim(-1.5,1.5)
plt.show()

enter image description here

单位根的计算方式类似于 this answer .

更新:如果你想使用seaborn,你可以很容易地获得独特的颜色:

import numpy as np
import pylab as plt
import itertools

import seaborn as sns
n = 13
colors = sns.color_palette("hls", n)
roots = np.roots( [1,] + [0,]*(n-1) + [-1,] )

# Sorted by angle
idx = np.argsort([np.angle(x) for x in roots])
roots = roots[idx]

plt.figure(figsize=(6,6))

for root,c in zip(roots,colors):
plt.arrow(0,0,root.real,root.imag,ec=c,lw=3)

plt.xlim(-1.25,1.25)
plt.ylim(-1.25,1.25)
plt.show()

enter image description here

关于python - 将复数单位根绘制为复平面上的箭头向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23299542/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com