Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

figure.add_subplot not creating new axes after change_geometry() #4786

Open
AeroEngy opened this issue Jul 25, 2015 · 12 comments

Comments

Projects
None yet
6 participants
@AeroEngy
Copy link

commented Jul 25, 2015

I have a PyQT4 GUI w/ embedded matplotlib that lets a user Add/Remove/Resize subplots and plot various items to them. Occasionally after a certain sequence of add/removes a line would be plotted on the wrong subplot and the one specified would be missing. A code sample is below to recreate the issue. Note in my real GUI I don't use pyplot and instead import Figure and FigureCanvasQTAgg but the behavior is the same as the sample below:

import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 10, 0.1)
y1 = 0.05 * x**2
y2 = -1 *y1
figure = plt.figure()
ax1 = figure.add_subplot(2,1,2,label='sub2')
ax1.plot(x, y1, 'g-')
plt.show()
# That was a mistake ... lets move it to sub1 and relabel.
ax1.change_geometry(2,1,1)
ax1.set_label('sub1')
#Now lets add subplot 2 for real this time and plot something else
ax2 = figure.add_subplot(2,1,2,label='sub2')
ax2.plot(x, y2, 'b-')
plt.draw()

figure_1

No subplot 2 and both lines plotted on subplot 1.

This seems to be because the second call to add_subplot had the same args as the first call. So it returned the existing object. However, I would have expected after the call to change_geometry and a re-label this wouldn't be the case.

Also this looks like the same issue as #429 that was closed along time ago just a different use case. I added a comment there but went ahead and create this new issue since that one has been closed for so long.

@tacaswell

This comment has been minimized.

Copy link
Member

commented Jul 25, 2015

huh, did not know that function existed and it has a rather ominous comment above it https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/axes/_subplots.py#L94

That function needs to go back up into the figure object and update the key the axes_stack that the figure holds. See https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/figure.py#L916

@tacaswell tacaswell added this to the proposed next point release milestone Jul 25, 2015

@AeroEngy

This comment has been minimized.

Copy link
Author

commented Jul 25, 2015

Thanks! What about the call to set_label or really anything that would modify one of the args/kwargs that make up the key? Should it/they also update they key? I am by no means an expert and just muddle through but it seems problematic since there is a wide combination of things that could be in the key depending on how add_subplot was called.

However, the label is specifically called out in the documentation for add_axes as being exposed for tracking the key so maybe it at least should also update the key it if possible.

Personally the behavior of tracking the key by args/kwargs and returning the axes if present seems unnecessary. To me add_subplot or add_axes should do exactly that always add an axes regardless. The was mentioned by efiring in a comment on the original Issue (4 years ago)

@WeatherGod

This comment has been minimized.

Copy link
Member

commented Jul 27, 2015

This is something we can fully blame Matlab for. In matlab, you dont work
from axes objects, so the subplot function was treated as the "getter" that
would create if needed.

The good news is that we have moved on from those functions and recommend
using things like add_subplots(), which doesn't expose that interface.
On Jul 25, 2015 10:47 AM, "AeroEngy" notifications@github.com wrote:

Thanks! What about the call to set_label or really anything that would
modify one of the args/kwargs that make up the key? Should it/they also
update they key? I am by no means an expert and just muddle through but it
seems problematic since there is a wide combination of things that could be
in the key depending on how add_subplot was called.

However, the label is specifically called out in the documentation for
add_axes
http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.add_axes
as being exposed for tracking the key so maybe it at least should also
update the key it if possible.

Personally the behavior of tracking the key by args/kwargs and returning
the axes if present seems unnecessary. To me add_subplot or add_axes
should do exactly that always add an axes regardless. The was mentioned by
efiring in a comment on the original Issue (4 years ago)


Reply to this email directly or view it on GitHub
#4786 (comment)
.

@AeroEngy

This comment has been minimized.

Copy link
Author

commented Jul 27, 2015

