P i*_*P i 3 python matplotlib color-mapping complex-numbers
我想使用 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()
Run Code Online (Sandbox Code Playgroud)
单位根的计算方式与此答案类似。
更新:如果你想使用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()
Run Code Online (Sandbox Code Playgroud)