-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
tight_labels #801
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
while self._get_label_occupy_fraction(renderer) < max_label_fraction: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
|
||
|
||
class XAxis(Axis): | ||
__name__ = 'xaxis' | ||
|
@@ -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' | ||
|
@@ -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 |
There was a problem hiding this comment.
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.