# 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 If we want to draw the arrow between objects, for example, the boundaries of two lines, we can use the arrowshorten option to make the arrow shorter by a certain number of points. 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)) Extra options will get passed on to show(), as long as they are valid: sage: arrow((-2, 2), (7,1), frame=True) sage: arrow((-2, 2), (7,1)).show(frame=True) """ from sage.plot.plot import Graphics g = Graphics() g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) if headpoint is not None and tailpoint is not None: xtail, ytail = tailpoint xhead, yhead = headpoint
• ## sage/plot/bar_chart.py

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

`diff -r 7f9164053b2a sage/plot/bezier_path.py`
 a sage: path = [[(0,0),(.5,1),(1,0)]] sage: curve = bezier_path(path, linestyle='dashed', rgbcolor='green') sage: curve Extra options will get passed on to show(), as long as they are valid: sage: bezier_path([[(0,1),(.5,0),(1,1)]], fontsize=50) sage: bezier_path([[(0,1),(.5,0),(1,1)]]).show(fontsize=50) # These are equivalent """ from sage.plot.plot import Graphics g = Graphics() g._set_extra_kwds(g._extract_kwds_for_show(options)) g.add_primitive(BezierPath(path, options)) return g
• ## sage/plot/circle.py

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

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

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

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

`diff -r 7f9164053b2a sage/plot/disk.py`
 a sage: P  = tl+tr+bl+br sage: P.show(figsize=(4,4),xmin=-2,xmax=2,ymin=-2,ymax=2) Extra options will get passed on to show(), as long as they are valid:: sage: disk((0, 0), 5, (0, pi/2), xmin=0, xmax=5, ymin=0, ymax=5, figsize=(2,2), rgbcolor=(1, 0, 1)) 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 """ from sage.plot.plot import Graphics g = Graphics() g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) g.add_primitive(Disk(point, radius, angle, options)) return g
• ## sage/plot/line.py

`diff -r 7f9164053b2a sage/plot/line.py`
 a sage: G + P + Q   # show the plot A line with no points or one point: sage: line([]) sage: line([(1,1)]) Extra options will get passed on to show(), as long as they are valid: sage: line([(0,1), (3,4)], figsize=[10, 2]) sage: line([(0,1), (3,4)]).show(figsize=[10, 2]) # These are equivalent """ from sage.plot.plot import Graphics, xydata_from_point_list xdata, ydata = xydata_from_point_list(points) g = Graphics() g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) g.add_primitive(Line(xdata, ydata, options)) return g
• ## sage/plot/matrix_plot.py

`diff -r 7f9164053b2a sage/plot/matrix_plot.py`
 a sage: import numpy sage: matrix_plot(numpy.random.rand(10, 10)) Extra options will get passed on to show(), as long as they are valid: sage: matrix_plot([[1, 0], [0, 1]], fontsize=10) sage: matrix_plot([[1, 0], [0, 1]]).show(fontsize=10) # These are equivalent TESTS: sage: P. = RR[] sage: matrix_plot(random_matrix(P, 3, 3)) yrange = (0, xy_data_array.shape[0]) g = Graphics() g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) g.add_primitive(MatrixPlot(xy_data_array, xrange, yrange, options)) return g
• ## sage/plot/plot.py

