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

Skip to content

Commit 1b44a78

Browse files
Phil Elsonpelson
Phil Elson
authored andcommitted
Added _as_mpl_axes test.
1 parent 37e3851 commit 1b44a78

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

lib/matplotlib/projections/polar.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ def __init__(self, *args, **kwargs):
231231
"""
232232

233233
self.resolution = kwargs.pop('resolution', None)
234+
self._default_theta_offset = kwargs.pop('theta_offset', 0)
235+
self._default_theta_direction = kwargs.pop('theta_direction', 1)
236+
234237
if self.resolution not in (None, 1):
235238
warnings.warn(
236239
"""The resolution kwarg to Polar plots is now ignored.
@@ -259,8 +262,8 @@ def cla(self):
259262
# Why do we need to turn on yaxis tick labels, but
260263
# xaxis tick labels are already on?
261264

262-
self.set_theta_offset(0)
263-
self.set_theta_direction(1)
265+
self.set_theta_offset(self._default_theta_offset)
266+
self.set_theta_direction(self._default_theta_direction)
264267

265268
def _init_axis(self):
266269
"move this out of __init__ because non-separable axes don't use it"

lib/matplotlib/tests/test_axes.py

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,16 +272,14 @@ def test_polar_wrap():
272272

273273
fig = plt.figure()
274274

275-
#NOTE: resolution=1 really should be the default
276-
plt.subplot( 111, polar=True, resolution=1 )
275+
plt.subplot(111, polar=True)
277276
plt.polar( [179*D2R, -179*D2R], [0.2, 0.1], "b.-" )
278277
plt.polar( [179*D2R, 181*D2R], [0.2, 0.1], "g.-" )
279278
plt.rgrids( [0.05, 0.1, 0.15, 0.2, 0.25, 0.3] )
280279

281280
fig = plt.figure()
282281

283-
#NOTE: resolution=1 really should be the default
284-
plt.subplot( 111, polar=True, resolution=1 )
282+
plt.subplot( 111, polar=True)
285283
plt.polar( [2*D2R, -2*D2R], [0.2, 0.1], "b.-" )
286284
plt.polar( [2*D2R, 358*D2R], [0.2, 0.1], "g.-" )
287285
plt.polar( [358*D2R, 2*D2R], [0.2, 0.1], "r.-" )
@@ -713,6 +711,56 @@ def test_scatter_plot():
713711
ax.scatter([3, 4, 2, 6], [2, 5, 2, 3], c=['r', 'y', 'b', 'lime'], s=[24, 15, 19, 29])
714712

715713

714+
def test_as_mpl_axes_api():
715+
# tests the _as_mpl_axes api
716+
from matplotlib.projections.polar import PolarAxes
717+
import matplotlib.axes as maxes
718+
719+
class Polar(object):
720+
def __init__(self):
721+
self.theta_offset = 0
722+
723+
def _as_mpl_axes(self):
724+
# implement the matplotlib axes interface
725+
return PolarAxes, {'theta_offset': self.theta_offset}
726+
prj = Polar()
727+
prj2 = Polar()
728+
prj2.theta_offset = np.pi
729+
prj3 = Polar()
730+
731+
# testing axes creation with plt.axes
732+
ax = plt.axes([0, 0, 1, 1], projection=prj)
733+
assert type(ax) == PolarAxes, \
734+
'Expected a PolarAxes, got %s' % type(ax)
735+
ax_via_gca = plt.gca(projection=prj)
736+
# ideally, ax_via_gca is ax should be true. However, gca isn't
737+
# plummed like that. (even with projection='polar').
738+
assert ax_via_gca is not ax
739+
plt.close()
740+
741+
# testing axes creation with gca
742+
ax = plt.gca(projection=prj)
743+
assert type(ax) == maxes._subplot_classes[PolarAxes], \
744+
'Expected a PolarAxesSubplot, got %s' % type(ax)
745+
ax_via_gca = plt.gca(projection=prj)
746+
assert ax_via_gca is ax
747+
# try getting the axes given a different polar projection
748+
ax_via_gca = plt.gca(projection=prj2)
749+
assert ax_via_gca is not ax
750+
assert ax.get_theta_offset() == 0, ax.get_theta_offset()
751+
assert ax_via_gca.get_theta_offset() == np.pi, ax_via_gca.get_theta_offset()
752+
# try getting the axes given an == (not is) polar projection
753+
ax_via_gca = plt.gca(projection=prj3)
754+
assert ax_via_gca is ax
755+
plt.close()
756+
757+
# testing axes creation with subplot
758+
ax = plt.subplot(121, projection=prj)
759+
assert type(ax) == maxes._subplot_classes[PolarAxes], \
760+
'Expected a PolarAxesSubplot, got %s' % type(ax)
761+
plt.close()
762+
763+
716764
if __name__=='__main__':
717765
import nose
718766
nose.runmodule(argv=['-s','--with-doctest'], exit=False)

0 commit comments

Comments
 (0)