Discussion:
[Matplotlib-users] Creating axes with fixed distance from figure edge
Thomas Robitaille
2015-09-09 21:01:22 UTC
Permalink
Hi everyone,

I am interested in creating axes in an interactive figure where the
distance from the spines of the axes to the figure edge are constant
in absolute terms.

To clarify what I mean, when using add_axes([0.1, 0.1, 0.8, 0.8]), the
spines of the axes are always located a distance from the edge of the
figure that is 10% of the size of the figure. However, in my case,
since the font size is constant, I want to be able to say that the
spines should always be e.g. 0.5" from the edge of the figure, which
would avoid wasting space when making the figure larger.

Is there a way to do this currently?

(I am aware of set_tight_layout which would result in something
similar, but this is not what I am after - I would like to be able to
specify the exact absolute distance from the figure edge)

Thanks!
Tom
Thomas Robitaille
2015-09-09 21:29:01 UTC
Permalink
Thanks Eric - unfortunately I need to be able to resize the figure
interactively and have the axes follow.

So an alternative that would be equally useful for me would be to
specify axes using add_subplot or add_axes but then essentially have
an option to say that the distance to the edge of the figure should be
preserved when resizing. In other words, I'm not too concerned about
whether I specify the original axes position in relative units or in
inches, but the important thing is that the distance to the edge of
the figure stays constant in absolute terms.

Is this something that would be easy to build as an Axes subclass?

Cheers,
Tom
Post by Thomas Robitaille
Hi everyone,
I am interested in creating axes in an interactive figure where the
distance from the spines of the axes to the figure edge are constant
in absolute terms.
To clarify what I mean, when using add_axes([0.1, 0.1, 0.8, 0.8]), the
spines of the axes are always located a distance from the edge of the
figure that is 10% of the size of the figure. However, in my case,
since the font size is constant, I want to be able to say that the
spines should always be e.g. 0.5" from the edge of the figure, which
would avoid wasting space when making the figure larger.
Is there a way to do this currently?
"""
Wrapper for Figure.add_axes in which *rect* is given in inches.
The translation to normalized coordinates is done immediately
based on the present figsize.
*rect* is left, bottom, width, height in inches
*kw* are passed to Figure.add_axes
"""
fw = fig.get_figwidth()
fh = fig.get_figheight()
l, b, w, h = rect
relrect = [l / fw, b / fh, w / fw, h / fh]
ax = fig.add_axes(relrect, **kw)
return ax
Note, however, that this works correctly only if you don't change figsize
after calling it, so maybe it is not what you are looking for.
Eric
Post by Thomas Robitaille
(I am aware of set_tight_layout which would result in something
similar, but this is not what I am after - I would like to be able to
specify the exact absolute distance from the figure edge)
Thanks!
Tom
------------------------------------------------------------------------------
Monitor Your Dynamic Infrastructure at Any Scale With Datadog!
Get real-time metrics from all of your servers, apps and tools
in one place.
SourceForge users - Click here to start your Free Trial of Datadog now!
http://pubads.g.doubleclick.net/gampad/clk?id=241902991&iu=/4140
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Thomas Robitaille
2015-09-09 22:01:55 UTC
Permalink
I managed to write an Axes sub-class to do this:

https://gist.github.com/astrofrog/8d579ea83e578a9cdb99

Try running this then resize the figure and the margin between axes
and figure edge will stay constant.

Is this something that would be useful to have in Matplotlib itself? I
could foresee something like:

fig.add_axes([0.1, 0.1, 0.8, 0.8], preserve_absolute_margins=True)

If this would be useful, I can open a pull request.

Cheers,
Tom

