Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 948
Arcturus B Avatar asked Oct 26 '25 21:10

Arcturus B


1 Answers

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

like image 100
Sheldore Avatar answered Oct 28 '25 09:10

Sheldore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!