Arcturus B
Arcturus B

Reputation: 5631

Cannot remove streamplot arrow heads from Matplotlib axes

How can I remove a streamplot from a Matplotlib plot without clearing everything (i.e. not using plt.cla() or plt.clf())?


plt.streamplot() returns a StreamplotSet (streams in the following example), which contains the stream lines (.lines) and arrow heads (.arrows).

Calling streams.lines.remove() removes the streamlines as expected.

However, I couldn’t find a way to remove the arrow heads: stream.arrows.remove() raises a NotImplementedError, and stream.arrows.set_visible(False) has no effect.

import matplotlib.pyplot as plt
import numpy as np

# Generate streamplot data
x = np.linspace(-5, 5, 10)
y = np.linspace(-5, 5, 10)
u, v = np.meshgrid(x, y)

# Create streamplot
streams = plt.streamplot(x, y, u, v)

# Remove streamplot
streams.lines.remove()  # Removes the stream lines
streams.arrows.set_visible(False)  # Does nothing
streams.arrows.remove()  # Raises NotImplementedError

The following figure illustrates the example. Left: streamplot, right: remaining arrow heads.

Streamplot example: full plot (left), and lines removed (right)


For context, I’m trying to add streamlines to an existing imshow animation (built with matplotlib.animation.FuncAnimation). In this setup, only the image data are updated at each frame, and I can’t clear and redraw the full plot.

Upvotes: 4

Views: 1572

Answers (1)

Sheldore
Sheldore

Reputation: 39072

This solution seems to work and is inspired from this answer. There are two ways:

  1. Remove the arrow patch
  2. Set the alpha parameter to 0

streams = plt.streamplot(x, y, u, v)

ax = plt.gca()

for art in ax.get_children():
    if not isinstance(art, matplotlib.patches.FancyArrowPatch):
        continue
    art.remove()        # Method 1
    # art.set_alpha(0)  # Method 2

enter image description here

Upvotes: 4

Related Questions