plot_utils: fix setting the size of the plots
Longstanding bug, finally fixed. Merge set_plot_size() into
pre_plot_setup() in the process as it's the only thing that it does.
diff --git a/cr2/plot_utils.py b/cr2/plot_utils.py
index 092ec8e..a9fa9da 100644
--- a/cr2/plot_utils.py
+++ b/cr2/plot_utils.py
@@ -5,23 +5,6 @@
GOLDEN_RATIO = 1.618034
-def set_plot_size(width, height):
- """Set the plot size.
-
- This has to be called before calls to .plot()
- """
- if height is None:
- if width is None:
- height = 6
- width = 10
- else:
- height = width / GOLDEN_RATIO
- else:
- if width is None:
- width = height * GOLDEN_RATIO
-
- plt.figure(figsize=(width, height))
-
def normalize_title(title, opt_title):
"""
Return a string with that contains the title and opt_title if it's not the empty string
@@ -70,13 +53,22 @@
def pre_plot_setup(width=None, height=None):
"""initialize a figure
- width and height are numbers This function should be called before
- any calls to plot()
+ width and height are numbers. This function should be called
+ before any calls to plot()
+
"""
- set_plot_size(width, height)
+ if height is None:
+ if width is None:
+ height = 6
+ width = 10
+ else:
+ height = width / GOLDEN_RATIO
+ else:
+ if width is None:
+ width = height * GOLDEN_RATIO
- _, ax = plt.subplots()
+ _, ax = plt.subplots(figsize=(width, height))
return ax
diff --git a/tests/test_plot_utils.py b/tests/test_plot_utils.py
index c6a1163..31b7094 100644
--- a/tests/test_plot_utils.py
+++ b/tests/test_plot_utils.py
@@ -7,13 +7,6 @@
import plot_utils
class TestPlotUtils(unittest.TestCase):
- def test_set_plot_size(self):
- """Test that plot_utils.set_plot_size() doesn't bomb"""
- plot_utils.set_plot_size(None, None)
- plot_utils.set_plot_size(height=9, width=None)
- plot_utils.set_plot_size(height=None, width=9)
- plot_utils.set_plot_size(3, 9)
-
def test_normalize_title(self):
"""Test normalize_title"""
self.assertEquals(plot_utils.normalize_title("Foo", ""), "Foo")
@@ -64,6 +57,13 @@
plot_utils.set_xlim(ax, "default")
plot_utils.set_xlim(ax, (0, 5))
+ def test_pre_plot_setup(self):
+ """Test that plot_utils.pre_plot_setup() doesn't bomb"""
+ plot_utils.pre_plot_setup(None, None)
+ plot_utils.pre_plot_setup(height=9, width=None)
+ plot_utils.pre_plot_setup(height=None, width=9)
+ plot_utils.pre_plot_setup(3, 9)
+
def test_post_plot_setup(self):
"""Test that post_plot_setup() doesn't bomb"""