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

Skip to content

Commit 8371fef

Browse files
committed
Fig dpi-changing-related bug where tick label padding was not updating
(Thanks Eric Firing for finding this). svn path=/trunk/matplotlib/; revision=4909
1 parent c593a75 commit 8371fef

9 files changed

Lines changed: 83 additions & 54 deletions

File tree

lib/matplotlib/axes.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -595,13 +595,13 @@ def get_xaxis_transform(self):
595595
"""
596596
return self._xaxis_transform
597597

598-
def get_xaxis_text1_transform(self, pad_pixels):
598+
def get_xaxis_text1_transform(self, pad_points):
599599
"""
600600
Get the transformation used for drawing x-axis labels, which
601-
will add the given number of pad_pixels between the axes and
602-
the label. The x-direction is in data coordinates and the
603-
y-direction is in axis coordinates. Returns a 3-tuple of the
604-
form:
601+
will add the given amount of padding (in points) between the
602+
axes and the label. The x-direction is in data coordinates
603+
and the y-direction is in axis coordinates. Returns a 3-tuple
604+
of the form:
605605
606606
(transform, valign, halign)
607607
@@ -612,14 +612,15 @@ def get_xaxis_text1_transform(self, pad_pixels):
612612
need to place axis elements in different locations.
613613
"""
614614
return (self._xaxis_transform +
615-
mtransforms.Affine2D().translate(0, -1 * pad_pixels),
615+
mtransforms.ScaledTranslation(0, -1 * pad_points / 72.0,
616+
self.figure.dpi_scale_trans),
616617
"top", "center")
617618

618-
def get_xaxis_text2_transform(self, pad_pixels):
619+
def get_xaxis_text2_transform(self, pad_points):
619620
"""
620621
Get the transformation used for drawing the secondary x-axis
621-
labels, which will add the given number of pad_pixels between
622-
the axes and the label. The x-direction is in data
622+
labels, which will add the given amount of padding (in points)
623+
between the axes and the label. The x-direction is in data
623624
coordinates and the y-direction is in axis coordinates.
624625
Returns a 3-tuple of the form:
625626
@@ -632,7 +633,8 @@ def get_xaxis_text2_transform(self, pad_pixels):
632633
need to place axis elements in different locations.
633634
"""
634635
return (self._xaxis_transform +
635-
mtransforms.Affine2D().translate(0, pad_pixels),
636+
mtransforms.ScaledTranslation(0, pad_points / 72.0,
637+
self.figure.dpi_scale_trans),
636638
"bottom", "center")
637639

638640
def get_yaxis_transform(self):
@@ -647,13 +649,13 @@ def get_yaxis_transform(self):
647649
"""
648650
return self._yaxis_transform
649651

650-
def get_yaxis_text1_transform(self, pad_pixels):
652+
def get_yaxis_text1_transform(self, pad_points):
651653
"""
652654
Get the transformation used for drawing y-axis labels, which
653-
will add the given number of pad_pixels between the axes and
654-
the label. The x-direction is in axis coordinates and the
655-
y-direction is in data coordinates. Returns a 3-tuple of the
656-
form:
655+
will add the given amount of padding (in points) between the
656+
axes and the label. The x-direction is in axis coordinates
657+
and the y-direction is in data coordinates. Returns a 3-tuple
658+
of the form:
657659
658660
(transform, valign, halign)
659661
@@ -664,14 +666,15 @@ def get_yaxis_text1_transform(self, pad_pixels):
664666
need to place axis elements in different locations.
665667
"""
666668
return (self._yaxis_transform +
667-
mtransforms.Affine2D().translate(-1 * pad_pixels, 0),
669+
mtransforms.ScaledTranslation(-1 * pad_points / 72.0, 0,
670+
self.figure.dpi_scale_trans),
668671
"center", "right")
669672

670-
def get_yaxis_text2_transform(self, pad_pixels):
673+
def get_yaxis_text2_transform(self, pad_points):
671674
"""
672675
Get the transformation used for drawing the secondary y-axis
673-
labels, which will add the given number of pad_pixels between
674-
the axes and the label. The x-direction is in axis
676+
labels, which will add the given amount of padding (in points)
677+
between the axes and the label. The x-direction is in axis
675678
coordinates and the y-direction is in data coordinates.
676679
Returns a 3-tuple of the form:
677680
@@ -684,7 +687,8 @@ def get_yaxis_text2_transform(self, pad_pixels):
684687
need to place axis elements in different locations.
685688
"""
686689
return (self._yaxis_transform +
687-
mtransforms.Affine2D().translate(pad_pixels, 0),
690+
mtransforms.ScaledTranslation(-1 * pad_points / 72.0, 0,
691+
self.figure.dpi_scale_trans),
688692
"center", "left")
689693

