3

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.

Alejandro
  • 3,263
  • 2
  • 22
  • 38

1 Answers1

1

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?)

Community
  • 1
  • 1
tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • Could you please be more specific? I know you can get the axis by get_axes, but I do not need it, I actually know the axis (axis1 and axis2 make the reference to them). Then, each axis has their own methods to add lines. So, my question is, why when you add the same line to one axis you can see it, but when I try to add it to both, it disappears. – Alejandro Aug 01 '13 at 15:42
  • @Alejandro See edit. `get_axes` was just the first example that I thought of to show that the internals are mixed. – tacaswell Aug 01 '13 at 16:15