2

Please refer to the example below. Two subplots are added, and to each a Line2D plot is inserted. I then change the Line2D's axes in the second subplot to be the first subplot. Judging by the get_geometry output this is successful. However in the actual figure the two Line2D plots are still in their original subplots.

What am I missing here? How can I refresh the figure to reflect the axes change?

Obviously this is a fairly moronic example, the real application is more of a dynamic nature.

Script:

import matplotlib.pyplot as plt

fig = plt.figure()  
ax_1 = fig.add_subplot(2,1,1)
ax_2 = fig.add_subplot(2,1,2)
ax_1.plot([0,1,2],[0,1,2])
ax_2.plot([0,1,2],[2,1,0])

print 'before'
for line in ax_1.get_lines():
    print line.get_ydata()
    print line.get_axes().get_geometry()
    print id(line.get_axes())

for line in ax_2.get_lines():
    print line.get_ydata()
    print line.get_axes().get_geometry()
    print id(line.get_axes())

f = ax_2.get_lines()[0]
f.set_axes(ax_1)

print 'after'
for line in ax_1.get_lines():
    print line.get_ydata()
    print line.get_axes().get_geometry()
    print id(line.get_axes())

for line in ax_2.get_lines():
    print line.get_ydata()
    print line.get_axes().get_geometry()
    print id(line.get_axes())

plt.show()

Output:

before
[0 1 2]
(2, 1, 1)
4330504912
[2 1 0]
(2, 1, 2)
4336262288
after
[0 1 2]
(2, 1, 1)
4330504912
[2 1 0]
(2, 1, 1)
4330504912

Figure output: Figure

c00kiemonster
  • 22,241
  • 34
  • 95
  • 133
  • What are you trying to achieve? maybe there is a better method than drawing a line in one graph and "moving it" to another one? – Eric O. Lebigot Aug 19 '11 at 12:18
  • Like I said in the original post, the example above is silly. The real application is a gui charting application that is a little bit involved than the example. Regardless of the application though, I don't see the point of having a `set_axes` function if it doesn't actually change anything. – c00kiemonster Aug 19 '11 at 12:47
  • 1
    I think it's `axes.add_line()` that you want, but I can't seem to get it to work yet. So far I've created a new line, used `update_from()` to copy `f` into this new line, and then I've added it to the other axes. This still doesn't work, but it might for your application. I don't think it works for me because I don't think the splines used to make the line are updated for the new axes, but I'm looking into this.... – Yann Aug 19 '11 at 17:07
  • Currently I am copying all the data (xydata) and metadata (color, linewidth etc) from the line in the old axes. I then delete the line in the old axes, and I plot the new one in the new axes (with the line's extracted data and metadata). I have always done it like this. Then just the other day I saw `set_axes` in the docs, but I can't get it to work (as I assume it is meant to work). Perhaps I have misunderstood the axes mechanics in matplotlib, but as far as I can see I think it's counterintuitive behavior. – c00kiemonster Aug 19 '11 at 23:29

2 Answers2

0

I believe that one problem is that your first axes (ax_1) do not have the line that you want to add (f) in their list of lines (ax_1.lines).

You can "copy" the line from the second plot to the first one with

f = ax_2.lines.pop()  # Removes the line from the second plot
ax_1.plot(*f.get_data())  # Draws a new line in the first plot, but with the coordinates of the second line

(with this method, there is obviously no need to do f.set_axes(ax_1)). Other arguments to plot() could be used in order to also copy the color, etc., I guess.

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
  • Well, that's just another symptom of the same problem, that is, that the axes ax_2's line, f, hasn't been changed properly. After the axes change both Line2D objects point to the same axes object. – c00kiemonster Aug 19 '11 at 06:02
  • @c00kiemonster: Could you expand on that? I would not expect `f` to change on the graph as long as it is part of `ax_2.lines`. If you want to make it disappear, you should probably remove it from `ax_2.lines`. – Eric O. Lebigot Aug 19 '11 at 06:10
  • But it shouldn't be a part of `ax_2.lines`, I changed its axes to `ax_1`. Unless I have completely misunderstood matplotlib, shouldn't both lines be shown in the same axes (the upper one) if they share the same axes? – c00kiemonster Aug 19 '11 at 06:22
  • The documentation indicates that the axis of the *line* is changed (http://matplotlib.sourceforge.net/api/artist_api.html#matplotlib.lines.Line2D.set_axes); it does not say that the axis itself is modified. This is what you observe. I'm not sure what the typical use of `set_axis()` is, though, in these circumstances (line `f` remains contained in `ax_2.lines`, but indicates that its axis is `ax_1`, after your `set_axis()`: this seems contradictory…); so, I starred your question. :) – Eric O. Lebigot Aug 19 '11 at 06:39
  • Yes I am a bit confused myself, not exactly intuitive behavior. Hopefully there is some matplotlib guru out there who can explain. – c00kiemonster Aug 19 '11 at 06:44
0

Later on I posted the same question here, and this was the response.

TL,DR It's not supported.

c00kiemonster
  • 22,241
  • 34
  • 95
  • 133