autofmt_xdate() broken for twinx()

Hi,

It seems that the autofmt_xdate helper method is broken when twinx is
used. Consider the script below:

autofmt_xdate-twinx.diff (1.58 KB)

···

-----------------------------------------
import datetime as dt

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.dates import date2num

strt = dt.datetime(2000, 3, 15, 6)
delta = dt.timedelta(hours=6)
date_list = [(strt + i*delta) for i in range(100)]

x = date2num(date_list)
y = np.sin(x)
z = np.cos(x)

fig, ax1 = plt.subplots()

ax1.plot(date_list, y, 'b-')

ax2 = ax1.twinx()
ax2.plot(date_list, z, 'r-')

# using the auto format method doesn't work
fig.autofmt_xdate()

plt.show()
-----------------------------------------

This is because the 'is_last_row' attribute isn't present on ax2 and
len(fig.axes) != 1 when the autofmt_xdate method is called on fig.

The attached patch fixes it for me and still seems to give the
advertised behaviour for single and vertically stacked subplots.

Cheers,
Scott

I am not very familiar with this part of mpl, but your diff seems to cut out a few things. In particular, the original code checks to see if the figure has a single axes object or more. However, your code seems to cut this check out. Now, it may have been that the check could have been unnecessary, but I am not sure. I am curious as to your insight on this.

In addition, the original code called “self.subplots_adjust(bottom=bottom)” only when all subplots were on the last row. Now, it seems that it is always called no matter what.

Ben Root

···

On Thu, Nov 18, 2010 at 8:58 AM, Scott Sinclair <scott.sinclair.za@gmail.com> wrote:

Hi,

It seems that the autofmt_xdate helper method is broken when twinx is

used. Consider the script below:


import datetime as dt

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.dates import date2num

strt = dt.datetime(2000, 3, 15, 6)

delta = dt.timedelta(hours=6)

date_list = [(strt + i*delta) for i in range(100)]

x = date2num(date_list)

y = np.sin(x)

z = np.cos(x)

fig, ax1 = plt.subplots()

ax1.plot(date_list, y, ‘b-’)

ax2 = ax1.twinx()

ax2.plot(date_list, z, ‘r-’)

using the auto format method doesn’t work

fig.autofmt_xdate()

plt.show()


This is because the ‘is_last_row’ attribute isn’t present on ax2 and

len(fig.axes) != 1 when the autofmt_xdate method is called on fig.

The attached patch fixes it for me and still seems to give the

advertised behaviour for single and vertically stacked subplots.

Cheers,

Scott

It seems that the autofmt_xdate helper method is broken when twinx is
used.

The attached patch fixes it for me and still seems to give the
advertised behaviour for single and vertically stacked subplots.

I am not very familiar with this part of mpl, but your diff seems to cut out
a few things. In particular, the original code checks to see if the figure
has a single axes object or more. However, your code seems to cut this
check out. Now, it may have been that the check could have been
unnecessary, but I am not sure. I am curious as to your insight on this.

I'm not very familiar with the Matplotlib classes either, but I'll
explain my reasoning.

Anytime I create a figure with a single axes object the axes object
has an is_last_row method which always returns True. I don't see much
point in special casing, unless there's significant overhead that can
be avoided by doing so?

In addition, the original code called "self.subplots_adjust(bottom=bottom)"
only when all subplots were on the last row. Now, it seems that it is
always called no matter what.

The hasattr test is only checking that each axes object in the figure
has an is_last_row method. In the current code all_subplots is always
True unless the figure contains an axes object created with twinx or
twiny (in which case the autofmt_xdate method does nothing useful -
hence my patch).

Cheers,
Scott

···

On 22 November 2010 19:01, Benjamin Root <ben.root@...553...> wrote:

On Thu, Nov 18, 2010 at 8:58 AM, Scott Sinclair > <scott.sinclair.za@...149...> wrote: