Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8979403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:50:23+00:00 2026-06-15T19:50:23+00:00

Is it possible to query the current state of the matplotlib color cycle? In

  • 0

Is it possible to query the current state of the matplotlib color cycle? In other words is there a function get_cycle_state that will behave in the following way?

>>> plot(x1, y1)
>>> plot(x2, y2)
>>> state = get_cycle_state()
>>> print state
2

Where I expect the state to be the index of the next color that will be used in a plot. Alternatively, if it returned the next color (“r” for the default cycle in the example above), that would be fine too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:50:25+00:00Added an answer on June 15, 2026 at 7:50 pm

    Accessing the color cycle iterator

    There’s no “user-facing” (a.k.a. “public”) method to access the underlying iterator, but you can access it through “private” (by convention) methods. However, you’d can’t get the state of an iterator without changing it.

    Setting the color cycle

    Quick aside: You can set the color/property cycle in a variety of ways (e.g. ax.set_color_cycle in versions <1.5 or ax.set_prop_cycler in >=1.5). Have a look at the example here for version 1.5 or greater, or the previous style here.

    Accessing the underlying iterator

    However, while there’s no public-facing method to access the iterable, you can access it for a given axes object (ax) through the _get_lines helper class instance. ax._get_lines is a touch confusingly named, but it’s the behind-the-scenes machinery that allows the plot command to process all of the odd and varied ways that plot can be called. Among other things, it’s what keeps track of what colors to automatically assign. Similarly, there’s ax._get_patches_for_fill to control cycling through default fill colors and patch properties.

    At any rate, the color cycle iterable is ax._get_lines.color_cycle for lines and ax._get_patches_for_fill.color_cycle for patches. On matplotlib >=1.5, this has changed to use the cycler library, and the iterable is called prop_cycler instead of color_cycle and yields a dict of properties instead of only a color.

    All in all, you’d do something like:

    import matplotlib.pyplot as plt
    
    fig, ax = plt.subplots()
    color_cycle = ax._get_lines.color_cycle
    # or ax._get_lines.prop_cycler on version >= 1.5
    # Note that prop_cycler cycles over dicts, so you'll want next(cycle)['color']
    

    You can’t view the state of an iterator

    However, this object is a “bare” iterator. We can easily get the next item (e.g. next_color = next(color_cycle), but that means that the next color after that is what will be plotted. By design, there’s no way to get the current state of an iterator without changing it.

    In v1.5 or greater, it would be nice to get the cycler object that’s used, as we could infer its current state. However, the cycler object itself isn’t accessible (publicly or privately) anywhere. Instead, only the itertools.cycle instance created from the cycler object is accessible. Either way, there’s no way to get to the underlying state of the color/property cycler.

    Match the color of the previously plotted item instead

    In your case, it sounds like you’re wanting to match the color of something that was just plotted. Instead of trying to determine what the color/property will be, set the color/etc of your new item based on the properties of what’s plotted.

    For example, in the case you described, I’d do something like this:

    import matplotlib.pyplot as plt
    import numpy as np
    
    def custom_plot(x, y, **kwargs):
        ax = kwargs.pop('ax', plt.gca())
        base_line, = ax.plot(x, y, **kwargs)
        ax.fill_between(x, 0.9*y, 1.1*y, facecolor=base_line.get_color(), alpha=0.5)
    
    x = np.linspace(0, 1, 10)
    custom_plot(x, x)
    custom_plot(x, 2*x)
    custom_plot(x, -x, color='yellow', lw=3)
    
    plt.show()
    

    enter image description here

    It’s not the only way, but its cleaner than trying to get the color of the plotted line before-hand, in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Mysql query: retrieve current date query I need a MySQL query that
I need, if possible, a T-SQL query that will return the values in a
Possible Duplicate: SQL Server Database query help Hi, I have a problem that I
So I have an awful query that current exists in MS Access that I
Is is possible to query facebook for information on which pages (on current domain)
Possible Duplicate: Postgres Dynamic Query Function I wish to use the returned string from
I see that in PL/SQL it is possible to set the session state of
Is is possible to query the current browser from within an applet? I would
Is it possible to query for specific objects within a nested document? Heres an
Is it possible to query using Facebook Query Language when not currently logged in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.