8000 Merge pull request #22940 from andrew-fennell/rainbow-example · matplotlib/matplotlib@4db5321 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4db5321

Browse files
authored
Merge pull request #22940 from andrew-fennell/rainbow-example
Fixed dpi bug in rainbow text example
2 parents cb28f3a + c4fadfc commit 4db5321

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

examples/text_labels_and_annotations/rainbow_text.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"""
2525

2626
import matplotlib.pyplot as plt
27-
from matplotlib.transforms import Affine2D
27+
from matplotlib.transforms import Affine2D, offset_copy
2828

2929

3030
def rainbow_text(x, y, strings, colors, orientation='horizontal',
@@ -51,7 +51,8 @@ def rainbow_text(x, y, strings, colors, orientation='horizontal',
5151
if ax is None:
5252
ax = plt.gca()
5353
t = ax.transData
54-
canvas = ax.figure.canvas
54+
fig = ax.figure
55+
canvas = fig.canvas
5556

5657
assert orientation in ['horizontal', 'vertical']
5758
if orientation == 'vertical':
@@ -63,10 +64,16 @@ def rainbow_text(x, y, strings, colors, orientation='horizontal',
6364
# Need to draw to update the text position.
6465
text.draw(canvas.get_renderer())
6566
ex = text.get_window_extent()
67+
# Convert window extent from pixels to inches
68+
# to avoid issues displaying at different dpi
69+
ex = fig.dpi_scale_trans.inverted().transform_bbox(ex)
70+
6671
if orientation == 'horizontal':
67-
t = text.get_transform() + Affine2D().translate(ex.width, 0)
72+
t = text.get_transform() + \
73+
offset_copy(Affine2D(), fig=fig, x=ex.width, y=0)
6874
else:
69-
t = text.get_transform() + Affine2D().translate(0, ex.height)
75+
t = text.get_transform() + \
76+
offset_copy(Affine2D(), fig=fig, x=0, y=ex.height)
7077

7178

7279
words = "all unicorns poop rainbows ! ! !".split()

0 commit comments

Comments
 (0)
0