Matplotlib&;Python:在进行迭代绘制时指定颜色
我有Matplotlib&;Python:在进行迭代绘制时指定颜色,python,matplotlib,Python,Matplotlib,我有一组要打印的椭圆,但椭圆将以相同的颜色打印。这使得很难区分两者: # import matplotlib as mpl # from matplotlib import pyplot as plt def plot_ellpises(ell_groups): print 'GMM Plot Result' fig, ax = plt.subplots() for ell in ell_groups: xy, width, height, angle
一组要打印的椭圆
,但椭圆将以相同的颜色打印。这使得很难区分两者:
# import matplotlib as mpl
# from matplotlib import pyplot as plt
def plot_ellpises(ell_groups):
print 'GMM Plot Result'
fig, ax = plt.subplots()
for ell in ell_groups:
xy, width, height, angle = ell
ell = mpl.patches.Ellipse(xy=xy, width=width, height=height, angle = angle)
ell.set_alpha(0.5)
ax.add_patch(ell)
ax.autoscale()
ax.set_aspect('equal')
return plt.show()
ell1= [-7.13529086, 5.28809598], 4.42823535, 5.97527801,107.60800706
ell2= [.96850139, 1.33792516], 5.73498868,8.98934084,97.8230716191
ell3= [1.43665497, 3.87692805], 1.42859078, 1.95525638,83.135072216
ell_groups = [ell1,ell2,ell3]
plot_ellpises(ell_groups)
我想知道如何在打印时为每个元素指定颜色,以便更容易查看Ellpise。您可以使用
ax访问Matplotlib的颜色循环。\u获取线。颜色循环
。这是一个迭代器,因此每次绘制椭圆时都要调用它的next()
:
def plot_ellpises(ell_groups):
print 'GMM Plot Result'
fig, ax = plt.subplots()
colors = ax._get_lines.color_cycle
for ell in ell_groups:
xy, width, height, angle = ell
ell = mpl.patches.Ellipse(xy=xy, width=width, height=height,
angle = angle, facecolor=next(colors))
ell.set_alpha(0.5)
ax.add_patch(ell)
ax.autoscale()
ax.set_aspect('equal')
return plt.show()
要指定循环使用的颜色,可以在打印前将其显式设置为rcParams。e、 例如,对于洋红色、红色、黄色:
mpl.rcParams['axes.color_cycle'] = ['m', 'r', 'y']
这比我想象的要好得多。我所做的是创建一个
corlor\u集合=['r','g','b']
,并将迭代转换为,用于枚举(ellu组)中的I,ell。
但是,如何更改颜色样式?我的和你的完全不同。还有一个问题,如果我在3
ellpise的中间绘制4
intead怎么办?如果我手动指定了颜色集,那么我需要添加额外的颜色。我现在在想,ax.\u获取线。颜色循环
可能是一个更好的选择?