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

Skip to content

Commit a99c62e

Browse files
committed
2 parents ba668f9 + 1a78c32 commit a99c62e

File tree

7 files changed

+67
-37
lines changed

7 files changed

+67
-37
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Preserve units with Text position
2+
`````````````````````````````````
3+
4+
Previously the 'get_position' method on Text would strip away unit information
5+
even though the units were still present. There was no inherent need to do
6+
this, so it has been changed so that unit data (if present) will be preserved.
7+
Essentially a call to 'get_position' will return the exact value from a call to
8+
'set_position'.
9+
10+
If you wish to get the old behaviour, then you can use the new method called
11+
'get_unitless_position'.
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from pylab import *
2-
x = randn(1000)
3-
y = randn(1000) + 5
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
x = np.random.randn(1000)
4+
y = np.random.randn(1000) + 5
45

56
# normal distribution center at x=0 and y=5
6-
hist2d(x, y, bins=40)
7-
show()
7+
plt.hist2d(x, y, bins=40)
8+
plt.show()
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from matplotlib.colors import LogNorm
2-
from pylab import *
2+
import matplotlib.pyplot as plt
3+
import numpy as np
34

45
# normal distribution center at x=0 and y=5
5-
x = randn(100000)
6-
y = randn(100000) + 5
6+
x = np.random.randn(100000)
7+
y = np.random.randn(100000) + 5
78

8-
hist2d(x, y, bins=40, norm=LogNorm())
9-
colorbar()
10-
show()
9+
plt.hist2d(x, y, bins=40, norm=LogNorm())
10+
plt.colorbar()
11+
plt.show()

examples/pylab_examples/image_demo2.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from __future__ import print_function
2-
# from pylab import *
32
import matplotlib.pyplot as plt
43
import numpy as np
54
import matplotlib.cbook as cbook

examples/pylab_examples/image_interp.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python
21
"""
32
The same (small) array, interpolated with three different
43
interpolation methods.
@@ -46,18 +45,20 @@
4645
suggested.
4746
4847
"""
49-
from pylab import *
50-
A = rand(5, 5)
51-
figure(1)
52-
imshow(A, interpolation='nearest')
53-
grid(True)
48+
import matplotlib.pyplot as plt
49+
import numpy as np
5450

55-
figure(2)
56-
imshow(A, interpolation='bilinear')
57-
grid(True)
51+
A = np.random.rand(5, 5)
52+
plt.figure(1)
53+
plt.imshow(A, interpolation='nearest')
54+
plt.grid(True)
5855

59-
figure(3)
60-
imshow(A, interpolation='bicubic')
61-
grid(True)
56+
plt.figure(2)
57+
plt.imshow(A, interpolation='bilinear')
58+
plt.grid(True)
6259

63-
show()
60+
plt.figure(3)
61+
plt.imshow(A, interpolation='bicubic')
62+
plt.grid(True)
63+
64+
plt.show()

lib/matplotlib/animation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ def new_saved_frame_seq(self):
11421142
if self._save_seq:
11431143
# While iterating we are going to update _save_seq
11441144
# so make a copy to safely iterate over
1145-
self._old_saved_seq = self._save_seq.copy()
1145+
self._old_saved_seq = list(self._save_seq)
11461146
return iter(self._old_saved_seq)
11471147
else:
11481148
return itertools.islice(self.new_frame_seq(), self.save_count)

lib/matplotlib/text.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ def update(self, kwargs):
240240
"""
241241
bbox = kwargs.pop('bbox', None)
242242
super(Text, self).update(kwargs)
243-
self.set_bbox(bbox) # depends on font properties
243+
if bbox:
244+
self.set_bbox(bbox) # depends on font properties
244245

245246
def __getstate__(self):
246247
d = super(Text, self).__getstate__()
@@ -280,7 +281,7 @@ def contains(self, mouseevent):
280281

281282
def _get_xy_display(self):
282283
'get the (possibly unit converted) transformed x, y in display coords'
283-
x, y = self.get_position()
284+
x, y = self.get_unitless_position()
284285
return self.get_transform().transform_point((x, y))
285286

286287
def _get_multialignment(self):
@@ -536,8 +537,8 @@ def update_bbox_position_size(self, renderer):
536537

537538
trans = self.get_transform()
538539

539-
# don't use self.get_position here, which refers to text position
540-
# in Text, and dash position in TextWithDash:
540+
# don't use self.get_unitless_position here, which refers to text
541+
# position in Text, and dash position in TextWithDash:
541542
posx = float(self.convert_xunits(self._x))
542543
posy = float(self.convert_yunits(self._y))
543544

@@ -877,12 +878,20 @@ def get_horizontalalignment(self):
877878
"""
878879
return self._horizontalalignment
879880

880-
def get_position(self):
881-
"Return the position of the text as a tuple (*x*, *y*)"
881+
def get_unitless_position(self):
882+
"Return the unitless position of the text as a tuple (*x*, *y*)"
883+
# This will get the position with all unit information stripped away.
884+
# This is here for convienience since it is done in several locations.
882885
x = float(self.convert_xunits(self._x))
883886
y = float(self.convert_yunits(self._y))
884887
return x, y
885888

889+
def get_position(self):
890+
"Return the position of the text as a tuple (*x*, *y*)"
891+
# This should return the same data (possible unitized) as was
892+
# specified with 'set_x' and 'set_y'.
893+
return self._x, self._y
894+
886895
def get_prop_tup(self):
887896
"""
888897
Return a hashable tuple of properties.
@@ -891,7 +900,7 @@ def get_prop_tup(self):
891900
want to cache derived information about text (e.g., layouts) and
892901
need to know if the text has changed.
893902
"""
894-
x, y = self.get_position()
903+
x, y = self.get_unitless_position()
895904
return (x, y, self.get_text(), self._color,
896905
self._verticalalignment, self._horizontalalignment,
897906
hash(self._fontproperties),
@@ -950,7 +959,7 @@ def get_window_extent(self, renderer=None, dpi=None):
950959
raise RuntimeError('Cannot get window extent w/o renderer')
951960

952961
bbox, info, descent = self._get_layout(self._renderer)
953-
x, y = self.get_position()
962+
x, y = self.get_unitless_position()
954963
x, y = self.get_transform().transform_point((x, y))
955964
bbox = bbox.translated(x, y)
956965
if dpi is not None:
@@ -1365,12 +1374,20 @@ def __init__(self,
13651374

13661375
#self.set_bbox(dict(pad=0))
13671376

1368-
def get_position(self):
1369-
"Return the position of the text as a tuple (*x*, *y*)"
1377+
def get_unitless_position(self):
1378+
"Return the unitless position of the text as a tuple (*x*, *y*)"
1379+
# This will get the position with all unit information stripped away.
1380+
# This is here for convienience since it is done in several locations.
13701381
x = float(self.convert_xunits(self._dashx))
13711382
y = float(self.convert_yunits(self._dashy))
13721383
return x, y
13731384

1385+
def get_position(self):
1386+
"Return the position of the text as a tuple (*x*, *y*)"
1387+
# This should return the same data (possibly unitized) as was
1388+
# specified with set_x and set_y
1389+
return self._dashx, self._dashy
1390+
13741391
def get_prop_tup(self):
13751392
"""
13761393
Return a hashable tuple of properties.
@@ -1402,7 +1419,7 @@ def update_coords(self, renderer):
14021419
with respect to the actual canvas's coordinates we need to map
14031420
back and forth.
14041421
"""
1405-
dashx, dashy = self.get_position()
1422+
dashx, dashy = self.get_unitless_position()
14061423
dashlength = self.get_dashlength()
14071424
# Shortcircuit this process if we don't have a dash
14081425
if dashlength == 0.0:

0 commit comments

Comments
 (0)