Ticket #11963: trac-11963-fix-aspect-ratio.patch

File trac-11963-fix-aspect-ratio.patch, 5.8 KB (added by jason, 9 years ago)
  • sage/plot/plot.py

    # 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  
    8181
    8282    sage: circle((1,1), 1) + plot(x^2, (x,0,5))
    8383
    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)
     84Notice that the aspect ratio of the above plot makes the plot very tall because
     85the plot adopts the default aspect ratio of the circle (to make the circle appear
     86like a circle).  We can change the aspect ratio to be what we normally expect for a plot
     87by explicitly asking for an 'automatic' aspect ratio::
     88
     89    sage: show(circle((1,1), 1) + plot(x^2, (x,0,5)), aspect_ratio='automatic')
    9090
    9191The 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::
    9292
     
    169169
    170170Many concentric circles shrinking toward the origin::
    171171
    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]))
    173173
    174174Here is a pretty graph::
    175175
     
    454454       
    455455            sage: G = Graphics()
    456456        """
    457         self.__aspect_ratio = 1.0
    458457        self.__fontsize = 10
    459458        self.__show_axes = True
    460459        self.__show_legend = False
     
    479478        -  ``ratio`` - a positive real number or 'automatic'
    480479       
    481480       
    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(1-x^2),(x,-1,1)); P
    486485       
    487486        So we set the aspect ratio and now it is round::
    488487       
    489488            sage: P.set_aspect_ratio(1)
     489            sage: P.aspect_ratio()
     490            1.0
    490491            sage: P
    491492       
    492493        Note that the aspect ratio is inherited upon addition (which takes
    493494        the max of aspect ratios of objects whose aspect ratio has been
    494495        set)::
    495496       
    496             sage: P + circle((0,0), 0.5)           # still square
     497            sage: P + plot(sqrt(4-x^2),(x,-2,2))
    497498       
    498499        In the following example, both plots produce a circle that looks
    499500        twice as tall as wide::
    500501       
    501502            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
    504507        """
    505508        if ratio != 'auto' and ratio != 'automatic':
    506509            ratio = float(ratio)
     
    508511                raise ValueError, "the aspect ratio must be positive or 'automatic'"
    509512        else:
    510513            ratio = 'automatic'
    511         self.__aspect_ratio = ratio
     514        self._extra_kwds['aspect_ratio'] = ratio
    512515
    513516    def aspect_ratio(self):
    514517        """
     
    530533            sage: P.aspect_ratio()
    531534            'automatic'
    532535        """
    533         return self.__aspect_ratio
     536        return self._extra_kwds.get('aspect_ratio',1.0)
    534537
    535538    def legend(self, show=None):
    536539        r"""
     
    12731276
    12741277            sage: (g1 + g2)._extra_kwds=={'aspect_ratio': 'automatic', 'frame': True}
    12751278            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
    12761285        """
    12771286        if isinstance(other, int) and other == 0:
    12781287            return self
     
    12831292            raise TypeError, "other (=%s) must be a Graphics objects"%other
    12841293        g = Graphics()
    12851294        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)
    12921295        g.__show_legend = self.__show_legend or other.__show_legend
    12931296        g._extra_kwds.update(self._extra_kwds)
    12941297        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()))
    12951304        return g
    12961305 
    12971306    def add_primitive(self, primitive):
     
    14191428          dimensions corresponding to ``figsize``.
    14201429
    14211430        - ``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
    14231432          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
    14251434          twice as long as horizontal units, so a unit square will be twice as
    14261435          high as it is wide.  If set to ``'automatic'``, the aspect ratio
    14271436          is determined by ``figsize`` and the picture fills the figure.