Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix argument checks in axis/base.margins #3078

Merged
merged 3 commits into from May 28, 2014

Conversation

andreasWallner
Copy link
Contributor

I tried this code today when guessing parameter names:

import matplotlib.pyplot as plt
ax = plt.subplot(1,1,1)
ax.plot([1,10])
ax.margins(x=0.1, y=0)

Which should set the axis margins (according to the function docstring) but throws this error

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-33eb86e98b1d> in <module>()
      2 ax = plt.subplot(1,1,1)
      3 ax.plot([1,10])
----> 4 ax.margins(x=0.1, y=0)
      5 

C:\Python33\lib\site-packages\matplotlib\axes.py in margins(self, *args, **kw)
   1840             mx, my = args
   1841         else:
-> 1842             raise ValueError("more than two arguments were supplied")
   1843         if mx is not None:
   1844             self.set_xmargin(mx)

ValueError: more than two arguments were supplied

After looking at the code it seems to me that the keyword parameters where simply forgotten in the checks.
This patch fixes this problem and provides test cases for all call variants of margins.

PS: I hope I did not add too many pictures for such a simple test, but I did not find a way to check for the same picture twice (margins_1_05_arg.png & margins_1_05_kwarg.png).

If keyword arguments where used, like mentioned in the documentation, the code would fail as
if more than two parameters where given (since len(args) would be 0 -> != 1/2)
@@ -3109,6 +3109,25 @@ def test_pie_ccw_true():
# Set aspect ratio to be equal so that pie is drawn as a circle.
plt.axis('equal')

@image_comparison(baseline_images=['margins_1', 'margins_1_05_arg', 'margins_1_05_kwarg'], extensions=['png'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to create images for these tests - the actual testing is being done in the assertions. As a result, we should just be able to remove this decorator, and replace it with the cleanup one.

@andreasWallner
Copy link
Contributor Author

You are right, it seems I was a little bit too eager there^^, fixed

@@ -3109,6 +3109,24 @@ def test_pie_ccw_true():
# Set aspect ratio to be equal so that pie is drawn as a circle.
plt.axis('equal')

def test_margins():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a @cleanup to make sure we don't like figures in the tests.

@tacaswell tacaswell added this to the v1.4.0 milestone May 20, 2014
pelson added a commit that referenced this pull request May 28, 2014
@pelson pelson merged commit da518ba into matplotlib:master May 28, 2014
@pelson
Copy link
Member

pelson commented May 28, 2014

Thanks @andreasWallner!

@andreasWallner andreasWallner deleted the fix_axis_margins_call_args branch May 29, 2014 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants