Changeset 7889:3670aecb098e


Ignore:
Timestamp:
12/23/07 23:34:29 (5 years ago)
Author:
William Stein <wstein@…>
Branch:
default
Message:

Add 3d parametric plots (implemented currently in terms of line segments); lots of other misc touch up.

Location:
sage/plot
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • sage/plot/plot.py

    r7888 r7889  
    22832283        plot(X, ...) 
    22842284         
    2285     where X is a SAGE object that either is callable and returns 
    2286     numbers that can be coerced to floats, or has a plot method 
    2287     that returns a GraphicPrimitive object. 
     2285    where X is a Sage object (or list of Sage objects) that either is 
     2286    callable and returns numbers that can be coerced to floats, or has 
     2287    a plot method that returns a GraphicPrimitive object. 
    22882288     
    22892289    Type plot.options for a dictionary of the default 
     
    23522352        sage: P.show()   # render 
    23532353         
     2354    We plot several functions together by passing a list 
     2355    of functions as input: 
     2356       sage: show(plot([sin(n*x) for n in [1..4]], 0, pi)) 
     2357 
     2358 
     2359    The function $\sin(1/x)$ wiggles wildtly near $0$, so the 
     2360    first plot below won't look perfect.  Sage adapts to this 
     2361    and plots extra points near the origin. 
     2362       sage: show(plot(sin(1/x), -1, 1)) 
     2363 
     2364    The \code{plot_points} option, you can increase the number 
     2365    of sample points, to obtain a more accurate plot.  
     2366       sage: show(plot(sin(1/x), -1, 1, plot_points=1000)) 
     2367 
     2368    The actual sample points are slightly randomized, so the above 
     2369    plots may look slightly different each time you draw them. 
     2370 
    23542371    Use \code{show(plot(sin, 0,10))} or \code{plot(sin,0,10).show()} 
    23552372    to show the corresponding graphics object. 
    2356      
     2373 
    23572374    We draw the graph of an elliptic curve as the union 
    23582375    of graphs of 2 functions. 
     
    24192436        #and will plotted as (f(x), g(x)) for all x in the given range 
    24202437        if parametric: 
    2421             f,g = funcs 
     2438            if len(funcs) == 3: 
     2439                # 3d 
     2440                from plot3d.shapes import parametric_plot_3d 
     2441                return parametric_plot_3d(funcs, xmin, xmax, polar=polar, label=label, show=show, **kwds) 
     2442            elif len(funcs) == 2: 
     2443                # 2d 
     2444                f,g = funcs 
     2445            else: 
     2446                raise ValueError, "parametric plots only implemented in 2 and 3 dimensions." 
     2447             
    24222448        #or we have only a single function to be plotted: 
    24232449        else: 
     
    25422568########## misc functions ################### 
    25432569 
    2544 def parametric_plot((f,g), tmin, tmax, show=None, **kwargs): 
     2570def parametric_plot(funcs, tmin, tmax, show=None, **kwargs): 
    25452571    """ 
    25462572    parametric_plot takes two functions as a list or a tuple and make 
     
    25492575 
    25502576    INPUT: 
    2551         (f,g) -- tuple of functions 
     2577        funcs -- 2 or 3-tuple of functions 
    25522578        tmin -- start value of t 
    25532579        tmax -- end value of t 
     
    25582584 
    25592585    EXAMPLE: 
     2586    We draw a 2d parametric plot: 
    25602587        sage: t = var('t') 
    25612588        sage: G = parametric_plot( (sin(t), sin(2*t)), 0, 2*pi, rgbcolor=hue(0.6) ) 
    25622589        sage: G.show() 
     2590 
     2591    We draw a 3d parametric plot: 
     2592        sage: show(parametric_plot( (5*cos(x), 5*sin(x), x), -12, 12, plot_points=150, color="red"))     
    25632593    """ 
    25642594    if show is None: 
    25652595        show = SHOW_DEFAULT 
    2566     return plot((f,g), tmin, tmax, parametric=True, show=show, **kwargs)             
     2596    return plot(funcs, tmin, tmax, parametric=True, show=show, **kwargs)             
    25672597 
    25682598def polar_plot(funcs, xmin, xmax, show=None, **kwargs): 
  • sage/plot/plot3d/base.pyx

    r7866 r7889  
    243243            import sage.misc.viewer 
    244244            viewer_app = sage.misc.viewer.browser() 
     245 
    245246        if DOCTEST_MODE or viewer=='java3d': 
    246247            f = open(filename+".obj", "w") 
     
    264265        if ext is None: 
    265266            raise ValueError, "Unknown 3d plot type: %s" % viewer 
     267 
    266268        if not DOCTEST_MODE and not EMBEDDED_MODE: 
    267269            if verbosity: 
  • sage/plot/plot3d/index_face_set.pyx

    r7866 r7889  
    178178    """ 
    179179 
    180     def __new__(self, faces, point_list=None, enclosde=False, **kwds): 
     180    def __new__(self, faces, point_list=None, enclosed=False, **kwds): 
    181181        self.vs = <point_c *>NULL 
    182182        self.face_indices = <int *>NULL 
  • sage/plot/plot3d/shapes.pyx

    r7865 r7889  
    5050from sage.modules.free_module_element import vector 
    5151 
    52 from base import Graphics3dGroup 
     52from sage.misc.all import srange 
     53 
     54from base import Graphics3dGroup, Graphics3d 
    5355 
    5456 
    5557class Box(IndexFaceSet): 
     58    """ 
     59 
     60    EXAMPLES: 
     61    A square black box: 
     62        sage: show(Box([1,1,1])) 
     63 
     64    A red rectangular box.  
     65        sage: show(Box([2,3,4], color="red")) 
     66 
     67    A stack of boxes: 
     68        sage: show(sum([Box([2,3,1], color="red").translate((0,0,6*i)) for i in [0..3]])) 
     69 
     70    A sinusoidal stack of multicolored boxes: 
     71        sage: B = sum([Box([2,4,1/4], color=(i/4,i/5,1)).translate((sin(i),0,5-i)) for i in [0..20]]) 
     72        sage: show(B, figsize=6)     
     73    """ 
    5674     
    5775    def __init__(self, *size, **kwds): 
     
    334352 
    335353 
     354def parametric_plot_3d(funcs, tmin, tmax, plot_points=50, show=None, thickness=0.2, polar=False, **kwargs): 
     355 
     356    if polar: 
     357        raise NotImplementedError, "3d parametric polar plots not implemented" 
     358 
     359    f,g,h = funcs 
     360 
     361    # normalize number of points to an integer 
     362    plot_points = int(plot_points) 
     363    if plot_points <= 1: 
     364        plot_points = 1 
     365 
     366     
     367    v = srange(tmin, tmax, (tmax-tmin)/plot_points, include_endpoint=True) 
     368    t0 = v[0] 
     369    P =  (f(t0), g(t0), h(t0)) 
     370    G = 0 
     371    for i in range(1, len(v)): 
     372        t1 = v[i] 
     373        Q = (f(t1), g(t1), h(t1)) 
     374        G += LineSegment(P, Q, **kwargs) 
     375        P = Q 
     376 
     377    if show: 
     378        G.show(**kwargs) 
     379     
     380    return G 
     381     
Note: See TracChangeset for help on using the changeset viewer.