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

Margins removed in baseline #49

Open
pylang opened this issue Apr 18, 2017 · 4 comments
Open

Margins removed in baseline #49

pylang opened this issue Apr 18, 2017 · 4 comments
Milestone

Comments

@pylang
Copy link

pylang commented Apr 18, 2017

When I run or save a modified README code in Jupyter, I get a different image than the generated baseline.

Example code run in a Jupyter cell:

#%%file test.py
%matplotlib inline

import matplotlib.pyplot as plt
import pytest

@pytest.mark.mpl_image_compare
def test_succeeds():
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    ax.plot([1,2,3])
    fig.savefig("test")
    return fig

Running test_succeeds(); in Jupyter gives resulting image and saved test.png with margins:

test

Next, I generate a test.py file by uncommenting the first line, commenting the second line
and then running the cell

%%file test.py
#%matplotlib inline

...

From a command prompt, in the appropriate directory I run the following to generate the baseline image:

> py.test --mpl-generate-path=baseline test.py

In the baseline folder, a comparable image called test_succeeds.png is created without margins:

test_succeeds

Aside from the difference in sizes, I notice the baseline image uses the classic mpl style. Perhaps this is the cause for the absence in margins. I report this as an issue as it is ideal that when testing the baseline image, it should be the same image as shown and saved by Jupyter.

2017-04-18 16_25_19-scratchpad 5

I tested this with Anaconda 4.2.

@astrofrog
Copy link
Collaborator

Do you have a matplotlibrc file in ~/.matplotlib? I wonder if you have the bbox_inches='tight' option set by default?

@pylang
Copy link
Author

pylang commented Apr 18, 2017

No, I don't have any customization in that file. I tried that latter with fig.tight_layout() as well. I got the same results.

I am unable to confirm this with that latest version of matplotlib (due to this issue while upgrading). I am interested if someone can reproduce these results with matplotlib 2.0.0.

@ConorMacBride
Copy link
Member

I believe the differences are due to pytest-mpl using the classic style by default. (Matplotlib changed their default style in v2.0) To use Matplotlib's current default style in pytest-mpl, test functions should be decorated with @pytest.mark.mpl_image_compare(style="default") instead.


Most new users today would probably prefer to use Matplotlib's current default style, so maybe we should consider only setting the style if it's explicitly passed to the pytest-mpl decorator? (This is assuming the default style remains stable between major releases of Matplotlib.) This could be a breaking change in pytest-mpl v1.0.0.

@ConorMacBride ConorMacBride added this to the 1.0.0 milestone Jul 5, 2022
@dopplershift
Copy link
Contributor

Jupyter by default uses bbox_inches='tight' with the matplotlib inline support.

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

No branches or pull requests

4 participants