Aligning xticks and labels with WeekdayLocator

Matplotlib 1.2.0, Windows Vista, Python 3.3.0. I want the first major xtick label aligned with the first date that's plotted. This never happens with the value of day below set in the range zero to six. The first major tick label actually occurs as follows.

Day Label date
0 25/03/2013
1 02/04/2013
2 10/04/2013
3 21/03/2013
4 29/03/2013
5 06/04/2013
6 17/03/2013

What am I doing wrong?

If day is set to seven then no xticks are displayed but labels for 14/03/2013 and 13/03/2014 are displayed. I expected a ValueError or similar using this number. Could you explain this behaviour please?

import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter, MultipleLocator
from matplotlib.dates import DateFormatter, WeekdayLocator
import datetime

dates = [datetime.date(2013, 3, 14), datetime.date(2014, 3, 13)]
values = [0, 1]
plt.ylabel('Balance')
plt.grid()
ax = plt.subplot(111)
plt.plot_date(dates, values, fmt = 'rx-')
plt.axis(xmin=dates[0], xmax=dates[-1])
day = ?
ax.xaxis.set_major_locator(WeekdayLocator(byweekday=day, interval=4))
ax.xaxis.set_minor_locator(WeekdayLocator(byweekday=day))
ax.xaxis.set_major_formatter(DateFormatter('%d/%m/%y'))
ax.yaxis.set_major_formatter(FormatStrFormatter('�%0.2f'))
ax.yaxis.set_minor_locator(MultipleLocator(5))
plt.setp(plt.gca().get_xticklabels(), rotation = 45, fontsize = 10)
plt.setp(plt.gca().get_yticklabels(), fontsize = 10)
plt.show()

···

--
Cheers.

Mark Lawrence

Mark,

I've found that rotation_mode='anchor' works best when rotation != 0

So that makes it:
plt.setp(plt.gca().get_xticklabels(), rotation = 45, fontsize = 10,
rotation_mode='anchor' )

HTH,
-paul

···

On Mon, Mar 18, 2013 at 8:34 PM, Mark Lawrence <breamoreboy@...225...>wrote:

Matplotlib 1.2.0, Windows Vista, Python 3.3.0. I want the first major
xtick label aligned with the first date that's plotted. This never
happens with the value of day below set in the range zero to six. The
first major tick label actually occurs as follows.

Day Label date
0 25/03/2013
1 02/04/2013
2 10/04/2013
3 21/03/2013
4 29/03/2013
5 06/04/2013
6 17/03/2013

What am I doing wrong?

If day is set to seven then no xticks are displayed but labels for
14/03/2013 and 13/03/2014 are displayed. I expected a ValueError or
similar using this number. Could you explain this behaviour please?

import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter, MultipleLocator
from matplotlib.dates import DateFormatter, WeekdayLocator
import datetime

dates = [datetime.date(2013, 3, 14), datetime.date(2014, 3, 13)]
values = [0, 1]
plt.ylabel('Balance')
plt.grid()
ax = plt.subplot(111)
plt.plot_date(dates, values, fmt = 'rx-')
plt.axis(xmin=dates[0], xmax=dates[-1])
day = ?
ax.xaxis.set_major_locator(WeekdayLocator(byweekday=day, interval=4))
ax.xaxis.set_minor_locator(WeekdayLocator(byweekday=day))
ax.xaxis.set_major_formatter(DateFormatter('%d/%m/%y'))
ax.yaxis.set_major_formatter(FormatStrFormatter('£%0.2f'))
ax.yaxis.set_minor_locator(MultipleLocator(5))
plt.setp(plt.gca().get_xticklabels(), rotation = 45, fontsize = 10)
plt.setp(plt.gca().get_yticklabels(), fontsize = 10)
plt.show()

I misread your question. Try setting your x-axis limits after defining the
locators and formatters.
-p

···

On Tue, Mar 19, 2013 at 11:30 AM, Paul Hobson <pmhobson@...287...> wrote:

On Mon, Mar 18, 2013 at 8:34 PM, Mark Lawrence <breamoreboy@...225...>wrote:

Matplotlib 1.2.0, Windows Vista, Python 3.3.0. I want the first major
xtick label aligned with the first date that's plotted. This never
happens with the value of day below set in the range zero to six. The
first major tick label actually occurs as follows.

Day Label date
0 25/03/2013
1 02/04/2013
2 10/04/2013
3 21/03/2013
4 29/03/2013
5 06/04/2013
6 17/03/2013

What am I doing wrong?

If day is set to seven then no xticks are displayed but labels for
14/03/2013 and 13/03/2014 are displayed. I expected a ValueError or
similar using this number. Could you explain this behaviour please?

