Skip to content

Commit

Permalink
Fix saving of plots with theme_xkcd
Browse files Browse the repository at this point in the history
Also made the plot saving tests less verbose.

fixes #199
  • Loading branch information
has2k1 committed Nov 20, 2018
1 parent b5db761 commit 5ffdaea
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
3 changes: 3 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Bug Fixes
- Fixed bug where :class:`~plotnine.coords.coord_flp` would not flip
geoms created by :class:`~plotnine.geoms.geom_rug` (:issue:`216`).

- Fixed bug where plots with :class:`~plotnine.themes.theme_xkcd` cannot be
saved twice (:issue:`199`)

Enhancements
************

Expand Down
4 changes: 2 additions & 2 deletions plotnine/ggplot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import os
import sys
from copy import copy, deepcopy
from copy import deepcopy
from contextlib import suppress
from warnings import warn

Expand Down Expand Up @@ -668,7 +668,7 @@ def save(self, filename=None, format=None, path=None,
filename = os.path.join(path, filename)

# Preserve the users object
self = copy(self)
self = deepcopy(self)

# theme
self.theme = self.theme or theme_get()
Expand Down
42 changes: 33 additions & 9 deletions plotnine/tests/test_ggsave.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import matplotlib.pyplot as plt
import pytest

from plotnine import ggplot, aes, geom_text, ggsave
import pandas as pd
from plotnine import (ggplot, aes, geom_text, geom_point, facet_wrap,
ggsave, theme_xkcd)
from plotnine.data import mtcars
from plotnine.exceptions import PlotnineError, PlotnineWarning

Expand Down Expand Up @@ -35,7 +37,7 @@ def assert_exist_and_clean(filename, msg=None):

class TestArguments:
def test_default_filename(self):
p.save()
p.save(verbose=False)
fn = p._save_filename('pdf')
assert_exist_and_clean(fn, "default filename")

Expand Down Expand Up @@ -70,21 +72,21 @@ def test_save_method(self):

def test_filename_plot_path(self):
fn = next(filename_gen)
p.save(fn, path='.')
p.save(fn, path='.', verbose=False)
assert_exist_and_clean(fn, "fn, plot and path")

def test_format_png(self):
p.save(format='png')
p.save(format='png', verbose=False)
fn = p._save_filename('png')
assert_exist_and_clean(fn, "format png")

def test_dpi(self):
fn = next(filename_gen)
p.save(fn, dpi=100)
p.save(fn, dpi=100, verbose=False)
assert_exist_and_clean(fn, "dpi = 100")

def test_ggsave(self):
ggsave(p)
ggsave(p, verbose=False)
fn = p._save_filename('pdf')
assert_exist_and_clean(fn, "default filename")

Expand All @@ -93,14 +95,36 @@ def test_save_big(self):
# supplying the ggplot object will work without
# printing it first! 26 is the current limit, just go
# over it to not use too much memory
p.save(fn, width=26, height=26, limitsize=False)
p.save(fn, width=26, height=26, limitsize=False, verbose=False)
assert_exist_and_clean(fn, "big height and width")

def test_dpi_theme_xkcd(self):
fn1 = next(filename_gen)
fn2 = next(filename_gen)

df = pd.DataFrame({
'x': range(4),
'y': range(4),
'b': list('aabb')
})

p = (
ggplot(df)
+ geom_point(aes('x', 'y'))
+ facet_wrap('b')
+ theme_xkcd()
)
p.save(fn1, verbose=False)
assert_exist_and_clean(fn1, "Saving with theme_xkcd and dpi (1)")

p.save(fn2, dpi=72, verbose=False)
assert_exist_and_clean(fn2, "Saving with theme_xkcd and dpi (2)")


class TestExceptions:
def test_unknown_format(self):
with pytest.raises(Exception):
p.save(format='unknown')
p.save(format='unknown', verbose=False)

def test_width_only(self):
with pytest.raises(PlotnineError):
Expand Down Expand Up @@ -128,6 +152,6 @@ def test_bad_units(self):
def test_ggsave_closes_plot():
assert plt.get_fignums() == [], "There are unsaved test plots"
fn = next(filename_gen)
p.save(fn)
p.save(fn, verbose=False)
assert_exist_and_clean(fn, "exist")
assert plt.get_fignums() == [], "ggplot.save did not close the plot"

0 comments on commit 5ffdaea

Please sign in to comment.