Andrew Straw wrote:
Gary Ruben wrote:
This looks nice Andrew,
I haven't tried it, but I wonder whether it's possible to add a keyword arg to suppress the 0's at the origin which are cut through by the axes in the zeroed case (and/or possibly shift the 0 on the horizontal axis left). The same thing is happening in the (1,2) case on the vertical axis.
Hi Gary,

John also suggested something like this. I don't think it's impossible, but it's outside the scope of the work I have done and beyond my immediate familiarity with the code base. I think it would involve looking at the tick label bounding boxes, finding overlaps, and then deciding which label was less important and removing it. I don't think it would be impossible, and maybe not even hard, but I haven't investigated at all. Thanks for keeping it on the queue.



I did something like this for the Sage project, but I did it on the tick formatter level. I just made a tick formatter that omitted specified positions. I've attached the "Selective Formatter" class. If anyone has comments, I'd appreciate them! Basically, I just replaced the formatter with the selective formatter, which passed things on to the previous formatter if the tick label was allowed.

It would be great if spines supported this sort of thing natively, but the flexibility of tick formatters seems like a nice place to handle it as well.

Thanks,

Jason




from matplotlib.ticker import Formatter

class SelectiveFormatter(Formatter):
    """
    This matplotlib formatter selectively omits some tick values and
    passes the rest on to a specified formatter.

    EXAMPLES:
    
    This example is almost straight from a matplotlib example.
    
    ::

        sage: from sage.plot.plot import SelectiveFormatter
        sage: import matplotlib.pyplot as plt
        sage: import numpy
        sage: fig=plt.figure()
        sage: ax=fig.add_subplot(111)
        sage: t = numpy.arange(0.0, 2.0, 0.01)
        sage: s = numpy.sin(2*numpy.pi*t)
        sage: line=ax.plot(t, s)
        sage: 
formatter=SelectiveFormatter(ax.xaxis.get_major_formatter(),skip_values=[0,1])
        sage: ax.xaxis.set_major_formatter(formatter)
        sage: fig.savefig(os.path.join(SAGE_TMP, 'test.png'))
    """
    def __init__(self, formatter,skip_values):
        """
        Initialize a SelectiveFormatter object.

        INPUT:

          - formatter -- the formatter object to which we should pass labels

          - skip_values -- a list of values that we should skip when
            formatting the tick labels

        EXAMPLES::

            sage: from sage.plot.plot import SelectiveFormatter
            sage: import matplotlib.pyplot as plt
            sage: import numpy
            sage: fig=plt.figure()
            sage: ax=fig.add_subplot(111)
            sage: t = numpy.arange(0.0, 2.0, 0.01)
            sage: s = numpy.sin(2*numpy.pi*t)
            sage: line=ax.plot(t, s)
            sage: 
formatter=SelectiveFormatter(ax.xaxis.get_major_formatter(),skip_values=[0,1])
            sage: ax.xaxis.set_major_formatter(formatter)
            sage: fig.savefig(os.path.join(SAGE_TMP, 'test.png'))
        """
        self.formatter=formatter
        self.skip_values=skip_values
    def set_locs(self, locs):
        """
        Set the locations for the ticks that are not skipped.
        
        EXAMPLES::
            sage: from sage.plot.plot import SelectiveFormatter
            sage: import matplotlib.ticker
            sage: 
formatter=SelectiveFormatter(matplotlib.ticker.Formatter(),skip_values=[0,200])
            sage: formatter.set_locs([i*100 for i in range(10)])
        """
        self.formatter.set_locs([l for l in locs if l not in self.skip_values])
    def __call__(self, x, *args, **kwds):
        """
        Return the format for tick val *x* at position *pos*

        EXAMPLES::

            sage: from sage.plot.plot import SelectiveFormatter
            sage: import matplotlib.ticker
            sage: 
formatter=SelectiveFormatter(matplotlib.ticker.FixedFormatter(['a','b']),skip_values=[0,2])
            sage: [formatter(i,1) for i in range(10)]
            ['', 'b', '', 'b', 'b', 'b', 'b', 'b', 'b', 'b']            
        """
        if x in self.skip_values:
            return ''
        else:
            return self.formatter(x, *args, **kwds)

------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Reply via email to