I'm a little confused by the last part of your comment. My example does use the add_subplot() method. This issue is present in that method because it is returning an existing object instead of just adding one.

@WeatherGod

This comment has been minimized.

Copy link
Member

commented Jul 27, 2015

Hmmm, indeed. I missed that detail. It is probably an inadvertent result of
implementing add_subplots() upon the same framework that add_subplot() is
built on. So, an important question is if that is something that users rely
on, or is it ok for us to fix this so that it never returns an existing
object?

On Mon, Jul 27, 2015 at 11:12 AM, AeroEngy notifications@github.com wrote:

I'm a little confused by the last part of your comment. My example uses
the add_subplots() metho. This issue is present in that method because it
is returning an existing object instead of just adding one.


Reply to this email directly or view it on GitHub
#4786 (comment)
.

@AeroEngy

This comment has been minimized.

Copy link
Author

commented Jul 27, 2015

Ummm.. Hold on I think I confused myself and you. My example uses add_subplot() no "s". I am not aware of an add_subplots() with an "s". Do you mean pyplot.subplots()... No "add_"

@WeatherGod

This comment has been minimized.

Copy link
Member

commented Jul 27, 2015

I mean Axes.add_subplots() (and by extension pyplot.subplots()). They are
newer, added a few years ago, and are the recommended way of creating a
grid of subplots now. The singular form is the older, Matlab-ish way of
creating a subplot at a time.

On Mon, Jul 27, 2015 at 12:24 PM, AeroEngy notifications@github.com wrote:

Ummm.. Hold on I think I confused myself and you. My example uses
add_subplot() no "s". I am not aware of an add_subplots() with an "s". Do
you mean pyplot.subplots()... No "add_"


Reply to this email directly or view it on GitHub
#4786 (comment)
.

@WeatherGod

This comment has been minimized.

Copy link
Member

commented Jul 27, 2015

gah... I keep doing this to myself... of course, not Axes.add_subplots()...
that would be silly...

On Mon, Jul 27, 2015 at 12:28 PM, Benjamin Root ben.v.root@gmail.com
wrote:

I mean Axes.add_subplots() (and by extension pyplot.subplots()). They are
newer, added a few years ago, and are the recommended way of creating a
grid of subplots now. The singular form is the older, Matlab-ish way of
creating a subplot at a time.

On Mon, Jul 27, 2015 at 12:24 PM, AeroEngy notifications@github.com
wrote:

Ummm.. Hold on I think I confused myself and you. My example uses
add_subplot() no "s". I am not aware of an add_subplots() with an "s". Do
you mean pyplot.subplots()... No "add_"


Reply to this email directly or view it on GitHub
#4786 (comment)
.

@AeroEngy

This comment has been minimized.

Copy link
Author

commented Jul 27, 2015

Yeah that confused me furthe. I can't really use subplots in my uses case since this part of a Gui that let's users add additional subplots 1 at a time. Example they have 2 current subplots with various lines on each. Then they want to plot something else on a third subplot.. Which doesn't exist yet. So I add one more with add_subplot then change_geometry on the existing 2.

@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.2 (next next feature release) Oct 3, 2017

@themightyoarfish

This comment has been minimized.

Copy link
Contributor

commented Aug 2, 2018

Is there any hope of having this fixed soonish?

@jklymak

This comment has been minimized.

Copy link
Contributor

commented Aug 2, 2018

I think this would require someone who needs this functionality to look into doing this properly. I think the core devs are relatively unanimous that the plt.subplot/fig.add_subplot interface is not preferred so making it even more complicated by making this work would be low priority

I think you would have more success just figuring how to do this properly with the basic axes and GridSpec objects. It shouldn’t be too convoluted. I think you just modify the axes subplotspec.

@fredrik-1

This comment has been minimized.

Copy link
Contributor

commented Aug 2, 2018

You can avoid the problem in the example above with using
ax2 = figure.add_subplot(2,1,2,label='sub3')
instead of
ax2 = figure.add_subplot(2,1,2,label='sub2')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.