Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 39301db

Browse filesBrowse files
anntzerImportanceOfBeingErnest
authored andcommitted
Remove unnecessary Legend._approx_text_height. (#16312)
It is only ever called with renderer=None, returning the fontsize directly. In fact the rest of the code assumes that the font size is in points (inch/72), not in pixels. This can be checked by setting the dpi to a large value (e.g. 500), forcing the renderer to be not None by adding `renderer = self.figure.canvas._cachedRenderer` and making sure that a cached renderer exists (via `fig.canvas.draw()`) before creating the legend -- such a patch results in a highly distorted legend layout, whereas just using fontsize is fine even at high dpi.
1 parent 69cd8bf commit 39301db
Copy full SHA for 39301db

File tree

Expand file treeCollapse file tree

1 file changed

+2
-12
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+2
-12
lines changed

‎lib/matplotlib/legend.py

Copy file name to clipboardExpand all lines: lib/matplotlib/legend.py
+2-12Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -610,16 +610,6 @@ def draw(self, renderer):
610610
renderer.close_group('legend')
611611
self.stale = False
612612

613-
def _approx_text_height(self, renderer=None):
614-
"""
615-
Return the approximate height of the text. This is used to place
616-
the legend handle.
617-
"""
618-
if renderer is None:
619-
return self._fontsize
620-
else:
621-
return renderer.points_to_pixels(self._fontsize)
622-
623613
# _default_handler_map defines the default mapping between plot
624614
# elements and the legend handlers.
625615

@@ -731,9 +721,9 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
731721

732722
# The approximate height and descent of text. These values are
733723
# only used for plotting the legend handle.
734-
descent = 0.35 * self._approx_text_height() * (self.handleheight - 0.7)
724+
descent = 0.35 * fontsize * (self.handleheight - 0.7)
735725
# 0.35 and 0.7 are just heuristic numbers and may need to be improved.
736-
height = self._approx_text_height() * self.handleheight - descent
726+
height = fontsize * self.handleheight - descent
737727
# each handle needs to be drawn inside a box of (x, y, w, h) =
738728
# (0, -descent, width, height). And their coordinates should
739729
# be given in the display coordinates.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.