import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter, MultipleLocator
from matplotlib.dates import DateFormatter, WeekdayLocator
import datetime

dates = [datetime.date(2013, 3, 14), datetime.date(2014, 3, 13)]
values = [0, 1]
plt.ylabel('Balance')
plt.grid()
ax = plt.subplot(111)
plt.plot_date(dates, values, fmt = 'rx-')
plt.axis(xmin=dates[0], xmax=dates[-1])
day = ?
ax.xaxis.set_major_locator(WeekdayLocator(byweekday=day, interval=4))
ax.xaxis.set_minor_locator(WeekdayLocator(byweekday=day))
ax.xaxis.set_major_formatter(DateFormatter('%d/%m/%y'))
ax.yaxis.set_major_formatter(FormatStrFormatter('£%0.2f'))
ax.yaxis.set_minor_locator(MultipleLocator(5))
plt.setp(plt.gca().get_xticklabels(), rotation = 45, fontsize = 10)
plt.setp(plt.gca().get_yticklabels(), fontsize = 10)
plt.show()

Mark,

I've found that rotation_mode='anchor' works best when rotation != 0

So that makes it:
plt.setp(plt.gca().get_xticklabels(), rotation = 45, fontsize = 10,
rotation_mode='anchor' )

HTH,
-paul

Please accept my apologies for the delay in replying, plus I should also have mentioned originally that I've only encountered this problem with WeekdayLocator.

Setting the x-axis limits after defining the locators and formatters makes no difference.

I've resolved my issue by reverting back to using MonthLocator, which I originally disliked as the minor tick locations made the display look poor around that darned month of February. My solution has been to ignore all rrule type computing and use the following code.

xticks = ax.get_xticks()
minorTicks =
for i,xt in enumerate(xticks, start=1):
     try:
         diff = (xticks[i] - xt) / 4
         for i in range(1, 4):
             minorTicks.append(xt + i * diff)
     except IndexError:
         pass
ax.set_xticks(minorTicks, minor=True)

It works a treat, but if there's a simpler solution please let me know :slight_smile:

···

On 19/03/2013 18:35, Paul Hobson wrote:

On Tue, Mar 19, 2013 at 11:30 AM, Paul Hobson <pmhobson@...287... > <mailto:pmhobson@…287…>> wrote:

    On Mon, Mar 18, 2013 at 8:34 PM, Mark Lawrence > <breamoreboy@...225... > <mailto:breamoreboy@…225…>> wrote:

        Matplotlib 1.2.0, Windows Vista, Python 3.3.0. I want the first
        major
        xtick label aligned with the first date that's plotted. This never
        happens with the value of day below set in the range zero to
        six. The
        first major tick label actually occurs as follows.

        Day Label date
        0 25/03/2013
        1 02/04/2013
        2 10/04/2013
        3 21/03/2013
        4 29/03/2013
        5 06/04/2013
        6 17/03/2013

        What am I doing wrong?

        If day is set to seven then no xticks are displayed but labels for
        14/03/2013 and 13/03/2014 are displayed. I expected a ValueError or
        similar using this number. Could you explain this behaviour please?

        import matplotlib.pyplot as plt
        from matplotlib.ticker import FormatStrFormatter, MultipleLocator
        from matplotlib.dates import DateFormatter, WeekdayLocator
        import datetime

        dates = [datetime.date(2013, 3, 14), datetime.date(2014, 3, 13)]
        values = [0, 1]
        plt.ylabel('Balance')
        plt.grid()
        ax = plt.subplot(111)
        plt.plot_date(dates, values, fmt = 'rx-')
        plt.axis(xmin=dates[0], xmax=dates[-1])
        day = ?
        ax.xaxis.set_major_locator(WeekdayLocator(byweekday=day,
        interval=4))
        ax.xaxis.set_minor_locator(WeekdayLocator(byweekday=day))
        ax.xaxis.set_major_formatter(DateFormatter('%d/%m/%y'))
        ax.yaxis.set_major_formatter(FormatStrFormatter('�%0.2f'))
        ax.yaxis.set_minor_locator(MultipleLocator(5))
        plt.setp(plt.gca().get_xticklabels(), rotation = 45, fontsize = 10)
        plt.setp(plt.gca().get_yticklabels(), fontsize = 10)
        plt.show()

    Mark,

    I've found that rotation_mode='anchor' works best when rotation != 0

    So that makes it:
    plt.setp(plt.gca().get_xticklabels(), rotation = 45, fontsize = 10,
      rotation_mode='anchor' )

    HTH,
    -paul

I misread your question. Try setting your x-axis limits after defining
the locators and formatters.
-p

--
Cheers.

Mark Lawrence