Search code examples
pythonpandasplotnine

Changing tick labels with plotnine


I'm using plotnine to generate a scatterplot where the x-axis is pandas.Timestamp objects.

Currently, the x tick labels (e.g., "2017-07-01") are running into each other. I'd like to be able to do an arbitrary transformation of the tick labels. How do I change the x tick labels on the plot?

It looks like I could do something like + scale_x_continuous(labels=???) but I don't know what argument to pass to labels.


Solution

  • I asked this question as an issue on the plotnine project, and got this as the solution:

    from mizani.breaks import date_breaks
    from mizani.formatters import date_format
    
    ...
    + scale_x_datetime(breaks=date_breaks('1 year'), labels=date_format('%Y'))
    

    This would also do it:

    + scale_x_datetime(labels=lambda lst: [x.year if x.month==1 and x.day==1 else "" for x in lst])