8000 Set figure width and height with set_size_inches by jhamrick · Pull Request #4677 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Set figure width and height with set_size_inches #4677

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

Merged
merged 3 commits into from
Jul 15, 2015

Conversation

jhamrick
Copy link
Contributor

Fixes #4673

This modifies set_figwidth and set_figheight so that they call set_size_inches and pass along any additional kwargs (such as forward). It also adds some very basic tests for calling all three methods.

@efiring
Copy link
Member
efiring commented Jul 13, 2015

The use of **kwargs in set_size_inches seems odd; blame shows that it dates back at least to 2006, but there is only one kwarg available, and it is used only in figure.py itself. (Only one example uses set_size_inches, and it does not use the kwarg.) Rather than propagate this into set_figheight etc., maybe it would be better to start using the single explicit kwarg.

I also wonder why the default of the "forward" kwarg is False; why would one want to use set_size_inches and not have the manager enlarge the window accordingly? Maybe it is just a question of when the action occurs.
@tacaswell, @mdboom, any thoughts on these questions?

@tacaswell
Copy link
Member

I am all for dropping kwargs when we can.

My guess on that default would be back compatibility and/or not picking up
an extra draw if the user is saving at a different size than they are
displaying on the screen?

That seems like a default we can change in 2.0.

On Mon, Jul 13, 2015, 1:36 AM Eric Firing notifications@github.com wrote:

The use of **kwargs in set_size_inches seems odd; blame shows that it
dates back at least to 2006, but there is only one kwarg available, and it
is used only in figure.py itself. (Only one example uses set_size_inches,
and it does not use the kwarg.) Rather than propagate this into
set_figheight etc., maybe it would be better to start using the single
explicit kwarg.

I also wonder why the default of the "forward" kwarg is False; why would
one want to use set_size_inches and not have the manager enlarge the
window accordingly? Maybe it is just a question of when the action occurs.
@tacaswell https://github.com/tacaswell, @mdboom
https://github.com/mdboom, any thoughts on these questions?


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

@WeatherGod
Copy link
Member

re: kwargs, remember that super-fied classes tends to prefer _args and
*_kwargs. Not taking a position one way or the other, though.

On Mon, Jul 13, 2015 at 9:11 AM, Thomas A Caswell notifications@github.com
wrote:

I am all for dropping kwargs w 8000 hen we can.

My guess on that default would be back compatibility and/or not picking up
an extra draw if the user is saving at a different size than they are
displaying on the screen?

That seems like a default we can change in 2.0.

On Mon, Jul 13, 2015, 1:36 AM Eric Firing notifications@github.com
wrote:

The use of **kwargs in set_size_inches seems odd; blame shows that it
dates back at least to 2006, but there is only one kwarg available, and
it
is used only in figure.py itself. (Only one example uses set_size_inches,
and it does not use the kwarg.) Rather than propagate this into
set_figheight etc., maybe it would be better to start using the single
explicit kwarg.

I also wonder why the default of the "forward" kwarg is False; why would
one want to use set_size_inches and not have the manager enlarge the
window accordingly? Maybe it is just a question of when the action
occurs.
@tacaswell https://github.com/tacaswell, @mdboom
https://github.com/mdboom, any thoughts on these questions?


Reply to this email directly or view it on GitHub
<
https://github.com/matplotlib/matplotlib/pull/4677#issuecomment-120829319>

.


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

@tacaswell
Copy link
Member

I think the way we should deal with *args, **kwargs and super is to avoid using them unless we need to.

The use case for the *args and **kwargs in super is for when you have the same methods on multiple branches of the inheritance tree (which is always the case with the dunder methods as they all go back to object) but getting overlaps of non-built in methods in really bad design.

@jhamrick
Copy link
Contributor Author

I also would think that having forward=True as the default would be more convenient and be less surprising to users. I can open another issue about the defaults change specifically.

I'm happy to make the change to just having a single explicit argument to set_size_inches (and set_figheight and set_figwidth as well).

@jhamrick
Copy link
Contributor Author

Ok, PR updated to remove *args and **kwargs.

w, h = args[0]
else:
w, h = args
# the width and height have been passed in as a tuple to the first
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the width...

Do we also want to add a comment that we don't recommend this? I remember from reading comments in the backend that we considered this bad practice (so I guess we may remove this at some point?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment here wouldn't help much. None of our code is using the tuple argument form. Ideally, set_width and set_height would not be needed at all; they could be handled in set_size_inches using keywords, with both w and h defaulted to None.
Does anything actually use set_width and/or set_height at all? I imagine that something must use them, or the issue of changing them to go through set_size_inches would not have arisen. Nevertheless, it seems like this is a place where we have the opportunity for some longer-term deprecation and code condensation to reign in our sprawling API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I just made it so it would be backwards compatible for how it works currently, but I agree it's kind of strange to support both the tuple and the arguments. I don't know if anything actually uses the tuple notation for this.

I personally use set_figwidth and set_figheight a lot, which is how this came up, but that's also partially because I didn't know about set_size_inches. I like the suggestion of having both width and height be keyword arguments in set_size_inches and allow setting either or both, and then deprecating set_figwidth and set_figheight. Is there a reason, though, for it being set_size_inches rather than just set_size?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set_xlim/set_ylim supports this notation, I think.

On Mon, Jul 13, 2015 at 3:55 PM, Jessica B. Hamrick <
notifications@github.com> wrote:

In lib/matplotlib/figure.py
#4677 (comment):

@@ -682,11 +682,10 @@ def set_size_inches(self, _args, *_kwargs):
matplotlib.Figure.get_size_inches
"""

  •    forward = kwargs.get('forward', False)
    
  •    if len(args) == 1:
    
  •        w, h = args[0]
    
  •    else:
    
  •        w, h = args
    
  •    # the width and height have been passed in as a tuple to the first
    

Yeah, I just made it so it would be backwards compatible for how it works
currently, but I agree it's kind of strange to support both the tuple and
the arguments. I don't know if anything actually uses the tuple notation
for this.

I personally use set_figwidth and set_figheight a lot, which is how this
came up, but that's also partially because I didn't know about
set_size_inches. I like the suggestion of having both width and height be
keyword arguments in set_size_inches and allow setting either or both,
and then deprecating set_figwidth and set_figheight. Is there a reason,
though, for it being set_size_inches rather than just set_size?


Reply to this email directly or view it on GitHub
https://github.com/matplotlib/matplotlib/pull/4677/files#r34504229.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@efiring Yup I agree internal code so not much point here, but what about on the lines above in the function definition? See FigureManagerTkAgg.resize for the comment I referred to, should we do something similar here? I have also just done a search for sfw and sfh and only these methods come up.

@jhamrick I think set_size sounds confusing as it contains no units, it inches, centimetres, pixels, etcetera. I guess we do it by inches because of dpi (dots per square inch), a rather odd unit, but used everywhere in graphics, I have never seen dpcm :(. Perhaps you should also take a look at

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, and it has its good points; but I'm not sure it's worth the added API complexity to support this form everywhere. set_xlim and set_ylim are higher level than set_size_inches, so it is less disruptive to simplify the latter than the former.
As for set_size_inches versus set_size: I suspect that like much in mpl, it is an accident of the way it evolved. Whoever put it in decided to make it very clear what the units are.

tacaswell added a commit that referenced this pull request Jul 15, 2015
MNT: Set figure width and height with set_size_inches
@tacaswell tacaswell merged commit 7285c4c into matplotlib:master Jul 15, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4059
Development

Successfully merging this pull request may close these issues.

5 participants
0