Ticket #4529: trac_4529-patch2.patch

File trac_4529-patch2.patch, 4.5 KB (added by kcrisman, 9 years ago)
  • sage/plot/graphics.py

    # HG changeset patch
    # User Punarbasu Purkayastha <ppurka@gmail.com>
    # Date 1338109823 -28800
    # Node ID 9161e5333271aa7d649f365b0dcb069e46fe8f2c
    # Parent  35ef31cc28aa5c31bda29843638d8898ba4fc5e2
    check for the presence of single tick
      - this is not robust enough in all cases for the log scale
      - including this patch will mean that most graphics such as arc, disk, etc
        which "just work" with log scale, will stop working.
    
    diff --git a/sage/plot/graphics.py b/sage/plot/graphics.py
    a b  
    14591459
    14601460            sage: G.show(scale=('loglog', 2, 3)) # x in base 2, y in base 3
    14611461
    1462         The base need not be an integer.::
    1463 
    1464             sage: G.show(scale='semilogy', base=float(e)) # base is e
     1462        The base need not be an integer, though it does have to be made
     1463        a float.  Also, currently the formatting is wrong for non-integer bases,
     1464        such as in this example::
     1465
     1466            sage: G.show(scale='semilogx', base=float(e)) # base is e
    14651467
    14661468        Logarithmic scale can be used for various kinds of plots. Here are
    14671469        some examples.::
     
    14721474            sage: G = parametric_plot((x, x**2), (x, 1, 10))
    14731475            sage: G.show(scale='loglog')
    14741476
     1477            sage: disk((5,5), 4, (0, 3*pi/2)).show(scale='loglog',base=2)
     1478
     1479            sage: x, y = var('x, y')
     1480            sage: G =  plot_vector_field((2^x,y^2),(x,1,10),(y,1,100))
     1481            sage: G.show(scale='semilogx',base=2)
     1482
     1483        But be sure to only plot things that will have a wide enough range
     1484        for the logarithmic scale to be interpretable::
     1485
    14751486            sage: G = arc((2,3), 2, 1, angle=pi/2, sector=(0,pi/2))
    14761487            sage: G.show(scale=('loglog', 2))
    1477 
    1478             sage: disk((0.1,0.1), 1, (pi/3, pi/2)).show(scale='loglog')
    1479 
    1480             sage: polygon([(1,1), (10,10), (10,1)]).show(scale='loglog')
    1481 
    1482             sage: x, y = var('x, y')
    1483             sage: G =  plot_vector_field((sin(x),cos(y)),(x,0.1,3),(y,0.1,3))
    1484             sage: G.show(scale='loglog')
     1488            Traceback (most recent call last):
     1489            ...
     1490            ValueError: Either expand the range of the dependent variable to allow two different integer powers of your `base`, or change your `base` to a smaller number.
    14851491
    14861492        Add grid lines at the major ticks of the axes.
    14871493       
     
    16631669
    16641670            sage: plot(arcsin(x),(x,-1,1),ticks=[None,pi/6],tick_formatter=[None,pi]) # Not so nice-looking
    16651671
     1672        When using logarithmic scale along the axis, make sure to have
     1673        enough room for two ticks so that the user can tell what the scale
     1674        is. This can be effected by increasing the range of the independent
     1675        variable, or by changing the ``base``.::
     1676
     1677            sage: p = list_plot(range(1, 10), plotjoined=True)
     1678            sage: p.show(scale='loglog')
     1679            Traceback (most recent call last):
     1680            ...
     1681            ValueError: Either expand the range of the dependent variable to allow two different integer powers of your `base`, or change your `base` to a smaller number.
     1682            sage: p.show(scale='loglog', base=8) # this works.
     1683
    16661684        """
    16671685
    16681686        # This option should not be passed on to save().
     
    19241942        subplot.xaxis.set_major_formatter(x_formatter)
    19251943        subplot.yaxis.set_major_formatter(y_formatter)
    19261944
     1945        # Check for whether there will be too few ticks in the log scale case
     1946        # If part of the data is nonpositive, we assume there are enough ticks
     1947        if scale[0] == 'log' and xmin > 0:
     1948            import math
     1949            base0 = base[0]
     1950            if (math.floor(math.log(xmax)/math.log(base0)) -
     1951                    math.ceil(math.log(xmin)/math.log(base0)) < 1):
     1952                raise ValueError('Either expand the range of the independent '
     1953                'variable to allow two different integer powers of your `base`, '
     1954                'or change your `base` to a smaller number.')
     1955        if scale[1] == 'log' and ymin > 0:
     1956            import math
     1957            base1 = base[1]
     1958            if (math.floor(math.log(ymax)/math.log(base1)) -
     1959                    math.ceil(math.log(ymin)/math.log(base1)) < 1):
     1960                raise ValueError('Either expand the range of the dependent '
     1961                'variable to allow two different integer powers of your `base`, '
     1962                'or change your `base` to a smaller number.')
     1963
    19271964        return (subplot, x_locator, y_locator, x_formatter, y_formatter)
    19281965
    19291966    def matplotlib(self, filename=None,