From: Charles <ct...@cs...> - 2003-11-12 01:45:42
|
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). Is this comment better for -users or -devel? Or bug tracking? -Charles # -*- Mode: Python; py-indent-offset: 4 -*- # Simple example to demonstrate ticklabel problem # If you don't set_ticks first, the following works fine. # If you do set_ticks, then ([]) doesn't blank the ticklabels. # --crt from matplotlib.matlab import * NUMPLOTS = 8 COLS = 3 ROWS = NUMPLOTS / COLS if NUMPLOTS % COLS: ROWS += 1 t = arange(0.0, 2.0, 0.01) s = sin(2*pi*t) for i in range(1,NUMPLOTS+1): ax = subplot (ROWS, COLS, i) plot(t, s) for i in range(1,NUMPLOTS+1): ax = subplot (ROWS, COLS, i) ax.set_xticks((0,1,2)) # HERE HERE HERE title('Simple ' + str(i)) if i % COLS == 1: # left edge ax.set_ylabel('voltage (mV)') else: ax.set_yticklabels ([]) if i > NUMPLOTS - COLS: # nothing below them ax.set_xlabel('time (s)') else: ax.set_xticklabels ([]) savefig('simple_plot') show() -- Charles R. Twardy www.csse.monash.edu.au/~ctwardy Monash University sarbayes.org Computer Sci. & Software Eng. +61(3) 9905 5823 (w) 5146 (fax) |
From: Andrew H. J. <a.h...@gm...> - 2013-03-14 15:32:23
|
Dear all, None of the obvious ways for changing ticklabels seem to work for the current version of Matplotlib (1.2.0 for me). At present, ax.yaxis.get_ticklabels().get_text() returns empty strings, as does ax.get_yticklabels(), and the equivalent set_* functions don't seem to have any effect. So: help! Yours, Andrew |
From: Goyo <goy...@gm...> - 2013-03-14 21:04:04
|
2013/3/14 Andrew H. Jaffe <a.h...@gm...>: > Dear all, > > None of the obvious ways for changing ticklabels seem to work for the current version of Matplotlib (1.2.0 for me). At present, ax.yaxis.get_ticklabels().get_text() returns empty strings, as does ax.get_yticklabels(), and the equivalent set_* functions don't seem to have any effect. It seems to be working for me. I'm using a development version but I don't thing this has changed. In [1]: import matplotlib.pyplot as plt In [2]: plt.get_backend() Out[2]: 'TkAgg' In [3]: plt.plot([1, 2]) Out[3]: [<matplotlib.lines.Line2D at 0x424c310>] In [4]: plt.draw() In [5]: labels = plt.gca().get_yticklabels() In [6]: map(lambda x: x.get_text(), labels) Out[6]: [u'1.0', u'1.2', u'1.4', u'1.6', u'1.8', u'2.0', u'2.2', ''] In [7]: plt.gca().set_yticklabels(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']) Out[7]: [<matplotlib.text.Text at 0x3e97bd0>, <matplotlib.text.Text at 0x3ea2390>, <matplotlib.text.Text at 0x42bed90>, <matplotlib.text.Text at 0x42c0390>, <matplotlib.text.Text at 0x42c05d0>, <matplotlib.text.Text at 0x42c0c50>, <matplotlib.text.Text at 0x42c1310>, <matplotlib.text.Text at 0x42c1990>] In [8]: map(lambda x: x.get_text(), labels) Out[8]: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] Goyo |
From: ChaoYue <cha...@gm...> - 2013-03-14 21:16:09
|
it also works for me for 1.2.0 In [1]: mat.__version__ Out[1]: '1.2.0' In [2]: plot(np.arange(5),'ro') Out[2]: [<matplotlib.lines.Line2D at 0xc88262c>] In [3]: ax = gca() In [4]: ax.set_xticklabels('abcdefghij') Out[4]: [<matplotlib.text.Text at 0xbbb048c>, <matplotlib.text.Text at 0xbbb0c8c>, <matplotlib.text.Text at 0xc88780c>, <matplotlib.text.Text at 0xc887e2c>, <matplotlib.text.Text at 0xc88b4cc>, <matplotlib.text.Text at 0xc88bb4c>, <matplotlib.text.Text at 0xc8911ec>, <matplotlib.text.Text at 0xc89186c>, <matplotlib.text.Text at 0xc891eec>] In [5]: [t.get_text() for t in ax.get_xticklabels()] Out[5]: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] -- View this message in context: http://matplotlib.1069221.n5.nabble.com/ticklabels-tp40656p40663.html Sent from the matplotlib - users mailing list archive at Nabble.com. |
From: John H. <jdh...@ac...> - 2003-11-12 04:51:12
|
>>>>> "Charles" == Charles <ct...@cs...> writes: Charles> Hi John, I added one line that shows the problem. If you Charles> do a set_xticks() first, then the usual ([]) doesn't Charles> blank them. Here's code that should suppress the xticks Charles> for all but the bottom-most graphs in each column. It Charles> works if you comment out the set_xticks line (HERE HERE Charles> 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 Charles> Is this comment better for -users or -devel? Or bug Charles> 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 |
From: Charles <ct...@cs...> - 2003-11-12 06:58:49
|
Hi John, The patch worked for me. :-) 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) |