`diff -r 7f9164053b2a sage/plot/plot.py`
 a self.__tick_label_color = (0, 0, 0) self.__axes_width = 0.8 self.__objects = [] self._extra_kwds = {} def set_aspect_ratio(self, ratio): """ EXAMPLES:: sage: g1 = plot(abs(sqrt(x^3-1)), (x,1,5)) sage: g1 = plot(abs(sqrt(x^3-1)), (x,1,5), frame=True) sage: g2 = plot(-abs(sqrt(x^3-1)), (x,1,5), rgbcolor=(1,0,0)) sage: g1 + g2  # displays the plot TESTS:: sage: (g1 + g2)._extra_kwds # extra keywords to show are propagated {'frame': True} """ if isinstance(other, int) and other == 0: return self g = Graphics() g.__objects = self.__objects + other.__objects g.__aspect_ratio = max(self.__aspect_ratio, other.__aspect_ratio) g._extra_kwds.update(self._extra_kwds) g._extra_kwds.update(other._extra_kwds) return g def add_primitive(self, primitive): if z: g = g.translate(0,0,z) return g @classmethod def _extract_kwds_for_show(cls, kwds, ignore=[]): """ Extract keywords relevant to show() from the provided dictionary. EXAMPLES:: sage: kwds = {'f': lambda x: x, 'xmin': 0, 'figsize': [1,1], 'plot_points': (40, 40)} sage: G_kwds = Graphics._extract_kwds_for_show(kwds, ignore='xmin') sage: kwds # Note how this action modifies the passed dictionary {'xmin': 0, 'plot_points': (40, 40), 'f': at ...>} sage: G_kwds {'figsize': [1, 1]} This method is intended to be used with _set_extra_kwds(). Here is an idiom to ensure the correct keywords will get passed on to show():: sage: options = {} # Usually this will come from an argument sage: g = Graphics() sage: g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) """ result = {} for option in cls.SHOW_OPTIONS: if option not in ignore: try: result[option] = kwds.pop(option) except KeyError: pass return result def _set_extra_kwds(self, kwds): """ Set a dictionary of keywords that will get passed on to show(). TESTS:: def show(self, xmin=None, xmax=None, ymin=None, ymax=None, figsize=DEFAULT_FIGSIZE, filename=None, dpi=DEFAULT_DPI, axes=None, axes_labels=None,frame=False, fontsize=None, aspect_ratio=None, gridlines=None, gridlinesstyle=None, vgridlinesstyle=None, hgridlinesstyle=None): sage: g = Graphics() sage: g._extra_kwds {} sage: g._set_extra_kwds({'figsize': [10,10]}) sage: g._extra_kwds {'figsize': [10, 10]} sage: g.show() # Now the (blank) plot will be extra large """ self._extra_kwds = kwds # This dictionary has the default values for the keywords to show(). When # show is invoked with keyword arguments, those arguments are merged with # this dictionary to create a set of keywords with the defaults filled in. # NOTE: If you intend to use a new parameter in show(), you should update # this dictionary to contain the default value for that parameter. SHOW_OPTIONS = dict(xmin=None, xmax=None, ymin=None, ymax=None, figsize=DEFAULT_FIGSIZE, filename=None, dpi=DEFAULT_DPI, axes=None, axes_labels=None,frame=False, fontsize=None, aspect_ratio=None, gridlines=None, gridlinesstyle=None, vgridlinesstyle=None, hgridlinesstyle=None) def show(self, **kwds): """ Show this graphics image with the default image viewer. sage: M = MatrixSpace(QQ,10).random_element() sage: matrix_plot(M).show(gridlines=True) """ if DOCTEST_MODE: self.save(DOCTEST_MODE_FILE, xmin, xmax, ymin, ymax, figsize, dpi=dpi, axes=axes, axes_labels=axes_labels,frame=frame, aspect_ratio=aspect_ratio, gridlines=gridlines, gridlinesstyle=gridlinesstyle, vgridlinesstyle=vgridlinesstyle, hgridlinesstyle=hgridlinesstyle) options = {} options.update(self.SHOW_OPTIONS) options.update(self._extra_kwds) options.update(kwds) if DOCTEST_MODE or EMBEDDED_MODE: self.save(**options) return if EMBEDDED_MODE: if filename is None: filename = sage.misc.misc.graphics_filename() self.save(filename, xmin, xmax, ymin, ymax, figsize, dpi=dpi, axes=axes, axes_labels=axes_labels,frame=frame, aspect_ratio=aspect_ratio, gridlines=gridlines, gridlinesstyle=gridlinesstyle, vgridlinesstyle=vgridlinesstyle, hgridlinesstyle=hgridlinesstyle) html(""%filename) return if filename is None: filename = sage.misc.misc.tmp_filename() + '.png' self.save(filename, xmin, xmax, ymin, ymax, figsize, dpi=dpi, axes=axes, axes_labels=axes_labels, frame=frame, fontsize=fontsize, aspect_ratio=aspect_ratio, gridlines=gridlines, gridlinesstyle=gridlinesstyle, vgridlinesstyle=vgridlinesstyle, hgridlinesstyle=hgridlinesstyle) os.system('%s %s 2>/dev/null 1>/dev/null &'%(sage.misc.viewer.browser(), filename)) if options['filename'] is None: options['filename'] = sage.misc.misc.tmp_filename() + '.png' self.save(**options) os.system('%s %s 2>/dev/null 1>/dev/null &' % \ (sage.misc.viewer.browser(), options['filename'])) def xmin(self, xmin=None): """ sage: def b(n): return lambda x: bessel_J(n, x) + 0.5*(n-1) sage: plot([b(c) for c in [1..5]], 0, 40, fill = dict([(i, i+1) for i in [0..3]])) Extra options will get passed on to show(), as long as they are valid:: sage: plot(sin(x^2), (x, -3, 3), figsize=[8,2]) sage: plot(sin(x^2), (x, -3, 3)).show(figsize=[8,2]) # These are equivalent TESTS: ... RuntimeError: Error in line(): option 'foo' not valid. """ G_kwds = Graphics._extract_kwds_for_show(kwds, ignore=['xmin', 'xmax']) original_opts = kwds.pop('__original_opts', {}) do_show = kwds.pop('show',False) if hasattr(funcs, 'plot'): else: sage.misc.misc.verbose("there were %s extra arguments (besides %s)" % (n, funcs), level=0) G._set_extra_kwds(G_kwds) if do_show: G.show() return G
