Home > Software design >  Seaborn heatmap change date frequency of yticks
Seaborn heatmap change date frequency of yticks

Time:11-30

My problem is similar to the one encountered on this topic: enter image description here

CodePudding user response:

Here are a couple ways to adapt that link for your use case (1 label per 6 months):

  1. Either: Show an empty string except on Jan 1 and Jul 1 (i.e., when %m%d evals to 0101 or 0701)

    labels = [date if date.strftime('%m%d') in ['0101', '0701'] else ''
              for date in df.index.date]
    
  2. Or: Show an empty string except every ~365/2 days (i.e., when row % 183 == 0)

    labels = [date if row % 183 == 0 else ''
              for row, date in enumerate(df.index.date)]
    

Note that you don't have a MultiIndex, so you can just use df.index.date (no need for get_level_values).


Here is the output with a minimized version of your df:

sns.heatmap(df, cmap='PuOr', cbar_kws={'label': 'Ice Velocity (m/yr)'},
            vmin=df.values.min(), vmax=df.values.max(),
            yticklabels=labels)

  • Related