Skip to content

tight_labels #801

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/pylab_examples/demo_tight_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import matplotlib.pyplot as plt

for n in [1, 2, 3, 5]:
fig, axes_list = plt.subplots(n, n)
plt.tight_labels(0.5, 0.3)
plt.tight_layout()

plt.show()
8 changes: 8 additions & 0 deletions lib/matplotlib/axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8435,6 +8435,14 @@ def triplot(self, *args, **kwargs):
mtri.triplot(self, *args, **kwargs)
triplot.__doc__ = mtri.triplot.__doc__

def optimize_ticker_nbin(self,
max_label_fraction_x, max_label_fraction_y,
renderer):
if max_label_fraction_x:
self.xaxis.optimize_ticker_nbin(max_label_fraction_x, renderer)
if max_label_fraction_y:
self.yaxis.optimize_ticker_nbin(max_label_fraction_y, renderer)


from matplotlib.gridspec import GridSpec, SubplotSpec

Expand Down
67 changes: 67 additions & 0 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,40 @@ def axis_date(self, tz=None):
tz = pytz.timezone(tz)
self.update_units(datetime.datetime(2009,1,1,0,0,0,0,tz))

def optimize_ticker_nbin(self, max_label_fraction, renderer):
"""
update the _nbin attribute of the major ticker so it is the maximum integer
that the occupy fraction of labels are less than the *max_label_fraction*.
"""

if not self.get_visible(): return

if not 0. <= max_label_fraction <= 1.:
raise ValueError()

min_nbin = self.major.locator._nbins
Copy link
Member

Choose a reason for hiding this comment

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

Apropos of the comment below by @pelson regarding use of _nbins, I think this method needs to start out by checking whether MaxNLocator is being used; if it is not, perhaps the method should exit with a warning rather than raising an exception.

To avoid use of the private _nbins attribute, one can use the public set_params method.


while self._get_label_occupy_fraction(renderer) < max_label_fraction:
Copy link
Member

Choose a reason for hiding this comment

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

Do we want to put in a safeguard here from getting stuck in an infinite loop? Is that a likely occurrence?

self.major.locator._nbins *= 2
else:
max_nbin = self.major.locator._nbins

if min_nbin == max_nbin:
min_nbin=1

while True:
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 would be nice. I think this is using a bisection method to find an optimum solution, but it would be nice if I had some confidence that that was the case.

if max_nbin - min_nbin <= 1:
break
new_nbin = int(0.5*(min_nbin+max_nbin))
self.major.locator._nbins = new_nbin
if self._get_label_occupy_fraction(renderer) < max_label_fraction:
min_nbin = new_nbin
else:
max_nbin = new_nbin

self.major.locator._nbins = min_nbin
Copy link
Member

Choose a reason for hiding this comment

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

There is a lot of tight coupling here with the axis knowing about the locators private _nbins attribute. Can you think of a neater solution (short of adding set_nbin and get_nbin!).




class XAxis(Axis):
__name__ = 'xaxis'
Expand Down Expand Up @@ -1786,6 +1820,22 @@ def set_default_intervals(self):
self.axes.viewLim.intervalx = xmin, xmax


def _get_label_occupy_fraction(self, renderer):
"""
Return label_total_length / axis_length
"""
ticks_to_draw = self._update_ticks(renderer)
ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw, renderer)

if not ticklabelBoxes:
ticklabelBoxes = ticklabelBoxes2

label_total_length = sum([bb.width for bb in ticklabelBoxes])

axis_length = self.axes.bbox.width

return label_total_length / axis_length


class YAxis(Axis):
__name__ = 'yaxis'
Expand Down Expand Up @@ -2049,3 +2099,20 @@ def set_default_intervals(self):
self.axes.dataLim.intervaly = ymin, ymax
if not viewMutated:
self.axes.viewLim.intervaly = ymin, ymax


def _get_label_occupy_fraction(self, renderer):
"""
Return label_total_length / axis_length
"""
ticks_to_draw = self._update_ticks(renderer)
ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw, renderer)

if not ticklabelBoxes:
ticklabelBoxes = ticklabelBoxes2

label_total_length = sum([bb.height for bb in ticklabelBoxes])

axis_length = self.axes.bbox.height

return label_total_length / axis_length
12 changes: 12 additions & 0 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,18 @@ def tight_layout(self, renderer=None, pad=1.2, h_pad=None, w_pad=None):
self.subplots_adjust(**kwargs)


def tight_labels(self, renderer=None, label_fraction_x=0.5, label_fraction_y=0.3):
"""
"""

from tight_layout import get_renderer

if renderer is None:
renderer = get_renderer(self)

for ax in self.axes:
ax.optimize_ticker_nbin( label_fraction_x, label_fraction_y, renderer)


def figaspect(arg):
"""
Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,15 @@ def tight_layout(pad=1.2, h_pad=None, w_pad=None):
draw_if_interactive()


def tight_labels(label_fraction_x=0.5, label_fraction_y=0.3):
"""
"""

fig = gcf()
fig.tight_labels(None, label_fraction_x, label_fraction_y)
draw_if_interactive()



def box(on=None):
"""
Expand Down