Ticket #1862: 1862.patch
File 1862.patch, 13.0 KB (added by , 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 1 1 from plot import (Graphics, line, polygon, plot, point, points, 2 2 text, circle, disk, hue, graphics_array, 3 3 list_plot, networkx_plot, parametric_plot, 4 polar_plot, contour_plot, arrow,4 polar_plot, contour_plot, implicit_plot, arrow, 5 5 plot_vector_field, matrix_plot, bar_chart, 6 6 is_Graphics, rainbow, 7 7 show_default) 
sage/plot/plot.py
diff r 563d91fd399a r 07db6448af01 sage/plot/plot.py
a b class GraphicPrimitive_ContourPlot(Graph 1995 1995 'cmap':"""The colormap, one of (autumn, bone, cool, copper, 1996 1996 gray, hot, hsv, jet, pink, prism, spring, summer, winter)""", 1997 1997 '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."""} 1999 2001 2000 2002 def _repr_(self): 2001 2003 return "ContourPlot defined by a %s x %s data grid"%(self.xy_array_row, self.xy_array_col) 2002 2004 2003 2005 def _render_on_subplot(self, subplot): 2006 from sage.rings.integer import Integer 2004 2007 options = self.options() 2005 2008 fill = options['fill'] 2006 2009 cmap = options['cmap'] … … class GraphicPrimitive_ContourPlot(Graph 2021 2024 if fill: 2022 2025 if contours is None: 2023 2026 subplot.contourf(self.xy_data_array, cmap=cmap, extent=(x0,x1,y0,y1)) 2024 el se:2027 elif isinstance(contours, (int, Integer)): 2025 2028 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)) 2026 2031 else: 2027 2032 if contours is None: 2028 2033 subplot.contour(self.xy_data_array, cmap=cmap, extent=(x0,x1,y0,y1)) 2029 el se:2034 elif isinstance(contours, (int, Integer)): 2030 2035 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)) 2031 2038 2032 2039 2033 2040 class GraphicPrimitive_MatrixPlot(GraphicPrimitive): … … class ArrowFactory(GraphicPrimitiveFacto 2712 2719 def _reset(self): 2713 2720 self.options={'width':0.02,'rgbcolor':(0, 0, 1)} 2714 2721 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 """ 2716 2730 return "type arrow? for help and examples" 2717 2731 2718 2732 def _from_xdata_ydata(self, xmin, ymin, xmax, ymax, options): … … class BarChartFactory(GraphicPrimitiveFa 2741 2755 def _reset(self): 2742 2756 self.options={'width':0.5,'rgbcolor':(0, 0, 1)} 2743 2757 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 """ 2745 2766 return "type bar_chart? for help and examples" 2746 2767 2747 2768 def _from_xdata_ydata(self, ind, datalist, xrange, yrange, options): … … class CircleFactory(GraphicPrimitiveFact 2800 2821 def _reset(self): 2801 2822 self.options={'alpha':1,'fill':False,'thickness':1,'rgbcolor':(0, 0, 1)} 2802 2823 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 """ 2804 2832 return "type circle? for help and examples" 2805 2833 2806 2834 def _from_xdata_ydata(self, point, r, options): … … class ContourPlotFactory(GraphicPrimitiv 2833 2861 cmap  string (default: 'gray'), the color map to use: 2834 2862 autumn, bone, cool, copper, gray, hot, hsv, 2835 2863 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. 2839 2872 2840 2873 2841 2874 EXAMPLES: … … class ContourPlotFactory(GraphicPrimitiv 2861 2894 sage: contour_plot(y^2 + 1  x^3  x, (y,pi,pi), (x,pi,pi)) 2862 2895 sage: contour_plot(lambda x,y: y^2 + 1  x^3  x, (y,pi,pi), (x,pi,pi)) 2863 2896 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 2865 2906 """ 2866 2907 def _reset(self): 2867 2908 self.options={'plot_points':25, 'fill':True, 'cmap':'gray', 'contours':None} 2868 2909 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 """ 2870 2918 return "type contour_plot? for help and examples" 2871 2919 2872 2920 def _from_xdata_ydata(self, xy_data_array, xrange, yrange, options): 2873 2921 g = Graphics() 2874 2922 g._contour_plot(xy_data_array, xrange, yrange, options) … … class ContourPlotFactory(GraphicPrimitiv 2876 2924 2877 2925 #unique contour_plot instance 2878 2926 contour_plot = ContourPlotFactory() 2927 2928 class 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)  2tuple, the range of x values 2939 (ymin, ymax)  2tuple, 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 firstlevel approximation is just a circle. 2960 sage: implicit_plot(mandel(1), (3, 3), (3, 3)).show(aspect_ratio=1) 2961 2962 A thirdlevel approximation starts to get interesting. 2963 sage: implicit_plot(mandel(3), (2, 1), (1.5, 1.5)).show(aspect_ratio=1) 2964 2965 The seventhlevel 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 2992 implicit_plot = ImplicitPlotFactory() 2879 2993 2880 2994 class LineFactory(GraphicPrimitiveFactory_from_point_list): 2881 2995 r""" … … class LineFactory(GraphicPrimitiveFactor 2977 3091 def _reset(self): 2978 3092 self.options = {'alpha':1,'rgbcolor':(0,0,1),'thickness':1} 2979 3093 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" 2982 3103 2983 3104 def _from_xdata_ydata(self, xdata, ydata, coerce, options): 2984 3105 if coerce: … … class MatrixPlotFactory(GraphicPrimitive 3026 3147 def _reset(self): 3027 3148 self.options={'cmap':'gray'} 3028 3149 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 """ 3030 3158 return "type matrix_plot? for help and examples" 3031 3159 3032 3160 def _from_xdata_ydata(self, xy_data_array, xrange, yrange, options): … … class DiskFactory(GraphicPrimitiveFactor 3102 3230 def _reset(self): 3103 3231 self.options={'alpha':1,'fill':True,'rgbcolor':(0,0,1),'thickness':0} 3104 3232 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 """ 3106 3241 return "type disk? for help and examples" 3107 3242 3108 3243 def _from_xdata_ydata(self, point, r, angle, options): … … class PointFactory(GraphicPrimitiveFacto 3131 3266 def _reset(self): 3132 3267 self.options = {'alpha':1,'pointsize':10,'faceted':False,'rgbcolor':(0,0,1)} 3133 3268 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" 3136 3278 3137 3279 def _from_xdata_ydata(self, xdata, ydata, coerce, options): 3138 3280 if coerce: … … class PolygonFactory(GraphicPrimitiveFac 3214 3356 def _reset(self): 3215 3357 self.options={'alpha':1,'rgbcolor':(0,0,1),'thickness':0} 3216 3358 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" 3219 3368 3220 3369 def _from_xdata_ydata(self, xdata, ydata, coerce, options): 3221 3370 if coerce: … … class PlotFactory(GraphicPrimitiveFactor 3363 3512 o['max_bend'] = 0.1 3364 3513 o['rgbcolor'] = (0,0,1) 3365 3514 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" 3368 3524 3369 3525 def __call__(self, funcs, *args, **kwds): 3370 3526 do_show = False … … class TextFactory(GraphicPrimitiveFactor 3545 3701 'vertical_alignment':'center', 3546 3702 'axis_coords':False} 3547 3703 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 """ 3549 3712 return "type text? for help and examples" 3550 3713 3551 3714 def _from_xdata_ydata(self, string, point, options):