Skip to content

Non-working 'set_visible' method for PatchCollection #6000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
galaunay opened this issue Feb 14, 2016 · 6 comments
Closed

Non-working 'set_visible' method for PatchCollection #6000

galaunay opened this issue Feb 14, 2016 · 6 comments

Comments

@galaunay
Copy link

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

@tacaswell
Copy link
Member

Can you provide a minimal (not) working example?

@tacaswell tacaswell added the status: needs clarification Issues that need more information to resolve. label Feb 14, 2016
@tacaswell tacaswell added this to the unassigned milestone Feb 14, 2016
@galaunay
Copy link
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.

@tacaswell tacaswell modified the milestones: 2.1 (next point release), unassigned Feb 15, 2016
@tacaswell
Copy link
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.

@tacaswell
Copy link
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).

@galaunay
Copy link
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

@QuLogic QuLogic removed the status: needs clarification Issues that need more information to resolve. label Jul 19, 2017
@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.2 (next next feature release) Oct 3, 2017
@QuLogic
Copy link
Member

QuLogic commented Aug 7, 2020

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

@QuLogic QuLogic closed this as completed Aug 7, 2020
@story645 story645 removed this from the future releases milestone Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants