使用 plotnine 更改刻度标签
Changing tick labels with plotnine
我正在使用 plotnine 生成散点图,其中 x 轴是 pandas.Timestamp 个对象。
目前,x 刻度标签(例如“2017-07-01”)相互 运行。我希望能够对刻度标签进行任意转换。如何更改绘图上的 x 刻度标签?
看起来我可以做类似 + scale_x_continuous(labels=???)
的事情,但我不知道要传递给标签的参数是什么。
我在 plotnine 项目上作为 issue 问了这个问题,得到了这个作为解决方案:
from mizani.breaks import date_breaks
from mizani.formatters import date_format
...
+ scale_x_datetime(breaks=date_breaks('1 year'), labels=date_format('%Y'))
这也行:
+ scale_x_datetime(labels=lambda lst: [x.year if x.month==1 and x.day==1 else "" for x in lst])