Ticket #5651: trac5651.patch
File trac5651.patch, 20.7 KB (added by , 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 310 310 If we want to draw the arrow between objects, for example, the 311 311 boundaries of two lines, we can use the arrowshorten option 312 312 to make the arrow shorter by a certain number of points. 313 313 314 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)) 314 315 316 Extra options will get passed on to show(), as long as they are valid: 315 317 318 sage: arrow((-2, 2), (7,1), frame=True) 319 sage: arrow((-2, 2), (7,1)).show(frame=True) 316 320 """ 317 321 from sage.plot.plot import Graphics 318 322 g = Graphics() 323 g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) 319 324 if headpoint is not None and tailpoint is not None: 320 325 xtail, ytail = tailpoint 321 326 xhead, yhead = headpoint -
sage/plot/bar_chart.py
diff -r 7f9164053b2a sage/plot/bar_chart.py
a b 112 112 113 113 A bar_chart with negative values and red bars: 114 114 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 115 119 """ 116 120 dl = len(datalist) 117 121 #if dl > 1: … … 127 131 #cnt += 1 128 132 129 133 g = Graphics() 134 g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) 130 135 #TODO: improve below for multiple data sets! 131 136 #cnt = 1 132 137 #for ind, pnts, xrange, yrange in bardata: -
sage/plot/bezier_path.py
diff -r 7f9164053b2a sage/plot/bezier_path.py
a b 166 166 sage: path = [[(0,0),(.5,1),(1,0)]] 167 167 sage: curve = bezier_path(path, linestyle='dashed', rgbcolor='green') 168 168 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 169 174 """ 170 175 from sage.plot.plot import Graphics 171 176 g = Graphics() 177 g._set_extra_kwds(g._extract_kwds_for_show(options)) 172 178 g.add_primitive(BezierPath(path, options)) 173 179 return g 174 180 -
sage/plot/circle.py
diff -r 7f9164053b2a sage/plot/circle.py
a b 143 143 ... 144 144 sage: g.show(xmin=-(paths+1)^2, xmax=(paths+1)^2, ymin=-(paths+1)^2, ymax=(paths+1)^2, figsize=[6,6]) 145 145 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 146 151 """ 147 152 from sage.plot.plot import Graphics 148 153 g = Graphics() 154 g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) 149 155 g.add_primitive(Circle(center[0], center[1], radius, options)) 150 156 return g -
sage/plot/complex_plot.pyx
diff -r 7f9164053b2a sage/plot/complex_plot.pyx
a b 235 235 236 236 The Riemann Zeta function: 237 237 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 238 242 """ 239 243 from sage.plot.plot import Graphics, setup_for_eval_on_grid 240 244 cdef double x, y … … 248 252 for y in yrange_list] 249 253 _sig_off 250 254 g = Graphics() 255 g._set_extra_kwds(Graphics._extract_kwds_for_show(options, ignore=['xmin', 'xmax'])) 251 256 g.add_primitive(ComplexPlot(z_values, xrange, yrange, options)) 252 257 return g -
sage/plot/contour_plot.py
diff -r 7f9164053b2a sage/plot/contour_plot.py
a b 149 149 sage: contour_plot(f, (-2, 2), (-2, 2), contours=(0.1, 1.0, 1.2, 1.4), cmap='hsv') 150 150 sage: contour_plot(f, (-2, 2), (-2, 2), contours=(1.0,), fill=False) 151 151 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 152 156 """ 153 157 from sage.plot.plot import Graphics, setup_for_eval_on_grid 154 158 g, xstep, ystep, xrange, yrange = setup_for_eval_on_grid([f], xrange, yrange, options['plot_points']) … … 157 161 for y in xsrange(yrange[0], yrange[1], ystep)] 158 162 159 163 g = Graphics() 164 g._set_extra_kwds(Graphics._extract_kwds_for_show(options, ignore=['xmin', 'xmax'])) 160 165 g.add_primitive(ContourPlot(xy_data_array, xrange, yrange, options)) 161 166 return g 162 167 -
sage/plot/density_plot.py
diff -r 7f9164053b2a sage/plot/density_plot.py
a b 123 123 variables x,y. 124 124 sage: density_plot(y^2 + 1 - x^3 - x, (y,-pi,pi), (x,-pi,pi)) 125 125 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 126 130 """ 127 131 from sage.plot.plot import Graphics, setup_for_eval_on_grid 128 132 g, xstep, ystep, xrange, yrange = setup_for_eval_on_grid([f], xrange, yrange, options['plot_points']) … … 131 135 for y in xsrange(yrange[0], yrange[1], ystep)] 132 136 133 137 g = Graphics() 138 g._set_extra_kwds(Graphics._extract_kwds_for_show(options, ignore=['xmin', 'xmax'])) 134 139 g.add_primitive(DensityPlot(xy_data_array, xrange, yrange, options)) 135 140 return g -
sage/plot/disk.py
diff -r 7f9164053b2a sage/plot/disk.py
a b 96 96 sage: P = tl+tr+bl+br 97 97 sage: P.show(figsize=(4,4),xmin=-2,xmax=2,ymin=-2,ymax=2) 98 98 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 99 102 """ 100 103 from sage.plot.plot import Graphics 101 104 g = Graphics() 105 g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) 102 106 g.add_primitive(Disk(point, radius, angle, options)) 103 107 return g -
sage/plot/line.py
diff -r 7f9164053b2a sage/plot/line.py
a b 317 317 sage: G + P + Q # show the plot 318 318 319 319 A line with no points or one point: 320 320 321 sage: line([]) 321 322 sage: line([(1,1)]) 322 323 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 323 328 """ 324 329 from sage.plot.plot import Graphics, xydata_from_point_list 325 330 xdata, ydata = xydata_from_point_list(points) 326 331 g = Graphics() 332 g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) 327 333 g.add_primitive(Line(xdata, ydata, options)) 328 334 return g 329 335 -
sage/plot/matrix_plot.py
diff -r 7f9164053b2a sage/plot/matrix_plot.py
a b 100 100 sage: import numpy 101 101 sage: matrix_plot(numpy.random.rand(10, 10)) 102 102 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 103 107 TESTS: 104 108 sage: P.<t> = RR[] 105 109 sage: matrix_plot(random_matrix(P, 3, 3)) … … 139 143 yrange = (0, xy_data_array.shape[0]) 140 144 141 145 g = Graphics() 146 g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) 142 147 g.add_primitive(MatrixPlot(xy_data_array, xrange, yrange, options)) 143 148 return g -
sage/plot/plot.py
diff -r 7f9164053b2a sage/plot/plot.py
a b 396 396 self.__tick_label_color = (0, 0, 0) 397 397 self.__axes_width = 0.8 398 398 self.__objects = [] 399 self._extra_kwds = {} 399 400 400 401 def set_aspect_ratio(self, ratio): 401 402 """ … … 1020 1021 1021 1022 EXAMPLES:: 1022 1023 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) 1024 1025 sage: g2 = plot(-abs(sqrt(x^3-1)), (x,1,5), rgbcolor=(1,0,0)) 1025 1026 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} 1026 1032 """ 1027 1033 if isinstance(other, int) and other == 0: 1028 1034 return self … … 1034 1040 g = Graphics() 1035 1041 g.__objects = self.__objects + other.__objects 1036 1042 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) 1037 1045 return g 1038 1046 1039 1047 def add_primitive(self, primitive): … … 1070 1078 if z: 1071 1079 g = g.translate(0,0,z) 1072 1080 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:: 1073 1117 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): 1080 1143 """ 1081 1144 Show this graphics image with the default image viewer. 1082 1145 … … 1268 1331 sage: M = MatrixSpace(QQ,10).random_element() 1269 1332 sage: matrix_plot(M).show(gridlines=True) 1270 1333 """ 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) 1279 1341 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'])) 1302 1348 1303 1349 def xmin(self, xmin=None): 1304 1350 """ … … 1887 1933 1888 1934 sage: def b(n): return lambda x: bessel_J(n, x) + 0.5*(n-1) 1889 1935 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 1890 1941 1891 1942 TESTS: 1892 1943 … … 1942 1993 ... 1943 1994 RuntimeError: Error in line(): option 'foo' not valid. 1944 1995 """ 1996 G_kwds = Graphics._extract_kwds_for_show(kwds, ignore=['xmin', 'xmax']) 1997 1945 1998 original_opts = kwds.pop('__original_opts', {}) 1946 1999 do_show = kwds.pop('show',False) 1947 2000 if hasattr(funcs, 'plot'): … … 1980 2033 else: 1981 2034 sage.misc.misc.verbose("there were %s extra arguments (besides %s)" % (n, funcs), level=0) 1982 2035 2036 G._set_extra_kwds(G_kwds) 1983 2037 if do_show: 1984 2038 G.show() 1985 2039 return G -
sage/plot/plot_field.py
diff -r 7f9164053b2a sage/plot/plot_field.py
a b 151 151 sage: x,y = var('x,y') 152 152 sage: plot_vector_field( (-x/sqrt(x^2+y^2), -y/sqrt(x^2+y^2)), (x, -10, 10), (y, -10, 10)) 153 153 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 154 159 """ 155 160 from sage.plot.plot import setup_for_eval_on_grid, Graphics 156 161 z, xstep, ystep, xrange, yrange = setup_for_eval_on_grid([f,g], xrange, yrange, options['plot_points']) … … 168 173 xvec_array = numpy.ma.masked_invalid(numpy.array(xvec_array, dtype=float)) 169 174 yvec_array = numpy.ma.masked_invalid(numpy.array(yvec_array, dtype=float)) 170 175 g = Graphics() 176 g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) 171 177 g.add_primitive(PlotField(xpos_array, ypos_array, xvec_array, yvec_array, options)) 172 178 return g 173 179 -
sage/plot/point.py
diff -r 7f9164053b2a sage/plot/point.py
a b 124 124 sage: point((1,2,3)) 125 125 sage: point([(0,0), (1,1)]) 126 126 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 127 131 """ 128 132 try: 129 133 return point2d(points, **kwds) … … 150 154 from sage.plot.plot import xydata_from_point_list, Graphics 151 155 xdata, ydata = xydata_from_point_list(points) 152 156 g = Graphics() 157 g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) 153 158 g.add_primitive(Point(xdata, ydata, options)) 154 159 return g 155 160 -
sage/plot/polygon.py
diff -r 7f9164053b2a sage/plot/polygon.py
a b 89 89 EXAMPLES: 90 90 sage: polygon([(0,0), (1,1), (0,1)]) 91 91 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 92 96 """ 93 97 try: 94 98 return polygon2d(points, **options) … … 163 167 from sage.plot.plot import xydata_from_point_list, Graphics 164 168 xdata, ydata = xydata_from_point_list(points) 165 169 g = Graphics() 170 g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) 166 171 g.add_primitive(Polygon(xdata, ydata, options)) 167 172 return g -
sage/plot/scatter_plot.py
diff -r 7f9164053b2a sage/plot/scatter_plot.py
a b 120 120 sage: s = scatter_plot([[0,1],[2,2],[4.3,1.1]], marker='s') 121 121 sage: s 122 122 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 123 126 """ 124 127 import numpy 125 128 from sage.plot.plot import Graphics 126 129 g = Graphics() 130 g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) 127 131 data = numpy.array(datalist, dtype='float') 128 132 if len(data) != 0: 129 133 xdata = data[:,0] -
sage/plot/text.py
diff -r 7f9164053b2a sage/plot/text.py
a b 119 119 ... 120 120 ValueError: use text3d instead for text in 3d 121 121 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 122 126 """ 123 127 try: 124 128 x, y = xy … … 130 134 options['rgbcolor'] = to_mpl_color(options['rgbcolor']) 131 135 point = (float(x), float(y)) 132 136 g = Graphics() 137 g._set_extra_kwds(Graphics._extract_kwds_for_show(options, ignore='fontsize')) 133 138 g.add_primitive(Text(string, point, options)) 134 139 return g