0

Using Python with %pylab (Matplotlib & Numpy). Need a very particular xtick label layout consisting of 3 rows, the first of which has a different orientation to the rest. This is a mock up below.

Example x-labels

I can add multiple lines to the xlabel using '/n' so can add the weekday & date on to the first label of each day on different lines. The problem is the orientation. The time element {9am, 10am, 11am...} needs to be rotated vertically (it does improve legibility), whereas the weekday and date element needs to remain horizontal.

This is some code that constructs the labels

def day_time_xlabels(lst, expand_on_time='9:00 AM', ignore_on_time='5:00 PM'):
    labels = []
    for l in lst:
        if l[1]==expand_on_time:
            labels+=[l[1] + '/n/n' + l[0].strftime('%A') + '/n/n'+ l[0].strftime('%d %b %y')]
        elif l[1]==ignore_on_time:
            labels+=['']
        else:
            labels+=[l[1]]
    return labels

Any ideas on the rotation? Or whether it is possible for me to have multiple xlabels?

I have considered ConciseDateFormatter (https://matplotlib.org/stable/api/dates_api.html#matplotlib.dates.ConciseDateFormatter) but this still does not give me enough control ovel the rotation.

My fall back ideas are to have a subplot without a graph so just the axis labels show, or to construct the labels as a separate html/css piece and overlay that as an image maybe.

I'd appreciate any ideas, please.

Many thanks

  • An idea. Use the horizontal labels on the top spine, the vertical ones on the bottom spine, or viceversa. I know it's not _exactly_ what you've asked for, but it's close and you can move on. – gboffi Jul 24 '23 at 12:37
  • 1
    Thanks gboffi, that is a good idea. I will give it a go – Rowan Hayes Jul 24 '23 at 12:40
  • I am also going to try annotations. Not sure it will let me offset outside the main graph but will post solution if it works – Rowan Hayes Jul 24 '23 at 12:41

0 Answers0