Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8ac194e

Browse files
authored
Merge pull request #1 from mdboom/min-exponent
Fix text alignment
2 parents a6b5299 + 50fe54e commit 8ac194e

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

lib/matplotlib/axis.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,9 @@ def get_tick_padding(self):
10981098
return max(values)
10991099
return 0.0
11001100

1101+
def _adjust_ticks(self, renderer, ticks_to_draw):
1102+
pass
1103+
11011104
@allow_rasterization
11021105
def draw(self, renderer, *args, **kwargs):
11031106
'Draw the axis lines, grid lines, tick lines and labels'
@@ -1110,6 +1113,8 @@ def draw(self, renderer, *args, **kwargs):
11101113
ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
11111114
renderer)
11121115

1116+
self._adjust_ticks(renderer, ticks_to_draw)
1117+
11131118
for tick in ticks_to_draw:
11141119
tick.draw(renderer)
11151120

@@ -1680,6 +1685,20 @@ class XAxis(Axis):
16801685
__name__ = 'xaxis'
16811686
axis_name = 'x'
16821687

1688+
def _adjust_ticks(self, renderer, ticks_to_draw):
1689+
heights = []
1690+
for tick in ticks_to_draw:
1691+
label1 = tick.label1
1692+
text = label1.get_text()
1693+
is_math = label1.is_math_text(text)
1694+
_, h, _ = renderer.get_text_width_height_descent(
1695+
text, label1.get_fontproperties(), is_math)
1696+
heights.append(h)
1697+
max_height = max(heights)
1698+
1699+
for tick, h in zip(ticks_to_draw, heights):
1700+
tick.label1.set_offset((0, max_height - h))
1701+
16831702
def contains(self, mouseevent):
16841703
"""Test whether the mouse event occured in the x axis.
16851704
"""

lib/matplotlib/text.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ def __init__(self,
233233
linespacing = 1.2 # Maybe use rcParam later.
234234
self._linespacing = linespacing
235235
self.set_rotation_mode(rotation_mode)
236+
self._offset = (0, 0)
236237
self.update(kwargs)
237238

238239
def update(self, kwargs):
@@ -773,12 +774,13 @@ def draw(self, renderer):
773774
textobj._set_gc_clip(gc)
774775

775776
angle = textobj.get_rotation()
777+
ox, oy = self._offset
776778

777779
for line, wh, x, y in info:
778780

779781
mtext = textobj if len(info) == 1 else None
780-
x = x + posx
781-
y = y + posy
782+
x = x + posx + ox
783+
y = y + posy - oy
782784
if renderer.flipy():
783785
y = canvash - y
784786
clean_line, ismath = textobj.is_math_text(line)
@@ -803,6 +805,13 @@ def draw(self, renderer):
803805
renderer.close_group('text')
804806
self.stale = False
805807

808+
def set_offset(self, offset):
809+
"""
810+
Set extra offset (in pixel space) to be applied immediately
811+
before drawing the text.
812+
"""
813+
self._offset = offset
814+
806815
def get_color(self):
807816
"Return the color of the text"
808817
return self._color

0 commit comments

Comments
 (0)