Skip to content

Commit

Permalink
Merge pull request #1111 from pelson/transoffset_fix
Browse files Browse the repository at this point in the history
Fixed transoffset example from failing.
  • Loading branch information
mdboom committed Aug 21, 2012
2 parents 9ae66ef + 8bfdaf2 commit dda4ab6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 26 deletions.
31 changes: 16 additions & 15 deletions lib/matplotlib/figure.py
Expand Up @@ -17,7 +17,7 @@
import artist
from artist import Artist, allow_rasterization
from axes import Axes, SubplotBase, subplot_class_factory
from cbook import flatten, allequal, Stack, iterable, is_string_like
from cbook import allequal, Stack, iterable
from matplotlib import _image
import colorbar as cbar
from image import FigureImage
Expand All @@ -27,15 +27,14 @@

from legend import Legend
from transforms import Affine2D, Bbox, BboxTransformTo, TransformedBbox
from projections import get_projection_names, get_projection_class, \
process_projection_requirements
from projections import get_projection_names, process_projection_requirements
from matplotlib.blocking_input import BlockingMouseInput, BlockingKeyMouseInput

import matplotlib.cbook as cbook
from matplotlib import docstring

from operator import itemgetter
import os.path


docstring.interpd.update(projection_names = get_projection_names())

Expand Down Expand Up @@ -110,7 +109,8 @@ def add(self, key, a):
a_existing = self.get(key)
if a_existing is not None:
Stack.remove(self, (key, a_existing))
warnings.Warn(
import warnings
warnings.warn(
"key %s already existed; Axes is being replaced" % key)
# I don't think the above should ever happen.

Expand Down Expand Up @@ -138,6 +138,7 @@ def __call__(self):
def __contains__(self, a):
return a in self.as_list()


class SubplotParams:
"""
A class to hold the parameters for a subplot
Expand Down Expand Up @@ -212,8 +213,6 @@ def reset():
reset()
raise ValueError('bottom cannot be >= top')



def _update_this(self, s, val):
if val is None:
val = getattr(self, s, None)
Expand All @@ -223,6 +222,7 @@ def _update_this(self, s, val):

setattr(self, s, val)


class Figure(Artist):

"""
Expand Down Expand Up @@ -1081,13 +1081,9 @@ def gca(self, **kwargs):
The following kwargs are supported for ensuring the returned axes
adheres to the given projection etc., and for axes creation if
the active axes does not exist:
%(Axes)s
.. note::
When specifying kwargs to ``gca`` to find the pre-created active
axes, they should be equivalent in every way to the kwargs which
were used in its creation.
"""
ckey, cax = self._axstack.current_key_axes()
# if there exists an axes on the stack see if it maches
Expand All @@ -1107,12 +1103,18 @@ def gca(self, **kwargs):
kwargs_copy = kwargs.copy()
projection_class, _, key = \
process_projection_requirements(self, **kwargs_copy)

# let the returned axes have any gridspec by removing it from the key
ckey = ckey[1:]
key = key[1:]

# if the cax matches this key then return the axes, otherwise
# continue and a new axes will be created
if key == ckey and isinstance(cax, projection_class):
return cax

return self.add_subplot(111, **kwargs)

# no axes found, so create one which spans the figure
return self.add_subplot(1, 1, 1, **kwargs)

def sca(self, a):
'Set the current axes to be a and return a'
Expand Down Expand Up @@ -1395,7 +1397,6 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None, w_pad=None, rect=Non
self.subplots_adjust(**kwargs)



def figaspect(arg):
"""
Create a figure with specified aspect ratio. If *arg* is a number,
Expand Down
6 changes: 1 addition & 5 deletions lib/matplotlib/pyplot.py
Expand Up @@ -2087,12 +2087,8 @@ def polar(*args, **kwargs):
Multiple *theta*, *r* arguments are supported, with format
strings, as in :func:`~matplotlib.pyplot.plot`.
An optional kwarg *resolution* sets the number of vertices to
interpolate between each pair of points. The default is 1,
which disables interpolation.
"""
resolution = kwargs.pop('resolution', 1)
ax = gca(polar=True, resolution=resolution)
ax = gca(polar=True)
ret = ax.plot(*args, **kwargs)
draw_if_interactive()
return ret
Expand Down
7 changes: 3 additions & 4 deletions lib/matplotlib/tests/test_axes.py
@@ -1,7 +1,7 @@
import numpy as np
from numpy import ma
import matplotlib
from matplotlib.testing.decorators import image_comparison, knownfailureif
from matplotlib.testing.decorators import image_comparison, cleanup
import matplotlib.pyplot as plt


Expand Down Expand Up @@ -733,6 +733,7 @@ def test_scatter_plot():
ax = plt.axes()
ax.scatter([3, 4, 2, 6], [2, 5, 2, 3], c=['r', 'y', 'b', 'lime'], s=[24, 15, 19, 29])

@cleanup
def test_as_mpl_axes_api():
# tests the _as_mpl_axes api
from matplotlib.projections.polar import PolarAxes
Expand All @@ -755,9 +756,7 @@ def _as_mpl_axes(self):
assert type(ax) == PolarAxes, \
'Expected a PolarAxes, got %s' % type(ax)
ax_via_gca = plt.gca(projection=prj)
# ideally, ax_via_gca is ax should be true. However, gca isn't
# plummed like that. (even with projection='polar').
assert ax_via_gca is not ax
assert ax_via_gca is ax
plt.close()

# testing axes creation with gca
Expand Down
31 changes: 29 additions & 2 deletions lib/matplotlib/tests/test_figure.py
@@ -1,6 +1,6 @@
import matplotlib
from nose.tools import assert_equal
from matplotlib.testing.decorators import image_comparison, knownfailureif, cleanup
from nose.tools import assert_equal, assert_is, assert_is_not
from matplotlib.testing.decorators import image_comparison, cleanup
import matplotlib.pyplot as plt


Expand Down Expand Up @@ -38,3 +38,30 @@ def test_figure():
# Return to the original; make sure the red line is not there.
plt.figure('today')
plt.close('tomorrow')


#@cleanup
def test_gca():
fig = plt.figure()

ax1 = fig.add_axes([0, 0, 1, 1])
assert_is(fig.gca(projection='rectilinear'), ax1)
assert_is(fig.gca(), ax1)

ax2 = fig.add_subplot(121, projection='polar')
assert_is(fig.gca(), ax2)
assert_is(fig.gca(polar=True), ax2)

ax3 = fig.add_subplot(122)
assert_is(fig.gca(), ax3)

# the final request for a polar axes will end up creating one
# with a spec of 111.
assert_is_not(fig.gca(polar=True), ax3)
assert_is_not(fig.gca(polar=True), ax2)
assert_equal(fig.gca().get_geometry(), (1, 1, 1))

fig.sca(ax1)
assert_is(fig.gca(projection='rectilinear'), ax1)
assert_is(fig.gca(), ax1)

0 comments on commit dda4ab6

Please sign in to comment.