Ticket #1862: implicit-plot.patch

File implicit-plot.patch, 13.5 KB (added by cwitty, 14 years ago)
  • sage/plot/all.py

    # HG changeset patch
    # User Carl Witty <cwitty@newtonlabs.com>
    # Date 1204482427 28800
    # Node ID 4226b07c8ce6e464ae949f2ef451364576066c84
    # Parent  cdd0eb9b06e4b2c3949e452700938cc5afefca12
    Implicit plots (based on contour plots)
    
    diff -r cdd0eb9b06e4 -r 4226b07c8ce6 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 cdd0eb9b06e4 -r 4226b07c8ce6 sage/plot/plot.py
    a b class GraphicPrimitive_ContourPlot(Graph 
    19911991                'cmap':"""The colormap, one of (autumn, bone, cool, copper,
    19921992                       gray, hot, hsv, jet, pink, prism, spring, summer, winter)""",
    19931993                       'fill':'Fill contours or not',
    1994                 'contours':'Number of contour levels.'}
     1994                'contours':"""Either an integer specifying the number of
     1995                       contour levels, or a sequence of numbers giving
     1996                       the actual contours to use."""}
    19951997
    19961998    def _repr_(self):
    19971999        return "ContourPlot defined by a %s x %s data grid"%(self.xy_array_row, self.xy_array_col)
    19982000
    19992001    def _render_on_subplot(self, subplot):
     2002        from sage.rings.integer import Integer
    20002003        options = self.options()
    20012004        fill = options['fill']
    20022005        cmap = options['cmap']
    class GraphicPrimitive_ContourPlot(Graph 
    20172020        if fill:
    20182021            if contours is None:
    20192022                subplot.contourf(self.xy_data_array, cmap=cmap, extent=(x0,x1,y0,y1))
    2020             else:
     2023            elif isinstance(contours, (int, Integer)):
    20212024                subplot.contourf(self.xy_data_array, int(contours), cmap=cmap, extent=(x0,x1,y0,y1))
     2025            else:
     2026                subplot.contourf(self.xy_data_array, contours, cmap=cmap, extent=(x0,x1,y0,y1))
    20222027        else:
    20232028            if contours is None:
    20242029                subplot.contour(self.xy_data_array, cmap=cmap, extent=(x0,x1,y0,y1))
    2025             else:
     2030            elif isinstance(contours, (int, Integer)):
    20262031                subplot.contour(self.xy_data_array, int(contours), cmap=cmap, extent=(x0,x1,y0,y1))
     2032            else:
     2033                subplot.contour(self.xy_data_array, contours, cmap=cmap, extent=(x0,x1,y0,y1))
    20272034
    20282035
    20292036class GraphicPrimitive_MatrixPlot(GraphicPrimitive):
    class ArrowFactory(GraphicPrimitiveFacto 
    27112718    def _reset(self):
    27122719        self.options={'width':0.02,'rgbcolor':(0, 0, 1)}
    27132720   
    2714     def _repr_(self):
     2721    def __repr__(self):
     2722        """
     2723        Returns a string representation of this ArrowFactory object.
     2724
     2725        TESTS:
     2726            sage: arrow
     2727            type arrow? for help and examples
     2728        """
    27152729        return "type arrow? for help and examples"
    27162730
    27172731    def _from_xdata_ydata(self, xmin, ymin, xmax, ymax, options):
    class BarChartFactory(GraphicPrimitiveFa 
    27402754    def _reset(self):
    27412755        self.options={'width':0.5,'rgbcolor':(0, 0, 1)}
    27422756   
    2743     def _repr_(self):
     2757    def __repr__(self):
     2758        """
     2759        Returns a string representation of this BarChartFactory object.
     2760
     2761        TESTS:
     2762            sage: bar_chart
     2763            type bar_chart? for help and examples
     2764        """
    27442765        return "type bar_chart? for help and examples"
    27452766
    27462767    def _from_xdata_ydata(self, ind, datalist, xrange, yrange, options):
    class CircleFactory(GraphicPrimitiveFact 
    27992820    def _reset(self):
    28002821        self.options={'alpha':1,'fill':False,'thickness':1,'rgbcolor':(0, 0, 1)}
    28012822   
    2802     def _repr_(self):
     2823    def __repr__(self):
     2824        """
     2825        Returns a string representation of this CircleFactory object.
     2826
     2827        TESTS:
     2828            sage: circle
     2829            type circle? for help and examples
     2830        """
    28032831        return "type circle? for help and examples"
    28042832
    28052833    def _from_xdata_ydata(self, point, r, options):
    class ContourPlotFactory(GraphicPrimitiv 
    28322860        cmap         -- string (default: 'gray'), the color map to use:
    28332861                        autumn, bone, cool, copper, gray, hot, hsv,
    28342862                        jet, pink, prism, spring, summer, winter
    2835         contours     -- integer (default: None), the number of contour
    2836                         lines to draw.  If None, determined automatically,
    2837                         and usually about 5.
     2863        contours     -- integer or list of numbers (default: None):
     2864                        If a list of numbers is given, then this specifies
     2865                        the contour levels to use.  If an integer is given,
     2866                        then this many contour lines are used, but the
     2867                        exact levels are determined automatically.
     2868                        If None is passed (or the option is not given),
     2869                        then the number of contour lines is determined
     2870                        automatically, and is usually about 5.
    28382871       
    28392872
    28402873    EXAMPLES:
    class ContourPlotFactory(GraphicPrimitiv 
    28522885    An even more complicated plot.
    28532886        sage: f = lambda x,y: sin(x^2 + y^2)*cos(x)*sin(y)
    28542887        sage: contour_plot(f, (-4, 4), (-4, 4),plot_points=100)
     2888
     2889    We can play with the contour levels.
     2890        sage: f = lambda x,y: x^2 + y^2
     2891        sage: contour_plot(f, (-2, 2), (-2, 2))
     2892        sage: contour_plot(f, (-2, 2), (-2, 2), contours=2)
     2893        sage: contour_plot(f, (-2, 2), (-2, 2), contours=(0.1, 1.0, 1.2, 1.4), cmap='hsv')
     2894        sage: contour_plot(f, (-2, 2), (-2, 2), contours=(1.0,), fill=False)
    28552895    """
    28562896    def _reset(self):
    28572897        self.options={'plot_points':25, 'fill':True, 'cmap':'gray', 'contours':None}
    28582898   
    2859     def _repr_(self):
     2899    def __repr__(self):
     2900        """
     2901        Returns a string representation of this ContourPlotFactory object.
     2902
     2903        TESTS:
     2904            sage: contour_plot
     2905            type contour_plot? for help and examples
     2906        """
    28602907        return "type contour_plot? for help and examples"
    28612908
    28622909    def _from_xdata_ydata(self, xy_data_array, xrange, yrange, options):
    class ContourPlotFactory(GraphicPrimitiv 
    28662913
    28672914#unique contour_plot instance
    28682915contour_plot = ContourPlotFactory()
     2916
     2917class ImplicitPlotFactory(ContourPlotFactory):
     2918    r"""
     2919    \code{implicit_plot} takes a function of two variables, $f(x,y)$
     2920    and plots the curve $f(x,y)=0$ over the specified
     2921    xrange and yrange as demonstrated below.
     2922
     2923      implicit_plot(f, (xmin, xmax), (ymin, ymax), ...)
     2924
     2925    INPUT:
     2926        f -- a function of two variables
     2927        (xmin, xmax) -- 2-tuple, the range of x values
     2928        (ymin, ymax) -- 2-tuple, the range of y values
     2929    The following inputs must all be passed in as named parameters:
     2930        plot_points  -- integer (default: 25); number of points to plot
     2931                        in each direction of the grid
     2932       
     2933
     2934    EXAMPLES:
     2935
     2936    We can define a level-$n$ approximation of the boundary of the
     2937    Mandelbrot set.
     2938        sage: def mandel(n):
     2939        ...       c = polygen(CDF, 'c')
     2940        ...       z = 0
     2941        ...       for i in range(n):
     2942        ...           z = z*z + c
     2943        ...       def f(x, y):
     2944        ...           val = z(CDF(x, y))
     2945        ...           return val.norm() - 4
     2946        ...       return f
     2947
     2948    The first-level approximation is just a circle.
     2949        sage: implicit_plot(mandel(1), (-3, 3), (-3, 3)).show(aspect_ratio=1)
     2950       
     2951    A third-level approximation starts to get interesting.
     2952        sage: implicit_plot(mandel(3), (-2, 1), (-1.5, 1.5)).show(aspect_ratio=1)
     2953
     2954    The seventh-level approximation is a degree 64 polynomial, and
     2955    implicit_plot does a pretty good job on this part of the curve.
     2956    (plot_points=200 looks even better, but it's about 16 times slower.)
     2957        sage: implicit_plot(mandel(7), (-0.3, 0.05), (-1.15, -0.9),plot_points=50).show(aspect_ratio=1)
     2958    """
     2959    def _reset(self):
     2960        """
     2961        Sets the default options for this ImplicitPlotFactory object.
     2962
     2963        TESTS:
     2964            sage: implicit_plot._reset()
     2965            sage: implicit_plot.options['contours']
     2966            (0.0,)
     2967        """
     2968        self.options={'plot_points':25, 'fill':False, 'cmap':'gray', 'contours':(0.0,)}
     2969   
     2970    def __repr__(self):
     2971        """
     2972        Returns a string representation of this ImplicitPlotFactory object.
     2973
     2974        TESTS:
     2975            sage: implicit_plot
     2976            type implicit_plot? for help and examples
     2977        """
     2978        return "type implicit_plot? for help and examples"
     2979
     2980#unique implicit_plot instance
     2981implicit_plot = ImplicitPlotFactory()
    28692982
    28702983class LineFactory(GraphicPrimitiveFactory_from_point_list):
    28712984    r"""
    class LineFactory(GraphicPrimitiveFactor 
    29673080    def _reset(self):
    29683081        self.options = {'alpha':1,'rgbcolor':(0,0,1),'thickness':1}
    29693082   
    2970     def _repr_(self):
    2971         return "type line? for help and examples."
     3083    def __repr__(self):
     3084        """
     3085        Returns a string representation of this LineFactory object.
     3086
     3087        TESTS:
     3088            sage: line
     3089            type line? for help and examples
     3090        """
     3091        return "type line? for help and examples"
    29723092       
    29733093    def _from_xdata_ydata(self, xdata, ydata, coerce, options):
    29743094        if coerce:
    class MatrixPlotFactory(GraphicPrimitive 
    30163136    def _reset(self):
    30173137        self.options={'cmap':'gray'}
    30183138   
    3019     def _repr_(self):
     3139    def __repr__(self):
     3140        """
     3141        Returns a string representation of this MatrixPlotFactory object.
     3142
     3143        TESTS:
     3144            sage: matrix_plot
     3145            type matrix_plot? for help and examples
     3146        """
    30203147        return "type matrix_plot? for help and examples"
    30213148
    30223149    def _from_xdata_ydata(self, xy_data_array, xrange, yrange, options):
    class PlotFieldFactory(GraphicPrimitiveF 
    30503177    def _reset(self):
    30513178        self.options={'plot_points':20, 'cmap':'gray'}
    30523179   
    3053     def _repr_(self):
    3054         return "type contour_plot? for help and examples"
     3180    def __repr__(self):
     3181        """
     3182        Returns a string representation of this PlotFieldFactory object.
     3183
     3184        TESTS:
     3185            sage: plot_vector_field
     3186            type plot_vector_field? for help and examples
     3187        """
     3188        return "type plot_vector_field? for help and examples"
    30553189
    30563190    def _from_xdata_ydata(self, xpos_array, ypos_array, xvec_array, yvec_array, xrange, yrange, options):
    30573191        g = Graphics()
    class DiskFactory(GraphicPrimitiveFactor 
    30833217    def _reset(self):
    30843218        self.options={'alpha':1,'fill':True,'rgbcolor':(0,0,1),'thickness':0}
    30853219   
    3086     def _repr_(self):
     3220    def __repr__(self):
     3221        """
     3222        Returns a string representation of this DiskFactory object.
     3223
     3224        TESTS:
     3225            sage: disk
     3226            type disk? for help and examples
     3227        """
    30873228        return "type disk? for help and examples"
    30883229
    30893230    def _from_xdata_ydata(self, point, r, angle, options):
    class PointFactory(GraphicPrimitiveFacto 
    31123253    def _reset(self):
    31133254        self.options = {'alpha':1,'pointsize':10,'faceted':False,'rgbcolor':(0,0,1)}
    31143255
    3115     def _repr_(self):
    3116         return "type point? for options help"
     3256    def __repr__(self):
     3257        """
     3258        Returns a string representation of this PointFactory object.
     3259
     3260        TESTS:
     3261            sage: point
     3262            type point? for help and examples
     3263        """
     3264        return "type point? for help and examples"
    31173265
    31183266    def _from_xdata_ydata(self, xdata, ydata, coerce, options):
    31193267        if coerce:
    class PolygonFactory(GraphicPrimitiveFac 
    31953343    def _reset(self):
    31963344        self.options={'alpha':1,'rgbcolor':(0,0,1),'thickness':0}
    31973345   
    3198     def _repr_(self):
    3199         return "Sage polygon; type polygon? for help and examples."
     3346    def __repr__(self):
     3347        """
     3348        Returns a string representation of this PolygonFactory object.
     3349
     3350        TESTS:
     3351            sage: polygon
     3352            Sage polygon; type polygon? for help and examples
     3353        """
     3354        return "Sage polygon; type polygon? for help and examples"
    32003355       
    32013356    def _from_xdata_ydata(self, xdata, ydata, coerce, options):
    32023357        if coerce:
    class PlotFactory(GraphicPrimitiveFactor 
    33443499        o['max_bend'] = 0.1       
    33453500        o['rgbcolor'] = (0,0,1)   
    33463501
    3347     def _repr_(self):
    3348         return "plot; type plot? for help and examples."
     3502    def __repr__(self):
     3503        """
     3504        Returns a string representation of this PlotFactory object.
     3505
     3506        TESTS:
     3507            sage: plot
     3508            type plot? for help and examples
     3509        """
     3510        return "type plot? for help and examples"
    33493511
    33503512    def __call__(self, funcs, *args, **kwds):
    33513513        do_show = False
    class TextFactory(GraphicPrimitiveFactor 
    35263688                        'vertical_alignment':'center',
    35273689                        'axis_coords':False}
    35283690   
    3529     def _repr_(self):
     3691    def __repr__(self):
     3692        """
     3693        Returns a string representation of this TextFactory object.
     3694
     3695        TESTS:
     3696            sage: text
     3697            type text? for help and examples
     3698        """
    35303699        return "type text? for help and examples"
    35313700
    35323701    def _from_xdata_ydata(self, string, point, options):