Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is matplotlib.patches.FancyArrow not well behaved?

I found a rare behaviour in matplotlib when trying to plot arrows. If you make a figure as below:

import matplotlib.pyplot as plt
import matplotlib.patches as patches

arrow = patches.FancyArrow(0.,0.,0.4,0.6)

fig = plt.figure(1)
ax1  = fig.add_subplot(121)
ax2  = fig.add_subplot(122)

ax1.add_line(arrow)
plt.show()

you will see the arrow perfectly in the first axis. But when trying to add the same arrow on the second axis:

import matplotlib.pyplot as plt
import matplotlib.patches as patches

arrow = patches.FancyArrow(0.,0.,0.4,0.6)

fig = plt.figure(1)
ax1  = fig.add_subplot(121)
ax2  = fig.add_subplot(122)

ax1.add_line(arrow)
ax2.add_line(arrow)

plt.show()

you are going to note that the arrows are not plotted.

If we try to make the same figure, but now with different copies of the arrow object:

import matplotlib.pyplot as plt
import matplotlib.patches as patches

arrow1 = patches.FancyArrow(0.,0.,0.4,0.6)
arrow2 = patches.FancyArrow(0.,0.,0.4,0.6)

fig = plt.figure(1)
ax1  = fig.add_subplot(121)
ax2  = fig.add_subplot(122)

ax1.add_line(arrow1)
ax2.add_line(arrow2)

plt.show()

It is possible to see both arrows, one in each panel. So, it seems that there is a dependence between different methods and objects. Someone knows what is going on here? Is FancyArrow buggy? Thanks.

like image 775
Alejandro Avatar asked Feb 17 '26 05:02

Alejandro


1 Answers

They behave just fine, you are just using them wrong, you can't add the same artist object to more than one axes [in the future, this will be enforced by the library].

There are bits and pieces of the internals of the artist and the axes get mixed up when you add an artist to an axes. For example get_axes returns what axes the Artist is in, if you added it to multiple axes this would get tricky(ier) to keep track of.

This is related to why moving axes between figures is hard (After creating an array of matplotlib.figure.Figure, how do I draw them as subplots of One figure? and How do I include a matplotlib Figure object as subplot?)

like image 191
tacaswell Avatar answered Feb 19 '26 20:02

tacaswell



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!