On 9 September 2015 at 23:29, Thomas Robitaille
Post by Thomas Robitaille
Thanks Eric - unfortunately I need to be able to resize the figure
interactively and have the axes follow.
So an alternative that would be equally useful for me would be to
specify axes using add_subplot or add_axes but then essentially have
an option to say that the distance to the edge of the figure should be
preserved when resizing. In other words, I'm not too concerned about
whether I specify the original axes position in relative units or in
inches, but the important thing is that the distance to the edge of
the figure stays constant in absolute terms.
Is this something that would be easy to build as an Axes subclass?
Cheers,
Tom
Post by Thomas Robitaille
Hi everyone,
I am interested in creating axes in an interactive figure where the
distance from the spines of the axes to the figure edge are constant
in absolute terms.
To clarify what I mean, when using add_axes([0.1, 0.1, 0.8, 0.8]), the
spines of the axes are always located a distance from the edge of the
figure that is 10% of the size of the figure. However, in my case,
since the font size is constant, I want to be able to say that the
spines should always be e.g. 0.5" from the edge of the figure, which
would avoid wasting space when making the figure larger.
Is there a way to do this currently?
"""
Wrapper for Figure.add_axes in which *rect* is given in inches.
The translation to normalized coordinates is done immediately
based on the present figsize.
*rect* is left, bottom, width, height in inches
*kw* are passed to Figure.add_axes
"""
fw = fig.get_figwidth()
fh = fig.get_figheight()
l, b, w, h = rect
relrect = [l / fw, b / fh, w / fw, h / fh]
ax = fig.add_axes(relrect, **kw)
return ax
Note, however, that this works correctly only if you don't change figsize
after calling it, so maybe it is not what you are looking for.
Eric
Post by Thomas Robitaille
(I am aware of set_tight_layout which would result in something
similar, but this is not what I am after - I would like to be able to
specify the exact absolute distance from the figure edge)
Thanks!
Tom
------------------------------------------------------------------------------
Monitor Your Dynamic Infrastructure at Any Scale With Datadog!
Get real-time metrics from all of your servers, apps and tools
in one place.
SourceForge users - Click here to start your Free Trial of Datadog now!
http://pubads.g.doubleclick.net/gampad/clk?id=241902991&iu=/4140
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Benjamin Root
2015-09-10 03:08:53 UTC
Permalink
What might be more generally useful is to make it easier to specify which
coordinate system you wish some spec to apply to. To be frank, I can never
keep the transform names straight, and it isn't possible to specify it at
all in some places.
Post by Thomas Robitaille
https://gist.github.com/astrofrog/8d579ea83e578a9cdb99
Try running this then resize the figure and the margin between axes
and figure edge will stay constant.
Is this something that would be useful to have in Matplotlib itself? I
fig.add_axes([0.1, 0.1, 0.8, 0.8], preserve_absolute_margins=True)
If this would be useful, I can open a pull request.
Cheers,
Tom
On 9 September 2015 at 23:29, Thomas Robitaille
Post by Thomas Robitaille
Thanks Eric - unfortunately I need to be able to resize the figure
interactively and have the axes follow.
So an alternative that would be equally useful for me would be to
specify axes using add_subplot or add_axes but then essentially have
an option to say that the distance to the edge of the figure should be
preserved when resizing. In other words, I'm not too concerned about
whether I specify the original axes position in relative units or in
inches, but the important thing is that the distance to the edge of
the figure stays constant in absolute terms.
Is this something that would be easy to build as an Axes subclass?
Cheers,
Tom
Post by Thomas Robitaille
Hi everyone,
I am interested in creating axes in an interactive figure where the
distance from the spines of the axes to the figure edge are constant
in absolute terms.
To clarify what I mean, when using add_axes([0.1, 0.1, 0.8, 0.8]), the
spines of the axes are always located a distance from the edge of the
figure that is 10% of the size of the figure. However, in my case,
since the font size is constant, I want to be able to say that the
spines should always be e.g. 0.5" from the edge of the figure, which
would avoid wasting space when making the figure larger.
Is there a way to do this currently?
"""
Wrapper for Figure.add_axes in which *rect* is given in inches.
The translation to normalized coordinates is done immediately
based on the present figsize.
*rect* is left, bottom, width, height in inches
*kw* are passed to Figure.add_axes
"""
fw = fig.get_figwidth()
fh = fig.get_figheight()
l, b, w, h = rect
relrect = [l / fw, b / fh, w / fw, h / fh]
ax = fig.add_axes(relrect, **kw)
return ax
Note, however, that this works correctly only if you don't change
figsize
Post by Thomas Robitaille
after calling it, so maybe it is not what you are looking for.
Eric
Post by Thomas Robitaille
(I am aware of set_tight_layout which would result in something
similar, but this is not what I am after - I would like to be able to
specify the exact absolute distance from the figure edge)
Thanks!
Tom
------------------------------------------------------------------------------
Post by Thomas Robitaille
Post by Thomas Robitaille
Monitor Your Dynamic Infrastructure at Any Scale With Datadog!
Get real-time metrics from all of your servers, apps and tools
in one place.
SourceForge users - Click here to start your Free Trial of Datadog now!
http://pubads.g.doubleclick.net/gampad/clk?id=241902991&iu=/4140
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Monitor Your Dynamic Infrastructure at Any Scale With Datadog!
Get real-time metrics from all of your servers, apps and tools
in one place.
SourceForge users - Click here to start your Free Trial of Datadog now!
http://pubads.g.doubleclick.net/gampad/clk?id=241902991&iu=/4140
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Loading...