ticklabels

Hi John, I added one line that shows the problem. If you

    > do a set_xticks() first, then the usual ([]) doesn't
    > blank them. Here's code that should suppress the xticks
    > for all but the bottom-most graphs in each column. It
    > works if you comment out the set_xticks line (HERE HERE
    > HERE).

Hi Charles,

As an aside, you'll be interested in these super secret undocumented
methods of the Subplot class, which I implemented so I wouldn't have
to do the if i % COLS == 1 tricks that I came to know and love in
matlab. I too find myself making lots-o-subplots and blanking out the
labels

Subplot methods:

    def is_first_col(self):
    def is_first_row(self):
    def is_last_row(self):
    def is_last_col(self):

which enables you to write your loop

for i in range(1,NUMPLOTS+1):
    ax = subplot (ROWS, COLS, i)
    ax.set_xticks((0,1,2))
    title('Simple ' + str(i))
    
    if ax.is_first_col(): ax.set_ylabel('voltage (mV)')
    else: ax.set_yticklabels ([])

    if ax.is_last_row(): ax.set_xlabel('time (s)')
    else: ax.set_xticklabels ([])

Now onto your problem. Thanks for the example script. I am not sure
which version of matplotlib you are working with, but it appears there
is a clear bug in Axis.set_ticklabels in the part which reads

        for s, label in zip(self._ticklabelStrings, self._ticklabels):
            label.set_text(s)
            label.update_properties(override)

when len(self._ticklabelStrings) is less than len(self._ticklabels),
the label text doesn't get updated. Duh!

Something like this should work better. At least with
matplotlib-0.32a it handles your example

    def set_ticklabels(self, ticklabels, *args, **kwargs):
        """
        Set the text values of the tick labels. ticklabels is a
        sequence of strings. Return a list of AxisText instances
        """
        ticklabels = ['%s'%l for l in ticklabels]
        
        self._ticklabelStrings = ticklabels
        override = {}
        override = backends._process_text_args(override, *args, **kwargs)

        Nnew = len(self._ticklabelStrings)
        existingLabels = self.get_ticklabels()
        for i, label in enumerate(existingLabels):
            if i<Nnew: label.set_text(self._ticklabelStrings[i])
            else: label.set_text('')
            label.update_properties(override)
        return existingLabels

    > Is this comment better for -users or -devel? Or bug
    > tracking?

I think users, since my answer includes a *possible* fix which others
may find useful. Let me know how this works for you; I'll take a
closer look on Thurs when I have some breathing room again.

JDH

Hi John,

The patch worked for me. :slight_smile:

JH: def is_last_row(self):

Nice, but maybe not enough. If the plots don't fill the bottom row, (say 7
plots on a 3x3 grid), it'll leave some columns with no xticklabels. We'd
need something like
  is_bottom()
But that would require knowing how many subplots there are, which subplot
doesn't do, and probably shouldn't.

-C

···

--
Charles R. Twardy www.csse.monash.edu.au/~ctwardy
Monash University sarbayes.org
Computer Sci. & Software Eng.
+61(3) 9905 5823 (w) 5146 (fax)