• ## sage/plot/plot_field.py

`diff -r 7f9164053b2a sage/plot/plot_field.py`
 a sage: x,y = var('x,y') sage: plot_vector_field( (-x/sqrt(x^2+y^2), -y/sqrt(x^2+y^2)), (x, -10, 10), (y, -10, 10)) sage: plot_vector_field( (-x/sqrt(x+y), -y/sqrt(x+y)), (x, -10, 10), (y, -10, 10)) Extra options will get passed on to show(), as long as they are valid:: sage: plot_vector_field((x, y), (x, -2, 2), (y, -2, 2), xmax=10) sage: plot_vector_field((x, y), (x, -2, 2), (y, -2, 2)).show(xmax=10) # These are equivalent """ from sage.plot.plot import setup_for_eval_on_grid, Graphics z, xstep, ystep, xrange, yrange = setup_for_eval_on_grid([f,g], xrange, yrange, options['plot_points']) xvec_array = numpy.ma.masked_invalid(numpy.array(xvec_array, dtype=float)) yvec_array = numpy.ma.masked_invalid(numpy.array(yvec_array, dtype=float)) g = Graphics() g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) g.add_primitive(PlotField(xpos_array, ypos_array, xvec_array, yvec_array, options)) return g
• ## sage/plot/point.py

`diff -r 7f9164053b2a sage/plot/point.py`
 a sage: point((1,2,3)) sage: point([(0,0), (1,1)]) sage: point([(0,0,1), (1,1,1)]) Extra options will get passed on to show(), as long as they are valid: sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)], frame=True) sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)]).show(frame=True) # These are equivalent """ try: return point2d(points, **kwds) from sage.plot.plot import xydata_from_point_list, Graphics xdata, ydata = xydata_from_point_list(points) g = Graphics() g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) g.add_primitive(Point(xdata, ydata, options)) return g
• ## sage/plot/polygon.py

`diff -r 7f9164053b2a sage/plot/polygon.py`
 a EXAMPLES: sage: polygon([(0,0), (1,1), (0,1)]) sage: polygon([(0,0,1), (1,1,1), (2,0,1)]) Extra options will get passed on to show(), as long as they are valid: sage: polygon([(0,0), (1,1), (0,1)], axes=False) sage: polygon([(0,0), (1,1), (0,1)]).show(axes=False) # These are equivalent """ try: return polygon2d(points, **options) from sage.plot.plot import xydata_from_point_list, Graphics xdata, ydata = xydata_from_point_list(points) g = Graphics() g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) g.add_primitive(Polygon(xdata, ydata, options)) return g
• ## sage/plot/scatter_plot.py

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

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