Ticket #1862: 1862.patch

File 1862.patch, 13.0 KB (added by mhansen, 14 years ago)
  • sage/plot/all.py

    # HG changeset patch
    # User Carl Witty <cwitty@newtonlabs.com>
    # Date 1205622861 25200
    # Node ID 07db6448af014fa62d9f89fd5a7bdfebf8ac40be
    # Parent  563d91fd399aed64639a1369cfb5dccb553c35ce
    Implicit plots (based on contour plots)
    
    diff -r 563d91fd399a -r 07db6448af01 sage/plot/all.py
    a b from plot import (Graphics, line, polygo 
    11from plot import (Graphics, line, polygon, plot, point, points,
    22                  text, circle, disk, hue, graphics_array,
    33                  list_plot, networkx_plot, parametric_plot,
    4                   polar_plot, contour_plot, arrow,
     4                  polar_plot, contour_plot, implicit_plot, arrow,
    55                  plot_vector_field, matrix_plot, bar_chart,
    66                  is_Graphics, rainbow,
    77                  show_default)
  • sage/plot/plot.py

    diff -r 563d91fd399a -r 07db6448af01 sage/plot/plot.py
    a b class GraphicPrimitive_ContourPlot(Graph 
    19951995                'cmap':"""The colormap, one of (autumn, bone, cool, copper,
    19961996                       gray, hot, hsv, jet, pink, prism, spring, summer, winter)""",
    19971997                       'fill':'Fill contours or not',
    1998                 'contours':'Number of contour levels.'}
     1998                'contours':"""Either an integer specifying the number of
     1999                       contour levels, or a sequence of numbers giving
     2000                       the actual contours to use."""}
    19992001
    20002002    def _repr_(self):
    20012003        return "ContourPlot defined by a %s x %s data grid"%(self.xy_array_row, self.xy_array_col)
    20022004
    20032005    def _render_on_subplot(self, subplot):
     2006        from sage.rings.integer import Integer
    20042007        options = self.options()
    20052008        fill = options['fill']
    20062009        cmap = options['cmap']
    class GraphicPrimitive_ContourPlot(Graph 
    20212024        if fill:
    20222025            if contours is None:
    20232026                subplot.contourf(self.xy_data_array, cmap=cmap, extent=(x0,x1,y0,y1))
    2024             else:
     2027            elif isinstance(contours, (int, Integer)):
    20252028                subplot.contourf(self.xy_data_array, int(contours), cmap=cmap, extent=(x0,x1,y0,y1))
     2029            else:
     2030                subplot.contourf(self.xy_data_array, contours, cmap=cmap, extent=(x0,x1,y0,y1))
    20262031        else:
    20272032            if contours is None:
    20282033                subplot.contour(self.xy_data_array, cmap=cmap, extent=(x0,x1,y0,y1))
    2029             else:
     2034            elif isinstance(contours, (int, Integer)):
    20302035                subplot.contour(self.xy_data_array, int(contours), cmap=cmap, extent=(x0,x1,y0,y1))
     2036            else:
     2037                subplot.contour(self.xy_data_array, contours, cmap=cmap, extent=(x0,x1,y0,y1))
    20312038
    20322039
    20332040class GraphicPrimitive_MatrixPlot(GraphicPrimitive):
    class ArrowFactory(GraphicPrimitiveFacto 
    27122719    def _reset(self):
    27132720        self.options={'width':0.02,'rgbcolor':(0, 0, 1)}
    27142721   
    2715     def _repr_(self):
     2722    def __repr__(self):
     2723        """
     2724        Returns a string representation of this ArrowFactory object.
     2725
     2726        TESTS:
     2727            sage: arrow
     2728            type arrow? for help and examples
     2729        """
    27162730        return "type arrow? for help and examples"
    27172731
    27182732    def _from_xdata_ydata(self, xmin, ymin, xmax, ymax, options):
    class BarChartFactory(GraphicPrimitiveFa 
    27412755    def _reset(self):
    27422756        self.options={'width':0.5,'rgbcolor':(0, 0, 1)}
    27432757   
    2744     def _repr_(self):
     2758    def __repr__(self):
     2759        """
     2760        Returns a string representation of this BarChartFactory object.
     2761
     2762        TESTS:
     2763            sage: bar_chart
     2764            type bar_chart? for help and examples
     2765        """
    27452766        return "type bar_chart? for help and examples"
    27462767
    27472768    def _from_xdata_ydata(self, ind, datalist, xrange, yrange, options):
    class CircleFactory(GraphicPrimitiveFact 
    28002821    def _reset(self):
    28012822        self.options={'alpha':1,'fill':False,'thickness':1,'rgbcolor':(0, 0, 1)}
    28022823   
    2803     def _repr_(self):
     2824    def __repr__(self):
     2825        """
     2826        Returns a string representation of this CircleFactory object.
     2827
     2828        TESTS:
     2829            sage: circle
     2830            type circle? for help and examples
     2831        """
    28042832        return "type circle? for help and examples"
    28052833
    28062834    def _from_xdata_ydata(self, point, r, options):
    class ContourPlotFactory(GraphicPrimitiv 
    28332861        cmap         -- string (default: 'gray'), the color map to use:
    28342862                        autumn, bone, cool, copper, gray, hot, hsv,
    28352863                        jet, pink, prism, spring, summer, winter
    2836         contours     -- integer (default: None), the number of contour
    2837                         lines to draw.  If None, determined automatically,
    2838                         and usually about 5.
     2864        contours     -- integer or list of numbers (default: None):
     2865                        If a list of numbers is given, then this specifies
     2866                        the contour levels to use.  If an integer is given,
     2867                        then this many contour lines are used, but the
     2868                        exact levels are determined automatically.
     2869                        If None is passed (or the option is not given),
     2870                        then the number of contour lines is determined
     2871                        automatically, and is usually about 5.
    28392872       
    28402873
    28412874    EXAMPLES:
    class ContourPlotFactory(GraphicPrimitiv 
    28612894        sage: contour_plot(y^2 + 1 - x^3 - x, (y,-pi,pi), (x,-pi,pi))
    28622895        sage: contour_plot(lambda x,y: y^2 + 1 - x^3 - x, (y,-pi,pi), (x,-pi,pi))
    28632896        sage: contour_plot(y^2 + 1 - x^3 - x, (-pi,pi), (-pi,pi))
    2864        
     2897
     2898
     2899    We can play with the contour levels.
     2900        sage: f = lambda x,y: x^2 + y^2
     2901        sage: contour_plot(f, (-2, 2), (-2, 2))
     2902        sage: contour_plot(f, (-2, 2), (-2, 2), contours=2)
     2903        sage: contour_plot(f, (-2, 2), (-2, 2), contours=(0.1, 1.0, 1.2, 1.4), cmap='hsv')
     2904        sage: contour_plot(f, (-2, 2), (-2, 2), contours=(1.0,), fill=False)
     2905
    28652906    """
    28662907    def _reset(self):
    28672908        self.options={'plot_points':25, 'fill':True, 'cmap':'gray', 'contours':None}
    28682909   
    2869     def _repr_(self):
     2910    def __repr__(self):
     2911        """
     2912        Returns a string representation of this ContourPlotFactory object.
     2913
     2914        TESTS:
     2915            sage: contour_plot
     2916            type contour_plot? for help and examples
     2917        """
    28702918        return "type contour_plot? for help and examples"
    2871 
     2919         
    28722920    def _from_xdata_ydata(self, xy_data_array, xrange, yrange, options):
    28732921        g = Graphics()
    28742922        g._contour_plot(xy_data_array, xrange, yrange, options)
    class ContourPlotFactory(GraphicPrimitiv 
    28762924
    28772925#unique contour_plot instance
    28782926contour_plot = ContourPlotFactory()
     2927
     2928class ImplicitPlotFactory(ContourPlotFactory):
     2929    r"""
     2930    \code{implicit_plot} takes a function of two variables, $f(x,y)$
     2931    and plots the curve $f(x,y)=0$ over the specified
     2932    xrange and yrange as demonstrated below.
     2933
     2934      implicit_plot(f, (xmin, xmax), (ymin, ymax), ...)
     2935
     2936    INPUT:
     2937        f -- a function of two variables
     2938        (xmin, xmax) -- 2-tuple, the range of x values
     2939        (ymin, ymax) -- 2-tuple, the range of y values
     2940    The following inputs must all be passed in as named parameters:
     2941        plot_points  -- integer (default: 25); number of points to plot
     2942                        in each direction of the grid
     2943       
     2944
     2945    EXAMPLES:
     2946
     2947    We can define a level-$n$ approximation of the boundary of the
     2948    Mandelbrot set.
     2949        sage: def mandel(n):
     2950        ...       c = polygen(CDF, 'c')
     2951        ...       z = 0
     2952        ...       for i in range(n):
     2953        ...           z = z*z + c
     2954        ...       def f(x, y):
     2955        ...           val = z(CDF(x, y))
     2956        ...           return val.norm() - 4
     2957        ...       return f
     2958
     2959    The first-level approximation is just a circle.
     2960        sage: implicit_plot(mandel(1), (-3, 3), (-3, 3)).show(aspect_ratio=1)
     2961       
     2962    A third-level approximation starts to get interesting.
     2963        sage: implicit_plot(mandel(3), (-2, 1), (-1.5, 1.5)).show(aspect_ratio=1)
     2964
     2965    The seventh-level approximation is a degree 64 polynomial, and
     2966    implicit_plot does a pretty good job on this part of the curve.
     2967    (plot_points=200 looks even better, but it's about 16 times slower.)
     2968        sage: implicit_plot(mandel(7), (-0.3, 0.05), (-1.15, -0.9),plot_points=50).show(aspect_ratio=1)
     2969    """
     2970    def _reset(self):
     2971        """
     2972        Sets the default options for this ImplicitPlotFactory object.
     2973
     2974        TESTS:
     2975            sage: implicit_plot._reset()
     2976            sage: implicit_plot.options['contours']
     2977            (0.0,)
     2978        """
     2979        self.options={'plot_points':25, 'fill':False, 'cmap':'gray', 'contours':(0.0,)}
     2980   
     2981    def __repr__(self):
     2982        """
     2983        Returns a string representation of this ImplicitPlotFactory object.
     2984
     2985        TESTS:
     2986            sage: implicit_plot
     2987            type implicit_plot? for help and examples
     2988        """
     2989        return "type implicit_plot? for help and examples"
     2990
     2991#unique implicit_plot instance
     2992implicit_plot = ImplicitPlotFactory()
    28792993
    28802994class LineFactory(GraphicPrimitiveFactory_from_point_list):
    28812995    r"""
    class LineFactory(GraphicPrimitiveFactor 
    29773091    def _reset(self):
    29783092        self.options = {'alpha':1,'rgbcolor':(0,0,1),'thickness':1}
    29793093   
    2980     def _repr_(self):
    2981         return "type line? for help and examples."
     3094    def __repr__(self):
     3095        """
     3096        Returns a string representation of this LineFactory object.
     3097
     3098        TESTS:
     3099            sage: line
     3100            type line? for help and examples
     3101        """
     3102        return "type line? for help and examples"
    29823103       
    29833104    def _from_xdata_ydata(self, xdata, ydata, coerce, options):
    29843105        if coerce:
    class MatrixPlotFactory(GraphicPrimitive 
    30263147    def _reset(self):
    30273148        self.options={'cmap':'gray'}
    30283149   
    3029     def _repr_(self):
     3150    def __repr__(self):
     3151        """
     3152        Returns a string representation of this MatrixPlotFactory object.
     3153
     3154        TESTS:
     3155            sage: matrix_plot
     3156            type matrix_plot? for help and examples
     3157        """
    30303158        return "type matrix_plot? for help and examples"
    30313159
    30323160    def _from_xdata_ydata(self, xy_data_array, xrange, yrange, options):
    class DiskFactory(GraphicPrimitiveFactor 
    31023230    def _reset(self):
    31033231        self.options={'alpha':1,'fill':True,'rgbcolor':(0,0,1),'thickness':0}
    31043232   
    3105     def _repr_(self):
     3233    def __repr__(self):
     3234        """
     3235        Returns a string representation of this DiskFactory object.
     3236
     3237        TESTS:
     3238            sage: disk
     3239            type disk? for help and examples
     3240        """
    31063241        return "type disk? for help and examples"
    31073242
    31083243    def _from_xdata_ydata(self, point, r, angle, options):
    class PointFactory(GraphicPrimitiveFacto 
    31313266    def _reset(self):
    31323267        self.options = {'alpha':1,'pointsize':10,'faceted':False,'rgbcolor':(0,0,1)}
    31333268
    3134     def _repr_(self):
    3135         return "type point? for options help"
     3269    def __repr__(self):
     3270        """
     3271        Returns a string representation of this PointFactory object.
     3272
     3273        TESTS:
     3274            sage: point
     3275            type point? for help and examples
     3276        """
     3277        return "type point? for help and examples"
    31363278
    31373279    def _from_xdata_ydata(self, xdata, ydata, coerce, options):
    31383280        if coerce:
    class PolygonFactory(GraphicPrimitiveFac 
    32143356    def _reset(self):
    32153357        self.options={'alpha':1,'rgbcolor':(0,0,1),'thickness':0}
    32163358   
    3217     def _repr_(self):
    3218         return "Sage polygon; type polygon? for help and examples."
     3359    def __repr__(self):
     3360        """
     3361        Returns a string representation of this PolygonFactory object.
     3362
     3363        TESTS:
     3364            sage: polygon
     3365            Sage polygon; type polygon? for help and examples
     3366        """
     3367        return "Sage polygon; type polygon? for help and examples"
    32193368       
    32203369    def _from_xdata_ydata(self, xdata, ydata, coerce, options):
    32213370        if coerce:
    class PlotFactory(GraphicPrimitiveFactor 
    33633512        o['max_bend'] = 0.1       
    33643513        o['rgbcolor'] = (0,0,1)   
    33653514
    3366     def _repr_(self):
    3367         return "plot; type plot? for help and examples."
     3515    def __repr__(self):
     3516        """
     3517        Returns a string representation of this PlotFactory object.
     3518
     3519        TESTS:
     3520            sage: plot
     3521            type plot? for help and examples
     3522        """
     3523        return "type plot? for help and examples"
    33683524
    33693525    def __call__(self, funcs, *args, **kwds):
    33703526        do_show = False
    class TextFactory(GraphicPrimitiveFactor 
    35453701                        'vertical_alignment':'center',
    35463702                        'axis_coords':False}
    35473703   
    3548     def _repr_(self):
     3704    def __repr__(self):
     3705        """
     3706        Returns a string representation of this TextFactory object.
     3707
     3708        TESTS:
     3709            sage: text
     3710            type text? for help and examples
     3711        """
    35493712        return "type text? for help and examples"
    35503713
    35513714    def _from_xdata_ydata(self, string, point, options):