690694
def _update_transScale(self):
@@ -4287,7 +4291,6 @@ def scatter(self, x, y, s=20, c='b', marker='o', cmap=None, norm=None,
42874291
if sym is not None:
42884292
if symstyle==0:
42894293
collection = mcoll.RegularPolyCollection(
4290-
self.figure.dpi,
42914294
numsides, rotation, scales,
42924295
facecolors = colors,
42934296
edgecolors = edgecolors,
@@ -4297,7 +4300,6 @@ def scatter(self, x, y, s=20, c='b', marker='o', cmap=None, norm=None,
42974300
)
42984301
elif symstyle==1:
42994302
collection = mcoll.StarPolygonCollection(
4300-
self.figure.dpi,
43014303
numsides, rotation, scales,
43024304
facecolors = colors,
43034305
edgecolors = edgecolors,
@@ -4307,7 +4309,6 @@ def scatter(self, x, y, s=20, c='b', marker='o', cmap=None, norm=None,
43074309
)
43084310
elif symstyle==2:
43094311
collection = mcoll.AsteriskPolygonCollection(
4310-
self.figure.dpi,
43114312
numsides, rotation, scales,
43124313
facecolors = colors,
43134314
edgecolors = edgecolors,
@@ -4316,6 +4317,7 @@ def scatter(self, x, y, s=20, c='b', marker='o', cmap=None, norm=None,
43164317
transOffset = self.transData,
43174318
)
43184319
else:
4320+
# MGDTODO: This has dpi problems
43194321
# rescale verts
43204322
rescale = npy.sqrt(max(verts[:,0]**2+verts[:,1]**2))
43214323
verts /= rescale

lib/matplotlib/axis.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ def _get_text1(self):
223223
# get the affine as an a,b,c,d,tx,ty list
224224
# x in data coords, y in axes coords
225225
#t = Text(
226-
trans, vert, horiz = self.axes.get_xaxis_text1_transform(self.get_pad_pixels())
226+
trans, vert, horiz = self.axes.get_xaxis_text1_transform(self._pad)
227227

228-
t = TextWithDash(
228+
t = TextWithDash(
229229
x=0, y=0,
230230
fontproperties=FontProperties(size=rcParams['xtick.labelsize']),
231231
color=rcParams['xtick.color'],
@@ -245,7 +245,7 @@ def _get_text2(self):
245245
'Get the default Text 2 instance'
246246
# x in data coords, y in axes coords
247247
#t = Text(
248-
trans, vert, horiz = self.axes.get_xaxis_text2_transform(self.get_pad_pixels())
248+
trans, vert, horiz = self.axes.get_xaxis_text2_transform(self._pad)
249249

250250
t = TextWithDash(
251251
x=0, y=1,
@@ -358,7 +358,7 @@ def _get_text1(self):
358358
'Get the default Text instance'
359359
# x in axes coords, y in data coords
360360
#t = Text(
361-
trans, vert, horiz = self.axes.get_yaxis_text1_transform(self.get_pad_pixels())
361+
trans, vert, horiz = self.axes.get_yaxis_text1_transform(self._pad)
362362

363363
t = TextWithDash(
364364
x=0, y=0,
@@ -378,7 +378,7 @@ def _get_text2(self):
378378
'Get the default Text instance'
379379
# x in axes coords, y in data coords
380380
#t = Text(
381-
trans, vert, horiz = self.axes.get_yaxis_text2_transform(self.get_pad_pixels())
381+
trans, vert, horiz = self.axes.get_yaxis_text2_transform(self._pad)
382382

383383
t = TextWithDash(
384384
x=1, y=0,

lib/matplotlib/collections.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,6 @@ class RegularPolyCollection(Collection):
505505
_path_generator = mpath.Path.unit_regular_polygon
506506

507507
def __init__(self,
508-
dpi,
509508
numsides,
510509
rotation = 0 ,
511510
sizes = (1,),
@@ -532,7 +531,6 @@ def __init__(self,
532531
black = (0,0,0,1)
533532
534533
collection = RegularPolyCollection(
535-
fig.dpi,
536534
numsides=5, # a pentagon
537535
rotation=0,
538536
sizes=(50,),
@@ -545,18 +543,21 @@ def __init__(self,
545543
"""
546544
Collection.__init__(self,**kwargs)
547545
self._sizes = sizes
548-
self._dpi = dpi
549546
self._paths = [self._path_generator(numsides)]
550-
# sizes is the area of the circle circumscribing the polygon
551-
# in points^2
552-
self._transforms = [
553-
transforms.Affine2D().rotate(-rotation).scale(
554-
(math.sqrt(x) * self._dpi / 72.0) / math.sqrt(math.pi))
555-
for x in sizes]
547+
self._rotation = rotation
556548
self.set_transform(transforms.IdentityTransform())
557549

558550
__init__.__doc__ = cbook.dedent(__init__.__doc__) % artist.kwdocd
559551

552+
def draw(self, renderer):
553+
# sizes is the area of the circle circumscribing the polygon
554+
# in points^2
555+
self._transforms = [
556+
transforms.Affine2D().rotate(-self._rotation).scale(
557+
(npy.sqrt(x) * renderer.dpi / 72.0) / npy.sqrt(npy.pi))
558+
for x in self._sizes]
559+
return Collection.draw(self, renderer)
560+
560561
def get_paths(self):
561562
return self._paths
562563

lib/matplotlib/figure.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ def __init__(self,
122122
if facecolor is None: facecolor = rcParams['figure.facecolor']
123123
if edgecolor is None: edgecolor = rcParams['figure.edgecolor']
124124

125-
self._dpi_scale_trans = Affine2D()
125+
self.dpi_scale_trans = Affine2D()
126126
self.dpi = dpi
127127
self.bbox_inches = Bbox.from_bounds(0, 0, *figsize)
128-
self.bbox = TransformedBbox(self.bbox_inches, self._dpi_scale_trans)
128+
self.bbox = TransformedBbox(self.bbox_inches, self.dpi_scale_trans)
129129

130130
self.frameon = frameon
131131

@@ -157,7 +157,7 @@ def _get_dpi(self):
157157
return self._dpi
158158
def _set_dpi(self, dpi):
159159
self._dpi = dpi
160-
self._dpi_scale_trans.clear().scale(dpi, dpi)
160+
self.dpi_scale_trans.clear().scale(dpi, dpi)
161161
dpi = property(_get_dpi, _set_dpi)
162162

163163
def enable_auto_layout(self, setting=True):

lib/matplotlib/patches.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -727,11 +727,11 @@ class YAArrow(Patch):
727727
def __str__(self):
728728
return "YAArrow()"
729729

730-
def __init__(self, dpi, xytip, xybase, width=4, frac=0.1, headwidth=12, **kwargs):
730+
def __init__(self, figure, xytip, xybase, width=4, frac=0.1, headwidth=12, **kwargs):
731731
"""
732732
xytip : (x,y) location of arrow tip
733733
xybase : (x,y) location the arrow base mid point
734-
dpi : the figure dpi instance (fig.dpi)
734+
figure : the figure instance (fig.dpi)
735735
width : the width of the arrow in points
736736
frac : the fraction of the arrow length occupied by the head
737737
headwidth : the width of the base of the arrow head in points
@@ -740,7 +740,7 @@ def __init__(self, dpi, xytip, xybase, width=4, frac=0.1, headwidth=12, **kwargs
740740
%(Patch)s
741741
742742
"""
743-
self.dpi = dpi
743+
self.figure = figure
744744
self.xytip = xytip
745745
self.xybase = xybase
746746
self.width = width
@@ -756,8 +756,8 @@ def get_path(self):
756756
# the base vertices
757757
x1, y1 = self.xytip
758758
x2, y2 = self.xybase
759-
k1 = self.width*self.dpi/72./2.
760-
k2 = self.headwidth*self.dpi/72./2.
759+
k1 = self.width*self.figure.dpi/72./2.
760+
k2 = self.headwidth*self.figure.dpi/72./2.
761761
xb1, yb1, xb2, yb2 = self.getpoints(x1, y1, x2, y2, k1)
762762

763763
# a point on the segment 20% of the distance from the tip to the base

lib/matplotlib/projections/geo.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,19 @@ def update_layout(self, renderer):
120120
def get_xaxis_transform(self):
121121
return self._xaxis_transform
122122

123-
def get_xaxis_text1_transform(self, pixelPad):
123+
def get_xaxis_text1_transform(self, pad):
124124
return self._xaxis_text1_transform, 'bottom', 'center'
125125

126-
def get_xaxis_text2_transform(self, pixelPad):
126+
def get_xaxis_text2_transform(self, pad):
127127
return self._xaxis_text2_transform, 'top', 'center'
128128

129129
def get_yaxis_transform(self):
130130
return self._yaxis_transform
131131

132-
def get_yaxis_text1_transform(self, pixelPad):
132+
def get_yaxis_text1_transform(self, pad):
133133
return self._yaxis_text1_transform, 'center', 'right'
134134

135-
def get_yaxis_text2_transform(self, pixelPad):
135+
def get_yaxis_text2_transform(self, pad):
136136
return self._yaxis_text2_transform, 'center', 'left'
137137

138138
def get_axes_patch(self):

lib/matplotlib/projections/polar.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,19 +252,19 @@ def update_layout(self, renderer):
252252
def get_xaxis_transform(self):
253253
return self._xaxis_transform
254254

255-
def get_xaxis_text1_transform(self, pixelPad):
255+
def get_xaxis_text1_transform(self, pad):
256256
return self._xaxis_text1_transform, 'center', 'center'
257257

258-
def get_xaxis_text2_transform(self, pixelPad):
258+
def get_xaxis_text2_transform(self, pad):
259259
return self._xaxis_text2_transform, 'center', 'center'
260260

261261
def get_yaxis_transform(self):
262262
return self._yaxis_transform
263263

264-
def get_yaxis_text1_transform(self, pixelPad):
264+
def get_yaxis_text1_transform(self, pad):
265265
return self._yaxis_text1_transform, 'center', 'center'
266266

267-
def get_yaxis_text2_transform(self, pixelPad):
267+
def get_yaxis_text2_transform(self, pad):
268268
return self._yaxis_text2_transform, 'center', 'center'
269269

270270
def get_axes_patch(self):

lib/matplotlib/text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@ def update_positions(self, renderer):
11951195
dx = shrink*r*math.cos(theta)
11961196
dy = shrink*r*math.sin(theta)
11971197

1198-
self.arrow = YAArrow(self.figure.dpi, (x0+dx,y0+dy), (x-dx, y-dy),
1198+
self.arrow = YAArrow(self.figure, (x0+dx,y0+dy), (x-dx, y-dy),
11991199
width=width, headwidth=headwidth, frac=frac,
12001200
**d)
12011201
self.arrow.set_clip_box(self.get_clip_box())

lib/matplotlib/transforms.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,6 +1898,32 @@ def get_matrix(self):
18981898
get_matrix.__doc__ = Affine2DBase.get_matrix.__doc__
18991899

19001900

1901+
class ScaledTranslation(Affine2DBase):
1902+
def __init__(self, xt, yt, scale_trans):
1903+
Affine2DBase.__init__(self)
1904+
self._t = (xt, yt)
1905+
self._scale_trans = scale_trans
1906+
self.set_children(scale_trans)
1907+
self._mtx = None
1908+
self._inverted = None
1909+
1910+
def __repr__(self):
1911+
return "ScaledTranslation(%s)" % (self._t)
1912+
__str__ = __repr__
1913+
1914+
def get_matrix(self):
1915+
if self._invalid:
1916+
xt, yt = self._scale_trans.transform_point(self._t)
1917+
self._mtx = npy.array([[1.0, 0.0, xt],
1918+
[0.0, 1.0, yt],
1919+
[0.0, 0.0, 1.0]],
1920+
npy.float_)
1921+
self._invalid = 0
1922+
self._inverted = None
1923+
return self._mtx
1924+
get_matrix.__doc__ = Affine2DBase.get_matrix.__doc__
1925+
1926+
19011927
class TransformedPath(TransformNode):
19021928
"""
19031929
A TransformedPath caches a non-affine transformed copy of the

0 commit comments

Comments
 (0)