1

I'm getting odd behaviour in Matplotlib when I try to reuse a Line2D artist from one figure in another: in the second figure the artist is offset.

Am I doing something wrong or is this a bug? And if so is there a quick way to avert it?

I've boiled the problem down to the following code:

import matplotlib.pyplot as pyplt
import numpy as np

xs=np.arange(10)
ys=np.arange(10)
line=pyplt.Line2D(xs,ys,color="red",linewidth=1)

print "IN THIS FIRST FIGURE, line LOOKS JUST FINE"
figure1 = pyplt.figure()
axes1 = figure1.add_subplot(111)
axes1.add_artist(line)
pyplt.show()
pyplt.close('all')

print "BUT WHEN I REUSE line IN ANOTHER FIGURE, IT IS OFFSET"
figure2 = pyplt.figure()
axes2 = figure2.add_subplot(111)
axes2.add_artist(line)
pyplt.show()
pyplt.close('all')

Image at https://i.stack.imgur.com/ZERTP.jpg but I lack the reputation to post it.

  • OK thanks. I was running version 1.3 of Matplotlib. I see in the version notes that 1.3.1 includes a fix: "Drawing lines from one subplot to another now works" so perhaps this addresses this. I will update and check. – user3401545 Jul 01 '14 at 10:09

1 Answers1

1

When you add an artist to an axes it picks up a good deal of detail from the axes (such as the transform stack used to get from data-space -> scree-space).

As such you can not re-use artists in multiple figures/axes (you can in principle move them, but it would involve touching the guts). Just plot it a second time.

tacaswell
  • 84,579
  • 22
  • 210
  • 199