# HG changeset patch
# User Jason Grout <jason.grout@drake.edu>
# Date 1321327539 21600
# Node ID 35351bab123bd4db825319eb2d5a935b57485d76
# Parent 89ad92b3241d9194cc2d67119dd05c51beadd8f3
Fix the aspect_ratio() and set_aspect_ratio() functions to be consistent with the aspect_ratio show keyword. Fix documentation as well.
diff git a/sage/plot/plot.py b/sage/plot/plot.py
a

b


81  81  
82  82  sage: circle((1,1), 1) + plot(x^2, (x,0,5)) 
83  83  
84   Notice that the above circle is not round, because the aspect ratio 
85   of the coordinate system is not 1:1. The 
86   ``aspect_ratio`` option to show allows us to fix 
87   this:: 
88   
89   sage: show(circle((1,1), 1) + plot(x^2, (x,0,5)), aspect_ratio=1) 
 84  Notice that the aspect ratio of the above plot makes the plot very tall because 
 85  the plot adopts the default aspect ratio of the circle (to make the circle appear 
 86  like a circle). We can change the aspect ratio to be what we normally expect for a plot 
 87  by explicitly asking for an 'automatic' aspect ratio:: 
 88  
 89  sage: show(circle((1,1), 1) + plot(x^2, (x,0,5)), aspect_ratio='automatic') 
90  90  
91  91  The aspect ratio describes the apparently height/width ratio of a unit square. If you want the vertical units to be twice as big as the horizontal units, specify an aspect ratio of 2:: 
92  92  
… 
… 

169  169  
170  170  Many concentric circles shrinking toward the origin:: 
171  171  
172   sage: show(sum(circle((i,0), i, hue=sin(i/10)) for i in [10,9.9,..,0]), aspect_ratio=1) 
 172  sage: show(sum(circle((i,0), i, hue=sin(i/10)) for i in [10,9.9,..,0])) 
173  173  
174  174  Here is a pretty graph:: 
175  175  
… 
… 

454  454  
455  455  sage: G = Graphics() 
456  456  """ 
457   self.__aspect_ratio = 1.0 
458  457  self.__fontsize = 10 
459  458  self.__show_axes = True 
460  459  self.__show_legend = False 
… 
… 

479  478   ``ratio``  a positive real number or 'automatic' 
480  479  
481  480  
482   EXAMPLES: We create a plot of a circle, and it doesn't look quite 
483   round:: 
484   
485   sage: P = circle((1,1), 1); P 
 481  EXAMPLES: We create a plot of the upper half of a circle, but it 
 482  doesn't look round because the aspect ratio is off:: 
 483  
 484  sage: P = plot(sqrt(1x^2),(x,1,1)); P 
486  485  
487  486  So we set the aspect ratio and now it is round:: 
488  487  
489  488  sage: P.set_aspect_ratio(1) 
 489  sage: P.aspect_ratio() 
 490  1.0 
490  491  sage: P 
491  492  
492  493  Note that the aspect ratio is inherited upon addition (which takes 
493  494  the max of aspect ratios of objects whose aspect ratio has been 
494  495  set):: 
495  496  
496   sage: P + circle((0,0), 0.5) # still square 
 497  sage: P + plot(sqrt(4x^2),(x,2,2)) 
497  498  
498  499  In the following example, both plots produce a circle that looks 
499  500  twice as tall as wide:: 
500  501  
501  502  sage: Q = circle((0,0), 0.5); Q.set_aspect_ratio(2) 
502   sage: P + Q 
503   sage: Q + P 
 503  sage: (P + Q).aspect_ratio(); P+Q 
 504  2.0 
 505  sage: (Q + P).aspect_ratio(); Q+P 
 506  2.0 
504  507  """ 
505  508  if ratio != 'auto' and ratio != 'automatic': 
506  509  ratio = float(ratio) 
… 
… 

508  511  raise ValueError, "the aspect ratio must be positive or 'automatic'" 
509  512  else: 
510  513  ratio = 'automatic' 
511   self.__aspect_ratio = ratio 
 514  self._extra_kwds['aspect_ratio'] = ratio 
512  515  
513  516  def aspect_ratio(self): 
514  517  """ 
… 
… 

530  533  sage: P.aspect_ratio() 
531  534  'automatic' 
532  535  """ 
533   return self.__aspect_ratio 
 536  return self._extra_kwds.get('aspect_ratio',1.0) 
534  537  
535  538  def legend(self, show=None): 
536  539  r""" 
… 
… 

1273  1276  
1274  1277  sage: (g1 + g2)._extra_kwds=={'aspect_ratio': 'automatic', 'frame': True} 
1275  1278  True 
 1279  sage: g1.set_aspect_ratio(2) 
 1280  sage: (g1+g2).aspect_ratio() 
 1281  2.0 
 1282  sage: g2.set_aspect_ratio(3) 
 1283  sage: (g1+g2).aspect_ratio() 
 1284  3.0 
1276  1285  """ 
1277  1286  if isinstance(other, int) and other == 0: 
1278  1287  return self 
… 
… 

1283  1292  raise TypeError, "other (=%s) must be a Graphics objects"%other 
1284  1293  g = Graphics() 
1285  1294  g.__objects = self.__objects + other.__objects 
1286   if self.__aspect_ratio=='automatic': 
1287   g.__aspect_ratio=other.__aspect_ratio 
1288   elif other.__aspect_ratio=='automatic': 
1289   g.__aspect_ratio=self.__aspect_ratio 
1290   else: 
1291   g.__aspect_ratio = max(self.__aspect_ratio, other.__aspect_ratio) 
1292  1295  g.__show_legend = self.__show_legend or other.__show_legend 
1293  1296  g._extra_kwds.update(self._extra_kwds) 
1294  1297  g._extra_kwds.update(other._extra_kwds) 
 1298  if self.aspect_ratio()=='automatic': 
 1299  g.set_aspect_ratio(other.aspect_ratio()) 
 1300  elif other.aspect_ratio()=='automatic': 
 1301  g.set_aspect_ratio(self.aspect_ratio()) 
 1302  else: 
 1303  g.set_aspect_ratio( max(self.aspect_ratio(), other.aspect_ratio())) 
1295  1304  return g 
1296  1305  
1297  1306  def add_primitive(self, primitive): 
… 
… 

1419  1428  dimensions corresponding to ``figsize``. 
1420  1429  
1421  1430   ``aspect_ratio``  the perceived height divided by the 
1422   perceived width. If the aspect ratio is set to ``1``, circles 
 1431  perceived width. For example, if the aspect ratio is set to ``1``, circles 
1423  1432  will look round and a unit square will appear to have sides 
1424   of equal length. If the aspect ratio is set ``2``, vertical units will be 
 1433  of equal length, and if the aspect ratio is set ``2``, vertical units will be 
1425  1434  twice as long as horizontal units, so a unit square will be twice as 
1426  1435  high as it is wide. If set to ``'automatic'``, the aspect ratio 
1427  1436  is determined by ``figsize`` and the picture fills the figure. 