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

Skip to content

Commit 668690d

Browse files
committed
Merge pull request #1929 from pelson/bbox_inches_contour_problem
Fixed failing bbox_inches='tight' case when a contour collection is empty
2 parents 3120e6c + c5d1147 commit 668690d

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed

lib/matplotlib/collections.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,13 @@ def get_datalim(self, transData):
186186
# get_path_collection_extents handles nan but not masked arrays
187187
offsets.shape = (-1, 2) # Make it Nx2
188188

189-
result = mpath.get_path_collection_extents(
190-
transform.frozen(), paths, self.get_transforms(),
191-
offsets, transOffset.frozen())
192-
result = result.inverse_transformed(transData)
189+
if paths:
190+
result = mpath.get_path_collection_extents(
191+
transform.frozen(), paths, self.get_transforms(),
192+
offsets, transOffset.frozen())
193+
result = result.inverse_transformed(transData)
194+
else:
195+
result = transforms.Bbox([[0, 0], [0, 0]])
193196
return result
194197

195198
def get_window_extent(self, renderer):

lib/matplotlib/tests/test_collections.py

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
Tests specific to the collections module.
33
"""
44

5-
import nose.tools
5+
from nose.tools import assert_equal
66
import numpy as np
7+
from numpy.testing import assert_array_equal
8+
79
import matplotlib.pyplot as plt
10+
import matplotlib.collections as mcollections
11+
import matplotlib.transforms as mtransforms
812
from matplotlib.collections import EventCollection
913
from matplotlib.testing.decorators import cleanup, image_comparison
1014

@@ -81,7 +85,7 @@ def test__EventCollection__get_orientation():
8185
orientation
8286
'''
8387
_, coll, props = generate_EventCollection_plot()
84-
nose.tools.assert_equal(props['orientation'], coll.get_orientation())
88+
assert_equal(props['orientation'], coll.get_orientation())
8589

8690

8791
@cleanup
@@ -91,7 +95,7 @@ def test__EventCollection__is_horizontal():
9195
orientation
9296
'''
9397
_, coll, _ = generate_EventCollection_plot()
94-
nose.tools.assert_equal(True, coll.is_horizontal())
98+
assert_equal(True, coll.is_horizontal())
9599

96100

97101
@cleanup
@@ -100,7 +104,7 @@ def test__EventCollection__get_linelength():
100104
check to make sure the default linelength matches the input linelength
101105
'''
102106
_, coll, props = generate_EventCollection_plot()
103-
nose.tools.assert_equal(props['linelength'], coll.get_linelength())
107+
assert_equal(props['linelength'], coll.get_linelength())
104108

105109

106110
@cleanup
@@ -109,7 +113,7 @@ def test__EventCollection__get_lineoffset():
109113
check to make sure the default lineoffset matches the input lineoffset
110114
'''
111115
_, coll, props = generate_EventCollection_plot()
112-
nose.tools.assert_equal(props['lineoffset'], coll.get_lineoffset())
116+
assert_equal(props['lineoffset'], coll.get_lineoffset())
113117

114118

115119
@cleanup
@@ -118,7 +122,7 @@ def test__EventCollection__get_linestyle():
118122
check to make sure the default linestyle matches the input linestyle
119123
'''
120124
_, coll, _ = generate_EventCollection_plot()
121-
nose.tools.assert_equal(coll.get_linestyle(), [(None, None)])
125+
assert_equal(coll.get_linestyle(), [(None, None)])
122126

123127

124128
@cleanup
@@ -213,8 +217,8 @@ def test__EventCollection__switch_orientation():
213217
splt, coll, props = generate_EventCollection_plot()
214218
new_orientation = 'vertical'
215219
coll.switch_orientation()
216-
nose.tools.assert_equal(new_orientation, coll.get_orientation())
217-
nose.tools.assert_equal(False, coll.is_horizontal())
220+
assert_equal(new_orientation, coll.get_orientation())
221+
assert_equal(False, coll.is_horizontal())
218222
new_positions = coll.get_positions()
219223
check_segments(coll,
220224
new_positions,
@@ -236,8 +240,8 @@ def test__EventCollection__switch_orientation_2x():
236240
coll.switch_orientation()
237241
coll.switch_orientation()
238242
new_positions = coll.get_positions()
239-
nose.tools.assert_equal(props['orientation'], coll.get_orientation())
240-
nose.tools.assert_equal(True, coll.is_horizontal())
243+
assert_equal(props['orientation'], coll.get_orientation())
244+
assert_equal(True, coll.is_horizontal())
241245
np.testing.assert_array_equal(props['positions'], new_positions)
242246
check_segments(coll,
243247
new_positions,
@@ -255,8 +259,8 @@ def test__EventCollection__set_orientation():
255259
splt, coll, props = generate_EventCollection_plot()
256260
new_orientation = 'vertical'
257261
coll.set_orientation(new_orientation)
258-
nose.tools.assert_equal(new_orientation, coll.get_orientation())
259-
nose.tools.assert_equal(False, coll.is_horizontal())
262+
assert_equal(new_orientation, coll.get_orientation())
263+
assert_equal(False, coll.is_horizontal())
260264
check_segments(coll,
261265
props['positions'],
262266
props['linelength'],
@@ -275,7 +279,7 @@ def test__EventCollection__set_linelength():
275279
splt, coll, props = generate_EventCollection_plot()
276280
new_linelength = 15
277281
coll.set_linelength(new_linelength)
278-
nose.tools.assert_equal(new_linelength, coll.get_linelength())
282+
assert_equal(new_linelength, coll.get_linelength())
279283
check_segments(coll,
280284
props['positions'],
281285
new_linelength,
@@ -293,7 +297,7 @@ def test__EventCollection__set_lineoffset():
293297
splt, coll, props = generate_EventCollection_plot()
294298
new_lineoffset = -5.
295299
coll.set_lineoffset(new_lineoffset)
296-
nose.tools.assert_equal(new_lineoffset, coll.get_lineoffset())
300+
assert_equal(new_lineoffset, coll.get_lineoffset())
297301
check_segments(coll,
298302
props['positions'],
299303
props['linelength'],
@@ -311,7 +315,7 @@ def test__EventCollection__set_linestyle():
311315
splt, coll, _ = generate_EventCollection_plot()
312316
new_linestyle = 'dashed'
313317
coll.set_linestyle(new_linestyle)
314-
nose.tools.assert_equal(coll.get_linestyle(), [(0, (6.0, 6.0))])
318+
assert_equal(coll.get_linestyle(), [(0, (6.0, 6.0))])
315319
splt.set_title('EventCollection: set_linestyle')
316320

317321

@@ -323,7 +327,7 @@ def test__EventCollection__set_linewidth():
323327
splt, coll, _ = generate_EventCollection_plot()
324328
new_linewidth = 5
325329
coll.set_linewidth(new_linewidth)
326-
nose.tools.assert_equal(coll.get_linewidth(), new_linewidth)
330+
assert_equal(coll.get_linewidth(), new_linewidth)
327331
splt.set_title('EventCollection: set_linewidth')
328332

329333

@@ -362,10 +366,10 @@ def check_segments(coll, positions, linelength, lineoffset, orientation):
362366

363367
# test to make sure each segment is correct
364368
for i, segment in enumerate(segments):
365-
nose.tools.assert_equal(segment[0, pos1], lineoffset + linelength / 2.)
366-
nose.tools.assert_equal(segment[1, pos1], lineoffset - linelength / 2.)
367-
nose.tools.assert_equal(segment[0, pos2], positions[i])
368-
nose.tools.assert_equal(segment[1, pos2], positions[i])
369+
assert_equal(segment[0, pos1], lineoffset + linelength / 2.)
370+
assert_equal(segment[1, pos1], lineoffset - linelength / 2.)
371+
assert_equal(segment[0, pos2], positions[i])
372+
assert_equal(segment[1, pos2], positions[i])
369373

370374

371375
def check_allprop(values, target):
@@ -375,7 +379,7 @@ def check_allprop(values, target):
375379
note: this is not a test, it is used by tests
376380
'''
377381
for value in values:
378-
nose.tools.assert_equal(value, target)
382+
assert_equal(value, target)
379383

380384

381385
def check_allprop_array(values, target):
@@ -387,6 +391,14 @@ def check_allprop_array(values, target):
387391
for value in values:
388392
np.testing.assert_array_equal(value, target)
389393

390-
if __name__ == '_main_':
394+
395+
def test_null_collection_datalim():
396+
col = mcollections.PathCollection([])
397+
col_data_lim = col.get_datalim(mtransforms.IdentityTransform())
398+
assert_array_equal(col_data_lim.get_points(),
399+
mtransforms.Bbox([[0, 0], [0, 0]]).get_points())
400+
401+
402+
if __name__ == '__main__':
391403
import nose
392404
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

lib/matplotlib/transforms.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,11 @@ def __init__(self, points, **kwargs):
772772
:meth:`from_bounds` and :meth:`from_extents`.
773773
"""
774774
BboxBase.__init__(self, **kwargs)
775-
self._points = np.asarray(points, np.float_)
775+
points = np.asarray(points, np.float_)
776+
if points.shape != (2, 2):
777+
raise ValueError('Bbox points must be of the form '
778+
'"[[x0, y0], [x1, y1]]".')
779+
self._points = points
776780
self._minpos = np.array([0.0000001, 0.0000001])
777781
self._ignore = True
778782
# it is helpful in some contexts to know if the bbox is a

0 commit comments

Comments
 (0)