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

Skip to content

Commit 3d638b4

Browse files
mdboomzblz
authored andcommitted
Fix text alignment
1 parent dc61940 commit 3d638b4

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
@@ -1104,6 +1104,9 @@ def get_tick_padding(self):
11041104
return max(values)
11051105
return 0.0
11061106

1107+
def _adjust_ticks(self, renderer, ticks_to_draw):
1108+
pass
1109+
11071110
@allow_rasterization
11081111
def draw(self, renderer, *args, **kwargs):
11091112
'Draw the axis lines, grid lines, tick lines and labels'
@@ -1116,6 +1119,8 @@ def draw(self, renderer, *args, **kwargs):
11161119
ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
11171120
renderer)
11181121

1122+
self._adjust_ticks(renderer, ticks_to_draw)
1123+
11191124
for tick in ticks_to_draw:
11201125
tick.draw(renderer)
11211126

@@ -1686,6 +1691,20 @@ class XAxis(Axis):
16861691
__name__ = 'xaxis'
16871692
axis_name = 'x'
16881693

1694+
def _adjust_ticks(self, renderer, ticks_to_draw):
1695+
heights = []
1696+
for tick in ticks_to_draw:
1697+
label1 = tick.label1
1698+
text = label1.get_text()
1699+
is_math = label1.is_math_text(text)
1700+
_, h, _ = renderer.get_text_width_height_descent(
1701+
text, label1.get_fontproperties(), is_math)
1702+
heights.append(h)
1703+
max_height = max(heights)
1704+
1705+
for tick, h in zip(ticks_to_draw, heights):
1706+
tick.label1.set_offset((0, max_height - h))
1707+
16891708
def contains(self, mouseevent):
16901709
"""Test whether the mouse event occured in the x axis.
16911710
"""

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)