Skip to content

variable alpha in line collections #3643

Closed

Description

@acadien

A line collection can have variable line width, face color, edge color, antialiasing, and offests but the alpha is fixed. It seems odd that so much variation is possible except when it comes to transparency. This is a headache to deal with when plotting a large number of line segments, would it be possible to make the alpha variable accept a list instead of a single float?

Activity

  1. tacaswell commented on Oct 14, 2014

    @tacaswell
    Member

    You can achieve this effect by passing RGBA tuples for the colors.

  2. added this to the milestone on Oct 14, 2014
  3. acadien commented on Oct 14, 2014

    @acadien
    Author

    I don't know why I didn't think of this, thanks for pointing it out. Regardless it would be nice to have the alpha interface when passing in a color map and array.

  4. tacaswell commented on Oct 14, 2014

    @tacaswell
    Member

    One difficulty with that has to do with the underlying mechanisms of the AGG rastering. As I understand it (and I am not super familiar with that bit of code so I could be wrong), the alpha kwargs is used when the whole artist is composited into the figure and is implemented way up the class stack. Changing how this is handled could turn into a major re-write.

  5. acadien commented on Oct 14, 2014

    @acadien
    Author

    Just a thought but a switch could be added somewhere in the class chain that catches if alpha is an array and constructs the corresponding rgba values array from the cmap.

  6. tacaswell commented on Oct 14, 2014

    @tacaswell
    Member

    @acadien That does sound reasonable, I suspect the correct place is in Collection. I don't have the bandwidth to look at that right now, but if you want to that would be great!

  7. modified the milestones: , needs sorting on Oct 6, 2022
  8. github-actions commented on Oct 9, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  9. added
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on Nov 8, 2023
  10. added
    keepItems to be ignored by the “Stale” Github Action
    and removed
    status: inactiveMarked by the “Stale” Github Action
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on Nov 8, 2023
  11. story645 commented on Nov 8, 2023

    @story645
    Member

    This is frequently asked for and we allow list alphas in other places

  12. greglucas commented on Nov 8, 2023

    @greglucas
    Contributor

    I'm pretty sure this works now. Adding alpha=np.linspace(0.1, 1, N) to the line collection example: https://matplotlib.org/stable/gallery/shapes_and_collections/line_collection.html shows a blended alpha.
    Figure_1

    import matplotlib.pyplot as plt
    import numpy as np
    
    from matplotlib.collections import LineCollection
    
    N = 50
    x = np.arange(N)
    ys = [x + i for i in x]  # Many sets of y to plot vs. x
    segs = [np.column_stack([x, y]) for y in ys]
    
    fig, ax = plt.subplots()
    ax.set_xlim(np.min(x), np.max(x))
    ax.set_ylim(np.min(ys), np.max(ys))
    
    line_segments = LineCollection(segs, array=x,
                                   linewidths=(0.5, 1, 1.5, 2),
                                   linestyles='solid',
                                   alpha=np.linspace(0.1, 1, N))
    ax.add_collection(line_segments)
    axcb = fig.colorbar(line_segments)
    axcb.set_label('Line Number')
    ax.set_title('Line Collection with mapped colors')
    plt.sci(line_segments)  # This allows interactive changing of the colormap.
    plt.show()
  13. modified the milestones: future releases, v3.5.0 on Jul 6, 2024
  14. removed
    keepItems to be ignored by the “Stale” Github Action
    on Jul 6, 2024
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