0

I have a group of ellipses to plot, but the ellipses would be plotted in the same color. This makes it hard to differentiate one from another:

# 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)

enter image description here

I want to know how can I assign a color to each when plotting, so the ellpises would be easier to look at.

1 Answer 1

2

You can access Matplotlib's color-cycle with ax._get_lines.color_cycle. This is an iterator, so call next() on it each time you draw an ellipse:

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() 

enter image description here

To specify the colors cycled through, you can set them explicitly as rcParams before plotting. e.g., for magenta, red, yellow:

mpl.rcParams['axes.color_cycle'] = ['m', 'r', 'y']

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

This is much better than what I've thought. What I did is create a corlor_set = ['r','g','b'], and turn the iteration into for i, ell in enumerate(ell_group).
But, how can I change the color style? Mine is quite different from yours.
There is an additional question, what if I what to plot 4 intead of 3 ellpise? Then I need to add the additional color if I manually specified color set. I'm now thinking ax._get_lines.color_cycle may be a better option?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.