Skip to content

Non-working 'set_visible' method for PatchCollection #6000

Closed

Description

@galaunay

matplotlib.collections.PatchCollection.set_visible doesn't seem to work.
I was unable to set the arrows of a pyplot.streamplot invisible.

Activity

  1. tacaswell commented on Feb 14, 2016

    @tacaswell
    Member

    Can you provide a minimal (not) working example?

  2. added this to the milestone on Feb 14, 2016
  3. galaunay commented on Feb 14, 2016

    @galaunay
    Author

    Sure,

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Data
    x = np.linspace(-10, 10, 10)
    y = np.linspace(-10, 10, 10)
    X, Y = np.meshgrid(x, y)
    U = X
    V = X**2
    
    # basic streamline plot
    plt.figure()
    sp1 = plt.streamplot(x, y, U, V)
    
    # Attempt to hide lines and arrows from streamline plot
    plt.figure()
    sp2 = plt.streamplot(x, y, U, V)
    sp2.lines.set_visible(False)
    sp2.arrows.set_visible(False)
    
    plt.show()
    

    I expected the last plot to be empty, don't know if it is the wanted behaviour though.

  4. modified the milestones: , on Feb 15, 2016
  5. tacaswell commented on Feb 15, 2016

    @tacaswell
    Member

    Interesting, looking at the code for streamplot, the arrows are each added as individual patches, then put into the collection and the collection is returned.

  6. tacaswell commented on Feb 15, 2016

    @tacaswell
    Member

    And the obvious thing of not adding the arrows as they are created and adding the PatchCollection later results in the arrows not being drawn at all (not sure why yet).

  7. galaunay commented on Feb 29, 2016

    @galaunay
    Author

    I tried adding a simple arrow through PatchCollection without success.
    If I'm using PatchCollection right it could explain why Axes.add_patches is necessary for streamplot() to show arrows...

    I'm also confused about the use of transform in that particular case. In streamplot(), transform argument is specified at FancyArrowPatch creation and at PatchCollection creation.
    Is it the right way of doing it ? because even a classic patch (the ellipse in the linked script) is not displayed right in that case.

    import matplotlib.pyplot as plt
    from matplotlib import patches, lines
    from matplotlib import collections
    
    
    def draw_test(ax, trans1, trans2, title):
        if trans1:
            transform1 = ax.transData
        else:
            transform1 = None
        if trans2:
            transform2 = ax.transData
        else:
            transform2 = None
        # Add arrow
        arrow_tail = [0, 0]
        arrow_head = [1, 0]
        p = patches.FancyArrowPatch(arrow_tail, arrow_head, arrowstyle='-|>',
                                    mutation_scale=40, transform=transform1,
                                    margins=False)
        ac = collections.PatchCollection([p], margins=False, transform=transform2)
        ax.add_collection(ac)
        # Add another kind of patch
        e = patches.Ellipse([0, 0], .2, .5, .5, transform=transform1)
        ec = collections.PatchCollection([e], margins=False, transform=transform2)
        ax.add_collection(ec)
        # Add lines
        streamline = [[0, -0.5], [0.5, 0], [1, 0.5]]
        lc = collections.LineCollection([streamline], margins=False,
                                         transform=transform2)
        ax.add_collection(lc)
        # 
        ax.set_title(title)
        ax.set_xlim(-1, 1)
        ax.set_ylim(-1, 1)
    
    
    # Create subplot
    fig, axs = plt.subplots(3, 2, figsize=(8, 8))
    axs[0, 1].remove()
    
    # Add as patches
    ax1 = axs[0, 0]
    transform1 = ax1.transData
    arrow_tail = [0, 0]
    arrow_head = [1, 0]
    p1 = patches.FancyArrowPatch(arrow_tail, arrow_head, arrowstyle='-|>',
                                 mutation_scale=40, transform=transform1,
                                 margins=False)
    ax1.add_patch(p1)
    e1 = patches.Ellipse([0, 0], .2, .5, .5)
    ax1.add_patch(e1)
    streamline1 = [[0, -0.5], [0.5, 0], [1, 0.5]]
    ax1.add_line(lines.Line2D(*zip(*streamline1)))
    ax1.set_title("Added as patches")
    ax1.set_xlim(-1, 1)
    ax1.set_ylim(-1, 1)
    
    # Add with various transform
    draw_test(axs[1, 0], trans1=False, trans2=False, title="No transform used")
    draw_test(axs[1, 1], trans1=True, trans2=False, title="Transform on patches")
    draw_test(axs[2, 0], trans1=False, trans2=True, title="Transform on PatchCollection")
    draw_test(axs[2, 1], trans1=True, trans2=True, title="Transform on both")
    
    plt.tight_layout()
    plt.show()
    

    issue_6000

  8. modified the milestones: , on Oct 3, 2017
  9. QuLogic commented on Aug 7, 2020

    @QuLogic
    Member

    This is in essence a duplicate of #2341, namely that streamplot returns a useless PatchCollection because it can't draw FancyArrowPatchs.

  10. removed this from the future releases milestone on Oct 6, 2022
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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions