在复平面上绘制复单位根的箭头矢量
我想用matplotlib画出n个单位根,每个根用不同颜色的箭头表示。
这些箭头应该看起来像一个星形,均匀地指向外面,指向单位圆。
matplotlib有一个画箭头的函数,但我能不能用复数来实现这个,还是说我必须转换成实数坐标系?
另外,有没有现成的颜色数组,这样无论我想显示多少个单位根,它都能给我一组不同的颜色?(而不是像七种几乎一模一样的红色阴影)
1 个回答
5
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()
单位根的计算方法和这个回答类似。
更新: 如果你想使用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()