from contextlib import contextmanager
import os
import tempfile
import unittest

from matplotlib.pyplot import Artist, savefig
from shapely.geometry import Polygon, LineString, Point

from geopandas import GeoSeries


@contextmanager
def get_tempfile():
    f, path = tempfile.mkstemp()
    try:
        yield path
    finally:
        try:
            os.remove(path)
        except:
            pass

class TestSeriesPlot(unittest.TestCase):

    def setUp(self):
        self.t1 = Polygon([(0, 0), (1, 0), (1, 1)])
        self.t2 = Polygon([(1, 0), (2, 1), (2, 1)])
        self.polys = GeoSeries([self.t1, self.t2])

    def test_poly_plot(self):
        """ Test plotting a simple series of polygons """
        ax = self.polys.plot()
        self.assertIsInstance(ax, Artist)
        with get_tempfile() as file:
            savefig(file)

if __name__ == '__main__':
    unittest.main()

import os
import unittest

from matplotlib.pyplot import Artist, savefig
from matplotlib.testing.decorators import image_comparison
from shapely.geometry import Polygon, LineString, Point

from geopandas import GeoSeries

# If set to True, generate images rather than perform tests (all tests will pass!)
GENERATE_BASELINE = False

BASELINE_DIR = os.path.join(os.path.dirname(__file__), 'baseline_images', 'test_plotting')

def save_baseline_image(filename):
    """ save a baseline image """
    savefig(os.path.join(BASELINE_DIR, filename))

@image_comparison(baseline_images=['poly_plot'], extensions=['png'])
def test_poly_plot():
    """ Test plotting a simple series of polygons """
    t1 = Polygon([(0, 0), (1, 0), (1, 1)])
    t2 = Polygon([(1, 0), (2, 1), (2, 1)])
    polys = GeoSeries([t1, t2])
    ax = polys.plot()
    assert isinstance(ax, Artist)
    if GENERATE_BASELINE:
        save_baseline_image('poly_plot.png')

if __name__ == '__main__':
    import nose
    nose.runmodule(argv=['-s', '--with-doctest'])

Use matplotlib image_comparison to actually compare plot output
TST: Use matplotlib image_comparison to actually compare plot output

- from contextlib import contextmanager
  import os
- import tempfile
  import unittest
  
  from matplotlib.pyplot import Artist, savefig
+ from matplotlib.testing.decorators import image_comparison
  from shapely.geometry import Polygon, LineString, Point
  
  from geopandas import GeoSeries
  
+ # If set to True, generate images rather than perform tests (all tests will pass!)
+ GENERATE_BASELINE = False
  
+ BASELINE_DIR = os.path.join(os.path.dirname(__file__), 'baseline_images', 'test_plotting')
- @contextmanager
- def get_tempfile():
-     f, path = tempfile.mkstemp()
-     try:
-         yield path
-     finally:
-         try:
-             os.remove(path)
-         except:
-             pass
  
- class TestSeriesPlot(unittest.TestCase):
+ def save_baseline_image(filename):
+     """ save a baseline image """
+     savefig(os.path.join(BASELINE_DIR, filename))
  
+ @image_comparison(baseline_images=['poly_plot'], extensions=['png'])
-     def setUp(self):
-         self.t1 = Polygon([(0, 0), (1, 0), (1, 1)])
-         self.t2 = Polygon([(1, 0), (2, 1), (2, 1)])
-         self.polys = GeoSeries([self.t1, self.t2])
- 
-     def test_poly_plot(self):
+ def test_poly_plot():
-         """ Test plotting a simple series of polygons """
+     """ Test plotting a simple series of polygons """
+     t1 = Polygon([(0, 0), (1, 0), (1, 1)])
+     t2 = Polygon([(1, 0), (2, 1), (2, 1)])
+     polys = GeoSeries([t1, t2])
-         ax = self.polys.plot()
+     ax = polys.plot()
-         self.assertIsInstance(ax, Artist)
+     assert isinstance(ax, Artist)
-         with get_tempfile() as file:
-             savefig(file)
+     if GENERATE_BASELINE:
+         save_baseline_image('poly_plot.png')
  
  if __name__ == '__main__':
-     unittest.main()
+     import nose
+     nose.runmodule(argv=['-s', '--with-doctest'])
  
Use matplotlib image_comparison to actually compare plot output
## Code Before:
from contextlib import contextmanager
import os
import tempfile
import unittest

from matplotlib.pyplot import Artist, savefig
from shapely.geometry import Polygon, LineString, Point

from geopandas import GeoSeries


@contextmanager
def get_tempfile():
    f, path = tempfile.mkstemp()
    try:
        yield path
    finally:
        try:
            os.remove(path)
        except:
            pass

class TestSeriesPlot(unittest.TestCase):

    def setUp(self):
        self.t1 = Polygon([(0, 0), (1, 0), (1, 1)])
        self.t2 = Polygon([(1, 0), (2, 1), (2, 1)])
        self.polys = GeoSeries([self.t1, self.t2])

    def test_poly_plot(self):
        """ Test plotting a simple series of polygons """
        ax = self.polys.plot()
        self.assertIsInstance(ax, Artist)
        with get_tempfile() as file:
            savefig(file)

if __name__ == '__main__':
    unittest.main()

## Instruction:
Use matplotlib image_comparison to actually compare plot output
## Code After:
import os
import unittest

from matplotlib.pyplot import Artist, savefig
from matplotlib.testing.decorators import image_comparison
from shapely.geometry import Polygon, LineString, Point

from geopandas import GeoSeries

# If set to True, generate images rather than perform tests (all tests will pass!)
GENERATE_BASELINE = False

BASELINE_DIR = os.path.join(os.path.dirname(__file__), 'baseline_images', 'test_plotting')

def save_baseline_image(filename):
    """ save a baseline image """
    savefig(os.path.join(BASELINE_DIR, filename))

@image_comparison(baseline_images=['poly_plot'], extensions=['png'])
def test_poly_plot():
    """ Test plotting a simple series of polygons """
    t1 = Polygon([(0, 0), (1, 0), (1, 1)])
    t2 = Polygon([(1, 0), (2, 1), (2, 1)])
    polys = GeoSeries([t1, t2])
    ax = polys.plot()
    assert isinstance(ax, Artist)
    if GENERATE_BASELINE:
        save_baseline_image('poly_plot.png')

if __name__ == '__main__':
    import nose
    nose.runmodule(argv=['-s', '--with-doctest'])
