Ticket #5651: trac5651.patch

File trac5651.patch, 20.7 KB (added by wcauchois, 14 years ago)
  • sage/plot/arrow.py

    From: Bill Cauchois <wcauchois@gmail.com>
    
    trac #5651 -- Make it so that plot(...) passes extra options to show.
    
    diff -r 7f9164053b2a sage/plot/arrow.py
    a b  
    310310    If we want to draw the arrow between objects, for example, the
    311311    boundaries of two lines, we can use the arrowshorten option
    312312    to make the arrow shorter by a certain number of points.
     313
    313314        sage: line([(0,0),(1,0)],thickness=10)+line([(0,1),(1,1)], thickness=10)+arrow((0.5,0),(0.5,1), arrowshorten=10,rgbcolor=(1,0,0))
    314315
     316    Extra options will get passed on to show(), as long as they are valid:
    315317
     318        sage: arrow((-2, 2), (7,1), frame=True)
     319        sage: arrow((-2, 2), (7,1)).show(frame=True)
    316320    """
    317321    from sage.plot.plot import Graphics
    318322    g = Graphics()
     323    g._set_extra_kwds(Graphics._extract_kwds_for_show(options))
    319324    if headpoint is not None and tailpoint is not None:
    320325        xtail, ytail = tailpoint
    321326        xhead, yhead = headpoint
  • sage/plot/bar_chart.py

    diff -r 7f9164053b2a sage/plot/bar_chart.py
    a b  
    112112
    113113    A bar_chart with negative values and red bars:
    114114        sage: bar_chart([-3,5,-6,11], rgbcolor=(1,0,0))
     115
     116    Extra options will get passed on to show(), as long as they are valid:
     117        sage: bar_chart([-2,8,-7,3], rgbcolor=(1,0,0), axes=False)
     118        sage: bar_chart([-2,8,-7,3], rgbcolor=(1,0,0)).show(axes=False) # These are equivalent
    115119    """
    116120    dl = len(datalist)
    117121    #if dl > 1:
     
    127131        #cnt += 1
    128132
    129133    g = Graphics()
     134    g._set_extra_kwds(Graphics._extract_kwds_for_show(options))
    130135    #TODO: improve below for multiple data sets!
    131136    #cnt = 1
    132137    #for ind, pnts, xrange, yrange in bardata:
  • sage/plot/bezier_path.py

    diff -r 7f9164053b2a sage/plot/bezier_path.py
    a b  
    166166        sage: path = [[(0,0),(.5,1),(1,0)]]
    167167        sage: curve = bezier_path(path, linestyle='dashed', rgbcolor='green')
    168168        sage: curve
     169
     170    Extra options will get passed on to show(), as long as they are valid:
     171
     172        sage: bezier_path([[(0,1),(.5,0),(1,1)]], fontsize=50)
     173        sage: bezier_path([[(0,1),(.5,0),(1,1)]]).show(fontsize=50) # These are equivalent
    169174    """
    170175    from sage.plot.plot import Graphics
    171176    g = Graphics()
     177    g._set_extra_kwds(g._extract_kwds_for_show(options))
    172178    g.add_primitive(BezierPath(path, options))
    173179    return g
    174180   
  • sage/plot/circle.py

    diff -r 7f9164053b2a sage/plot/circle.py
    a b  
    143143        ...
    144144        sage: g.show(xmin=-(paths+1)^2, xmax=(paths+1)^2, ymin=-(paths+1)^2, ymax=(paths+1)^2, figsize=[6,6])
    145145
     146    Extra options will get passed on to show(), as long as they are valid:
     147
     148        sage: circle((0, 0), 2, figsize=[10,10]) # That circle is huge!
     149        sage: circle((0, 0), 2).show(figsize=[10,10]) # These are equivalent
     150
    146151    """
    147152    from sage.plot.plot import Graphics
    148153    g = Graphics()
     154    g._set_extra_kwds(Graphics._extract_kwds_for_show(options))
    149155    g.add_primitive(Circle(center[0], center[1], radius, options))
    150156    return g
  • sage/plot/complex_plot.pyx

    diff -r 7f9164053b2a sage/plot/complex_plot.pyx
    a b  
    235235
    236236    The Riemann Zeta function:
    237237        sage: complex_plot(zeta, (-30,30), (-30,30))
     238
     239    Extra options will get passed on to show(), as long as they are valid:
     240        sage: complex_plot(lambda z: z, (-3, 3), (-3, 3), figsize=[1,1])
     241        sage: complex_plot(lambda z: z, (-3, 3), (-3, 3)).show(figsize=[1,1]) # These are equivalent
    238242    """
    239243    from sage.plot.plot import Graphics, setup_for_eval_on_grid
    240244    cdef double x, y
     
    248252                                            for y in yrange_list]
    249253    _sig_off
    250254    g = Graphics()
     255    g._set_extra_kwds(Graphics._extract_kwds_for_show(options, ignore=['xmin', 'xmax']))
    251256    g.add_primitive(ComplexPlot(z_values, xrange, yrange, options))
    252257    return g
  • sage/plot/contour_plot.py

    diff -r 7f9164053b2a sage/plot/contour_plot.py
    a b  
    149149        sage: contour_plot(f, (-2, 2), (-2, 2), contours=(0.1, 1.0, 1.2, 1.4), cmap='hsv')
    150150        sage: contour_plot(f, (-2, 2), (-2, 2), contours=(1.0,), fill=False)
    151151
     152    Extra options will get passed on to show(), as long as they are valid:
     153        sage: f(x, y) = cos(x) + sin(y)
     154        sage: contour_plot(f, (0, pi), (0, pi), axes=False)
     155        sage: contour_plot(f, (0, pi), (0, pi)).show(axes=False) # These are equivalent
    152156    """
    153157    from sage.plot.plot import Graphics, setup_for_eval_on_grid
    154158    g, xstep, ystep, xrange, yrange = setup_for_eval_on_grid([f], xrange, yrange, options['plot_points'])
     
    157161                              for y in xsrange(yrange[0], yrange[1], ystep)]
    158162
    159163    g = Graphics()
     164    g._set_extra_kwds(Graphics._extract_kwds_for_show(options, ignore=['xmin', 'xmax']))
    160165    g.add_primitive(ContourPlot(xy_data_array, xrange, yrange, options))
    161166    return g       
    162167
  • sage/plot/density_plot.py

    diff -r 7f9164053b2a sage/plot/density_plot.py
    a b  
    123123    variables x,y.
    124124        sage: density_plot(y^2 + 1 - x^3 - x, (y,-pi,pi), (x,-pi,pi))
    125125        sage: density_plot(y^2 + 1 - x^3 - x, (x,-pi,pi), (y,-pi,pi))
     126
     127    Extra options will get passed on to show(), as long as they are valid:
     128        sage: density_plot(log(x) + log(y), (x, 1, 10), (y, 1, 10), dpi=20)
     129        sage: density_plot(log(x) + log(y), (x, 1, 10), (y, 1, 10)).show(dpi=20) # These are equivalent
    126130    """
    127131    from sage.plot.plot import Graphics, setup_for_eval_on_grid
    128132    g, xstep, ystep, xrange, yrange = setup_for_eval_on_grid([f], xrange, yrange, options['plot_points'])
     
    131135                              for y in xsrange(yrange[0], yrange[1], ystep)]
    132136
    133137    g = Graphics()
     138    g._set_extra_kwds(Graphics._extract_kwds_for_show(options, ignore=['xmin', 'xmax']))
    134139    g.add_primitive(DensityPlot(xy_data_array, xrange, yrange, options))
    135140    return g
  • sage/plot/disk.py

    diff -r 7f9164053b2a sage/plot/disk.py
    a b  
    9696        sage: P  = tl+tr+bl+br
    9797        sage: P.show(figsize=(4,4),xmin=-2,xmax=2,ymin=-2,ymax=2)
    9898
     99    Extra options will get passed on to show(), as long as they are valid::
     100        sage: disk((0, 0), 5, (0, pi/2), xmin=0, xmax=5, ymin=0, ymax=5, figsize=(2,2), rgbcolor=(1, 0, 1))
     101        sage: disk((0, 0), 5, (0, pi/2), rgbcolor=(1, 0, 1)).show(xmin=0, xmax=5, ymin=0, ymax=5, figsize=(2,2)) # These are equivalent
    99102    """
    100103    from sage.plot.plot import Graphics
    101104    g = Graphics()
     105    g._set_extra_kwds(Graphics._extract_kwds_for_show(options))
    102106    g.add_primitive(Disk(point, radius, angle, options))
    103107    return g
  • sage/plot/line.py

    diff -r 7f9164053b2a sage/plot/line.py
    a b  
    317317        sage: G + P + Q   # show the plot
    318318
    319319    A line with no points or one point:
     320
    320321        sage: line([])
    321322        sage: line([(1,1)])
    322323
     324    Extra options will get passed on to show(), as long as they are valid:
     325   
     326        sage: line([(0,1), (3,4)], figsize=[10, 2])
     327        sage: line([(0,1), (3,4)]).show(figsize=[10, 2]) # These are equivalent
    323328    """
    324329    from sage.plot.plot import Graphics, xydata_from_point_list
    325330    xdata, ydata = xydata_from_point_list(points)
    326331    g = Graphics()
     332    g._set_extra_kwds(Graphics._extract_kwds_for_show(options))
    327333    g.add_primitive(Line(xdata, ydata, options))
    328334    return g
    329335
  • sage/plot/matrix_plot.py

    diff -r 7f9164053b2a sage/plot/matrix_plot.py
    a b  
    100100        sage: import numpy
    101101        sage: matrix_plot(numpy.random.rand(10, 10))
    102102
     103    Extra options will get passed on to show(), as long as they are valid:
     104        sage: matrix_plot([[1, 0], [0, 1]], fontsize=10)
     105        sage: matrix_plot([[1, 0], [0, 1]]).show(fontsize=10) # These are equivalent
     106
    103107    TESTS:
    104108        sage: P.<t> = RR[]
    105109        sage: matrix_plot(random_matrix(P, 3, 3))
     
    139143    yrange = (0, xy_data_array.shape[0])
    140144
    141145    g = Graphics()
     146    g._set_extra_kwds(Graphics._extract_kwds_for_show(options))
    142147    g.add_primitive(MatrixPlot(xy_data_array, xrange, yrange, options))
    143148    return g
  • sage/plot/plot.py

    diff -r 7f9164053b2a sage/plot/plot.py
    a b  
    396396        self.__tick_label_color = (0, 0, 0)
    397397        self.__axes_width = 0.8
    398398        self.__objects = []
     399        self._extra_kwds = {}
    399400
    400401    def set_aspect_ratio(self, ratio):
    401402        """
     
    10201021       
    10211022        EXAMPLES::
    10221023       
    1023             sage: g1 = plot(abs(sqrt(x^3-1)), (x,1,5))
     1024            sage: g1 = plot(abs(sqrt(x^3-1)), (x,1,5), frame=True)
    10241025            sage: g2 = plot(-abs(sqrt(x^3-1)), (x,1,5), rgbcolor=(1,0,0))
    10251026            sage: g1 + g2  # displays the plot
     1027
     1028        TESTS::
     1029
     1030            sage: (g1 + g2)._extra_kwds # extra keywords to show are propagated
     1031            {'frame': True}
    10261032        """
    10271033        if isinstance(other, int) and other == 0:
    10281034            return self
     
    10341040        g = Graphics()
    10351041        g.__objects = self.__objects + other.__objects
    10361042        g.__aspect_ratio = max(self.__aspect_ratio, other.__aspect_ratio)
     1043        g._extra_kwds.update(self._extra_kwds)
     1044        g._extra_kwds.update(other._extra_kwds)
    10371045        return g
    10381046 
    10391047    def add_primitive(self, primitive):
     
    10701078        if z:
    10711079            g = g.translate(0,0,z)
    10721080        return g
     1081
     1082    @classmethod
     1083    def _extract_kwds_for_show(cls, kwds, ignore=[]):
     1084        """
     1085        Extract keywords relevant to show() from the provided dictionary.
     1086
     1087        EXAMPLES::
     1088
     1089            sage: kwds = {'f': lambda x: x, 'xmin': 0, 'figsize': [1,1], 'plot_points': (40, 40)}
     1090            sage: G_kwds = Graphics._extract_kwds_for_show(kwds, ignore='xmin')
     1091            sage: kwds # Note how this action modifies the passed dictionary
     1092            {'xmin': 0, 'plot_points': (40, 40), 'f': <function <lambda> at ...>}
     1093            sage: G_kwds
     1094            {'figsize': [1, 1]}
     1095
     1096        This method is intended to be used with _set_extra_kwds(). Here is an
     1097        idiom to ensure the correct keywords will get passed on to show()::
     1098
     1099            sage: options = {} # Usually this will come from an argument
     1100            sage: g = Graphics()
     1101            sage: g._set_extra_kwds(Graphics._extract_kwds_for_show(options))
     1102        """
     1103        result = {}
     1104        for option in cls.SHOW_OPTIONS:
     1105            if option not in ignore:
     1106                try:
     1107                    result[option] = kwds.pop(option)
     1108                except KeyError:
     1109                    pass
     1110        return result
     1111
     1112    def _set_extra_kwds(self, kwds):
     1113        """
     1114        Set a dictionary of keywords that will get passed on to show().
     1115
     1116        TESTS::
    10731117       
    1074     def show(self, xmin=None, xmax=None, ymin=None, ymax=None,
    1075              figsize=DEFAULT_FIGSIZE, filename=None,
    1076              dpi=DEFAULT_DPI, axes=None, axes_labels=None,frame=False,
    1077              fontsize=None, aspect_ratio=None,
    1078              gridlines=None, gridlinesstyle=None,
    1079              vgridlinesstyle=None, hgridlinesstyle=None):
     1118            sage: g = Graphics()
     1119            sage: g._extra_kwds
     1120            {}
     1121            sage: g._set_extra_kwds({'figsize': [10,10]})
     1122            sage: g._extra_kwds
     1123            {'figsize': [10, 10]}
     1124            sage: g.show() # Now the (blank) plot will be extra large
     1125        """
     1126        self._extra_kwds = kwds
     1127
     1128    # This dictionary has the default values for the keywords to show(). When
     1129    # show is invoked with keyword arguments, those arguments are merged with
     1130    # this dictionary to create a set of keywords with the defaults filled in.
     1131
     1132    # NOTE: If you intend to use a new parameter in show(), you should update
     1133    # this dictionary to contain the default value for that parameter.
     1134
     1135    SHOW_OPTIONS = dict(xmin=None, xmax=None, ymin=None, ymax=None,
     1136                        figsize=DEFAULT_FIGSIZE, filename=None,
     1137                        dpi=DEFAULT_DPI, axes=None, axes_labels=None,frame=False,
     1138                        fontsize=None, aspect_ratio=None,
     1139                        gridlines=None, gridlinesstyle=None,
     1140                        vgridlinesstyle=None, hgridlinesstyle=None)
     1141
     1142    def show(self, **kwds):
    10801143        """
    10811144        Show this graphics image with the default image viewer.
    10821145
     
    12681331            sage: M = MatrixSpace(QQ,10).random_element()
    12691332            sage: matrix_plot(M).show(gridlines=True)
    12701333        """
    1271         if DOCTEST_MODE:
    1272             self.save(DOCTEST_MODE_FILE,
    1273                       xmin, xmax, ymin, ymax, figsize,
    1274                       dpi=dpi, axes=axes, axes_labels=axes_labels,frame=frame,
    1275                       aspect_ratio=aspect_ratio, gridlines=gridlines,
    1276                       gridlinesstyle=gridlinesstyle,
    1277                       vgridlinesstyle=vgridlinesstyle,
    1278                       hgridlinesstyle=hgridlinesstyle)
     1334        options = {}
     1335        options.update(self.SHOW_OPTIONS)
     1336        options.update(self._extra_kwds)
     1337        options.update(kwds)
     1338
     1339        if DOCTEST_MODE or EMBEDDED_MODE:
     1340            self.save(**options)
    12791341            return
    1280         if EMBEDDED_MODE:
    1281             if filename is None:
    1282                 filename = sage.misc.misc.graphics_filename()
    1283             self.save(filename, xmin, xmax, ymin, ymax, figsize,
    1284                       dpi=dpi, axes=axes, axes_labels=axes_labels,frame=frame,
    1285                       aspect_ratio=aspect_ratio, gridlines=gridlines,
    1286                       gridlinesstyle=gridlinesstyle,
    1287                       vgridlinesstyle=vgridlinesstyle,
    1288                       hgridlinesstyle=hgridlinesstyle)
    1289             html("<img src='cell://%s'>"%filename)
    1290             return
    1291         if filename is None:
    1292             filename = sage.misc.misc.tmp_filename() + '.png'
    1293         self.save(filename, xmin, xmax, ymin, ymax, figsize, dpi=dpi, axes=axes,
    1294                   axes_labels=axes_labels,
    1295                   frame=frame, fontsize=fontsize,
    1296                   aspect_ratio=aspect_ratio,
    1297                   gridlines=gridlines,
    1298                   gridlinesstyle=gridlinesstyle,
    1299                   vgridlinesstyle=vgridlinesstyle,
    1300                   hgridlinesstyle=hgridlinesstyle)
    1301         os.system('%s %s 2>/dev/null 1>/dev/null &'%(sage.misc.viewer.browser(), filename))
     1342
     1343        if options['filename'] is None:
     1344            options['filename'] = sage.misc.misc.tmp_filename() + '.png'
     1345        self.save(**options)
     1346        os.system('%s %s 2>/dev/null 1>/dev/null &' % \
     1347            (sage.misc.viewer.browser(), options['filename']))
    13021348
    13031349    def xmin(self, xmin=None):
    13041350        """
     
    18871933
    18881934        sage: def b(n): return lambda x: bessel_J(n, x) + 0.5*(n-1)
    18891935        sage: plot([b(c) for c in [1..5]], 0, 40, fill = dict([(i, i+1) for i in [0..3]]))
     1936   
     1937    Extra options will get passed on to show(), as long as they are valid::
     1938
     1939        sage: plot(sin(x^2), (x, -3, 3), figsize=[8,2])
     1940        sage: plot(sin(x^2), (x, -3, 3)).show(figsize=[8,2]) # These are equivalent
    18901941
    18911942    TESTS:
    18921943   
     
    19421993        ...
    19431994        RuntimeError: Error in line(): option 'foo' not valid.
    19441995    """
     1996    G_kwds = Graphics._extract_kwds_for_show(kwds, ignore=['xmin', 'xmax'])
     1997
    19451998    original_opts = kwds.pop('__original_opts', {})
    19461999    do_show = kwds.pop('show',False)
    19472000    if hasattr(funcs, 'plot'):
     
    19802033        else:
    19812034            sage.misc.misc.verbose("there were %s extra arguments (besides %s)" % (n, funcs), level=0)
    19822035
     2036    G._set_extra_kwds(G_kwds)
    19832037    if do_show:
    19842038        G.show()
    19852039    return G
  • sage/plot/plot_field.py

    diff -r 7f9164053b2a sage/plot/plot_field.py
    a b  
    151151        sage: x,y = var('x,y')
    152152        sage: plot_vector_field( (-x/sqrt(x^2+y^2), -y/sqrt(x^2+y^2)), (x, -10, 10), (y, -10, 10))
    153153        sage: plot_vector_field( (-x/sqrt(x+y), -y/sqrt(x+y)), (x, -10, 10), (y, -10, 10))
     154
     155    Extra options will get passed on to show(), as long as they are valid::
     156
     157        sage: plot_vector_field((x, y), (x, -2, 2), (y, -2, 2), xmax=10)
     158        sage: plot_vector_field((x, y), (x, -2, 2), (y, -2, 2)).show(xmax=10) # These are equivalent
    154159    """
    155160    from sage.plot.plot import setup_for_eval_on_grid, Graphics
    156161    z, xstep, ystep, xrange, yrange = setup_for_eval_on_grid([f,g], xrange, yrange, options['plot_points'])
     
    168173    xvec_array = numpy.ma.masked_invalid(numpy.array(xvec_array, dtype=float))
    169174    yvec_array = numpy.ma.masked_invalid(numpy.array(yvec_array, dtype=float))
    170175    g = Graphics()
     176    g._set_extra_kwds(Graphics._extract_kwds_for_show(options))
    171177    g.add_primitive(PlotField(xpos_array, ypos_array, xvec_array, yvec_array, options))
    172178    return g
    173179
  • sage/plot/point.py

    diff -r 7f9164053b2a sage/plot/point.py
    a b  
    124124        sage: point((1,2,3))
    125125        sage: point([(0,0), (1,1)])
    126126        sage: point([(0,0,1), (1,1,1)])
     127
     128    Extra options will get passed on to show(), as long as they are valid:
     129        sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)], frame=True)
     130        sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)]).show(frame=True) # These are equivalent
    127131    """
    128132    try:
    129133        return point2d(points, **kwds)
     
    150154    from sage.plot.plot import xydata_from_point_list, Graphics
    151155    xdata, ydata = xydata_from_point_list(points)
    152156    g = Graphics()
     157    g._set_extra_kwds(Graphics._extract_kwds_for_show(options))
    153158    g.add_primitive(Point(xdata, ydata, options))
    154159    return g
    155160
  • sage/plot/polygon.py

    diff -r 7f9164053b2a sage/plot/polygon.py
    a b  
    8989    EXAMPLES:
    9090        sage: polygon([(0,0), (1,1), (0,1)])
    9191        sage: polygon([(0,0,1), (1,1,1), (2,0,1)])
     92
     93    Extra options will get passed on to show(), as long as they are valid:
     94        sage: polygon([(0,0), (1,1), (0,1)], axes=False)
     95        sage: polygon([(0,0), (1,1), (0,1)]).show(axes=False) # These are equivalent
    9296    """
    9397    try:
    9498        return polygon2d(points, **options)
     
    163167    from sage.plot.plot import xydata_from_point_list, Graphics
    164168    xdata, ydata = xydata_from_point_list(points)
    165169    g = Graphics()
     170    g._set_extra_kwds(Graphics._extract_kwds_for_show(options))
    166171    g.add_primitive(Polygon(xdata, ydata, options))
    167172    return g
  • sage/plot/scatter_plot.py

    diff -r 7f9164053b2a sage/plot/scatter_plot.py
    a b  
    120120        sage: s = scatter_plot([[0,1],[2,2],[4.3,1.1]], marker='s')
    121121        sage: s
    122122       
     123    Extra options will get passed on to show(), as long as they are valid:
     124        sage: scatter_plot([(0, 0), (1, 1)], markersize=100, facecolor='green', ymax=100)
     125        sage: scatter_plot([(0, 0), (1, 1)], markersize=100, facecolor='green').show(ymax=100) # These are equivalent
    123126    """
    124127    import numpy
    125128    from sage.plot.plot import Graphics
    126129    g = Graphics()
     130    g._set_extra_kwds(Graphics._extract_kwds_for_show(options))
    127131    data = numpy.array(datalist, dtype='float')
    128132    if len(data) != 0:
    129133        xdata = data[:,0]
  • sage/plot/text.py

    diff -r 7f9164053b2a sage/plot/text.py
    a b  
    119119        ...
    120120        ValueError: use text3d instead for text in 3d
    121121        sage: t = text3d("hi",(1,2,3))
     122
     123    Extra options will get passed on to show(), as long as they are valid:
     124        sage: text("MATH IS AWESOME", (0, 0), fontsize=40, axes=False)
     125        sage: text("MATH IS AWESOME", (0, 0), fontsize=40).show(axes=False) # These are equivalent
    122126    """
    123127    try:
    124128        x, y = xy
     
    130134    options['rgbcolor'] = to_mpl_color(options['rgbcolor'])
    131135    point = (float(x), float(y))
    132136    g = Graphics()
     137    g._set_extra_kwds(Graphics._extract_kwds_for_show(options, ignore='fontsize'))
    133138    g.add_primitive(Text(string, point, options))
    134139    return g