sharex with different tick labels

Greetings, all.

I have an issue: I have several axes stacked in a column with a common time vector on each x-axis. Each plot is a contour, so overplotting is not an option. In a perfect world, I want the following:

  1. The subplots are tightly spaced such that with ax.grid() activated, the grid lines appear continuous. This makes comparing simultaneous characteristics between subplots very easy.

  2. The subplots are linked via the “sharex” keyword so I can move them all in unison.

  3. Only the bottommost subplot has x tick labels; on other plots, the long time-formatted labels stick out of the left and right of the plots.

Items 2 and 3 are contradictory: if I turn off tick labels (e.g. ax.set_xticklabels(’’)) on one axes, the others turn off as well, including the bottom axes. That is bad.

Does anyone know of a good workaround for this?

Thanks for your help.

-dw

Hi Daniel,

2012/9/13 Daniel Welling <dantwelling@...287...>

Greetings, all.

I have an issue: I have several axes stacked in a column with a common time vector on each x-axis. Each plot is a contour, so overplotting is not an option. In a perfect world, I want the following:
1) The subplots are tightly spaced such that with ax.grid() activated, the grid lines appear continuous. This makes comparing simultaneous characteristics between subplots very easy.
2) The subplots are linked via the "sharex" keyword so I can move them all in unison.
3) Only the bottommost subplot has x tick labels; on other plots, the long time-formatted labels stick out of the left and right of the plots.

Items 2 and 3 are contradictory: if I turn off tick labels (e.g. ax.set_xticklabels('')) on one axes, the others turn off as well, including the bottom axes. That is bad.
Does anyone know of a good workaround for this?

To obtain what you want you have to set the tick labels invisible is
the axes where you don't want them to show up.

From http://matplotlib.org/examples/pylab_examples/shared_axis_demo.html:

setp( ax.get_xticklabels(), visible=False)

As the axis are shared, the ticklabels are also shared, so setting
them to "", erase them from all the axes.
I hope that this helps.

Cheers,
Francesco

···

Thanks for your help.

-dw

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Daniel,

I found that I came across this often, so I created three functions (one for sharing x, one for y, and one for both). In looking over them right now, there may be some inconsistencies between their style, but the idea is there. I am pasting them below in case they are useful to someone else. The original ideas came from figure.autofmt_xdate.

-Sterling

def autofmt_sharexy(fig=None):
if fig==None:
  fig = gcf()
for a in fig.axes:
  if a.is_first_col():
   if not a.is_first_row():
    a.get_yticklabels()[-1].set_visible(False)
  else:
   for yl in a.get_yticklabels():
    yl.set_visible(False)
   a.set_ylabel('')
  if not a.is_last_row():
   for xl in a.get_xticklabels():
    xl.set_visible(False)
   a.set_xlabel('')
  else:
   if not a.is_last_col():
    a.get_xticklabels()[-1].set_visible(False)
subplots_adjust(hspace=0,wspace=0)

def autofmt_sharey(trim_xlabel=True,fig=None):
if fig==None:
  fig = gcf()
from matplotlib.ticker import MaxNLocator
for a in fig.axes:
  if a.is_first_col():
   if not a.is_first_row():
    a.get_yticklabels()[-1].set_visible(False)
  else:
   for yl in a.get_yticklabels():
    yl.set_visible(False)
   a.set_ylabel('')
   if trim_xlabel:
    a.get_xticklabels()[0].set_visible(False)
  if not trim_xlabel:
   a.xaxis.set_major_locator(MaxNLocator(6))
   xtl = a.get_xticklabels()
   setp(xtl[0::2],visible=False)
subplots_adjust(wspace=0)

def autofmt_sharex(fig=None):
   if fig==None:
    fig = gcf()
   nax=len(fig.get_axes())
   if nax==0: return
   try:
      nr=fig.axes[0].numRows
      nc=fig.axes[0].numCols
   except:
      print 'Unable to determine numRows,numCols'
      return
   def is_last_row(n):
      if n>nax-nc-1:
         return True
      else:
         return False
   for i,ax in enumerate(fig.axes):
      if not is_last_row(i):
         for xt in ax.get_xticklabels(): xt.set_visible(False)
         ax.set_xlabel('')

-Sterling

···

On Sep 13, 2012, at 1:23PM, Daniel Welling wrote:

Greetings, all.

I have an issue: I have several axes stacked in a column with a common time vector on each x-axis. Each plot is a contour, so overplotting is not an option. In a perfect world, I want the following:
1) The subplots are tightly spaced such that with ax.grid() activated, the grid lines appear continuous. This makes comparing simultaneous characteristics between subplots very easy.
2) The subplots are linked via the "sharex" keyword so I can move them all in unison.
3) Only the bottommost subplot has x tick labels; on other plots, the long time-formatted labels stick out of the left and right of the plots.

Items 2 and 3 are contradictory: if I turn off tick labels (e.g. ax.set_xticklabels('')) on one axes, the others turn off as well, including the bottom axes. That is bad.
Does anyone know of a good workaround for this?

Thanks for your help.

-dw
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

From: Daniel Welling [mailto:dantwelling@…287…]
Sent: Thursday, September 13, 2012 16:23

Greetings, all.

I have an issue: I have several axes stacked in a column with a common time vector on each x-axis. Each plot is a contour, so overplotting is not an option. In a perfect world, I want the following:

  1. The subplots are tightly spaced such that with ax.grid() activated, the grid lines appear continuous. This makes comparing simultaneous characteristics between subplots very easy.
  1. The subplots are linked via the “sharex” keyword so I can move them all in unison.
  1. Only the bottommost subplot has x tick labels; on other plots, the long time-formatted labels stick out of the left and right of the plots.

[…]

For #3, there is a convenience method of subplots, label_outer [1], that sets the visibility of the tick labels (as Francesco described), making them visible in the bottom row and invisible elsewhere (as in Sterling’s code). Just iterate over all of your subplots and call the label_outer method on each one.

[1] http://matplotlib.org/api/axes_api.html#matplotlib.axes.SubplotBase.label_outer