]> gitweb.michael.orlitzky.com - sage.d.git/blobdiff - mjo/plot.py
Update existing tests to use the codim() function.
[sage.d.git] / mjo / plot.py
index 0eaf079fa5343bed2b32d6747bf0ab6f69995ed6..21bd24cfa1a0691a48c6cfb494b65978bdb1df41 100644 (file)
@@ -1,38 +1,62 @@
+"""
+Plotting helpers.
+
+ * We set the default tick_formatter to 'latex'.
+
+ * We replace the plot() function with our own version that sets some
+   default legend options.
+
+"""
+
 from sage.all import *
 
-# Save the default so that we can call it within mjo_plot.
-sage_plot = plot
+# Graphics.SHOW_OPTIONS contains the default options that will be
+# passed to show(). It already handles merging the user-specified
+# options, so we just tap into that power.
+show_opts = sage.plot.graphics.Graphics.SHOW_OPTIONS
+show_opts['tick_formatter'] = 'latex'
+
+# Save the original implementation so that we can call it from
+# within mjo_plot.
+plot_impl = sage.plot.plot._plot
+list_plot_impl = sage.plot.plot.list_plot
 
 def mjo_plot(*args, **kwargs):
     """
     Replacement for the default plot function.
 
-     - Use the 'latex' tick formatter.
-
      - If there's a legend, set the background color to 'white' and
        give it a drop shadow.
 
     """
-    plot_opts = { 'tick_formatter': 'latex' }
+    default_legend_opts = { 'back_color': 'white',
+                            'shadow': True }
+
+    p = plot_impl(*args, **kwargs)
+    p.set_legend_options(**default_legend_opts)
+
+    return p
 
-    # Merge the user's plot options with mine. The ones given as
-    # kwargs should override the defaults!
-    plot_opts.update(kwargs)
-    kwargs = plot_opts
 
-    p = sage_plot(*args, **kwargs)
+def mjo_list_plot(*args, **kwargs):
+    """
+    Replacement for the default list_plot function.
 
-    # Merge the user's legend options with mine. The ones passed to us
-    # should override the defaults!
+     - If there's a legend, set the background color to 'white' and
+       give it a drop shadow.
+
+    """
     default_legend_opts = { 'back_color': 'white',
                             'shadow': True }
-    default_legend_opts.update(p._Graphics__legend_opts)
-    p._Graphics__legend_opts = default_legend_opts
+
+    p = list_plot_impl(*args, **kwargs)
+    p.set_legend_options(**default_legend_opts)
 
     return p
 
-# Replace both the global `plot` and the ones in the modules. I am
-# unclear about why this is necessary, and don't care too much.
-sage.plot.plot.plot = mjo_plot
-sage.plot.all.plot = mjo_plot
-plot = mjo_plot
+
+# This way, we don't have to try to replace all of the calls to plot()
+# and list_plot(); we just replace the two function that did the
+# actual implementations.
+sage.plot.plot._plot = mjo_plot
+sage.plot.plot.list_plot = mjo_list_plot