Skip to content

ENH: Set the alpha value for plots in rcParams #5132

Closed

Description

@Jul3k

Using Matplotlib i often find myself repeadily writing the following to change the alpha value of the plots:

plot(x,y1, alpha=.6)
plot(x,y2, alpha=.6)
plot(x,y3, alpha=.6)
...

I was hoping to find a matching value in the rcParameters to change to option globally like:

plt.rcParams['lines.alpha'] = 0.6

Could this be implemented so it can be set in a style sheet and the code stays short and readable?

Activity

  1. added this to the milestone on Sep 24, 2015
  2. tacaswell commented on Sep 24, 2015

    @tacaswell
    Member

    You can do this by setting a the color/prop cycle to use colors which have an alpha component.

    The traitlet overhaul will take care of this as well

  3. Jul3k commented on Sep 24, 2015

    @Jul3k
    Author

    Can i set the "axes.color_cycle" values in hex natation as e.g.:
    "#ff0000b4"
    Because when i try to do so i receive an exception of "invalid hex color string"

  4. WeatherGod commented on Sep 24, 2015

    @WeatherGod
    Member

    take out the pound sign because our rcfile reader is really naive and
    always treats that as a comment symbol.

    Also note that color_cycle is deprecated in the upcoming 1.5 release,
    replaced with the more versatile "prop_cycle" mechanism. However, it will
    likely be awhile before we actually remove "color_cycle".

    On Thu, Sep 24, 2015 at 9:30 AM, Jul3k [email protected] wrote:

    Can i set the "axes.color_cycle" values in hex natation as e.g.:
    "#ff0000b4"
    Because when i try to do so i receive an exception of "invalid hex color
    string"

    —
    Reply to this email directly or view it on GitHub
    #5132 (comment)
    .

  5. Jul3k commented on Sep 24, 2015

    @Jul3k
    Author

    Without the pound sign i get a "to_rgb: Invalid rgb arg "ff0000b4"" error.
    The exact call is:

    plt.rcParams['axes.color_cycle'][0] = "ff0000b4"
    

    matplotlib version 1.4.3

    Without the alpha content ("b4") it works fine. It seams to me that the parser doese not accept 4 Byte hex color codes with alpha content. I could work around this by using the to_rgba builtin method

    My workaround looks as follows:

    to_rgba = matplotlib.colors.ColorConverter().to_rgba
    for i, c in enumerate(plt.rcParams['axes.color_cycle']):
        plt.rcParams['axes.color_cycle'][i] = to_rgba(c,0.2)
    
  6. WeatherGod commented on Sep 24, 2015

    @WeatherGod
    Member

    Ah, indeed, our hex color spec parser is just rgb, not rgb[a]. Sounds like
    an easy feature to implement, though...

    On Thu, Sep 24, 2015 at 10:21 AM, Jul3k [email protected] wrote:

    Without the pound sign i get a "to_rgb: Invalid rgb arg "ff0000b4"" error.
    The exact call is:
    plt.rcParams['axes.color_cycle'][0] = "ff0000b4"

    matplotlib version 1.4.3

    Without the alpha content ("b4") it works fine. It seams to me that the
    parser doese not accept 4 Byte hex color codes with alpha content. I could
    work around this by using the to_rgba builtin method

    My working code looks as follows:
    to_rgba = matplotlib.colors.ColorConverter().to_rgba
    for i, c in enumerate(plt.rcParams['axes.color_cycle']):
    plt.rcParams['axes.color_cycle'][i] = to_rgba(c,0.2)

    —
    Reply to this email directly or view it on GitHub
    #5132 (comment)
    .

  7. tacaswell commented on Sep 24, 2015

    @tacaswell
    Member

    Pretty sure rcparams will take a 4-tuple an input

  8. Jul3k commented on Sep 24, 2015

    @Jul3k
    Author

    Yes rcparams takes a 4-tuple as an input. As @WeatherGod stated the problem was related to the fact that the color parser only checks for 3-Byte (#FFFFFF) color strings and does not understand 4-Byte (#FFFFFFAA) color strings with alpha channel. So the way i presented the color information was not suiteable to the parser

  9. tacaswell commented on Sep 24, 2015

    @tacaswell
    Member

    @Jul3k Can you make a new issue to upgrade out hex parser to deal with RGBA hex codes?

  10. Jul3k commented on Sep 24, 2015

    @Jul3k
    Author

    I opend a new issue relating to the color spec parser

  11. WeatherGod commented on May 19, 2016

    @WeatherGod
    Member

    I think this is closed by #6382, which updated the parser to allow 4-byte hex strings. Re-open this issue if that isn't sufficient.

  12. JoElfner commented on Aug 6, 2020

    @JoElfner

    I frequently need alpha independently of the color cycler. Most importantly I want different plots to have different default alphas, f.i. alpha=.5 for my scatter plots, while not using alpha for my line plots. Thus, as far as I can see, setting alpha to the color cycle is not working. Any change this is going to be implemented in the rcParams?

  13. timhoffm commented on Aug 6, 2020

    @timhoffm
    Member

    This is too specialized for rcParams, in particular if you want to have different values for scatter plots and line plots.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions