python chaco axis labels time formatting

萝らか妹 提交于 2019-12-12 10:53:37

问题


In Enthought's Chaco, the TimeFormatter class is used to format the time string of the tick labels. is there a way to specify the time format (something like time.strftime()).

the source code now hard-codes the format when displaying month and day of the month to the american style (MMDD). I would like to add some flexibility so that the time/date format hints would somehow be passed to the TimeFormatter

I dont know of any nice way to do this (other than changing the source code itself (TimeFormatter._formats dictionary))


回答1:


Honestly, the easiest way is going to be to monkeypatch the TimeFormatter's _formats dictionary:

from enthought.chaco.scales.formatters import TimeFormatter
TimeFormatter._formats['days'] = ('%d/%m', '%d%a',)

If you don't want to do this, then you need to subclass TimeFormatter. That's easy. What's more cumbersome is making all the existing scale systems that the chaco.scales package creates use your new subclass rather than the built-in TimeFormatter. If you look at scales.time_scale.TimeScale, it accepts a 'formatter' keyword argument in the constructor. So, at the bottom of time_scale.py, when the MDYScales list is built, you'd have to create your own:

EuroMDYScales = [TimeScale(day_of_month=range(1,31,3), formatter=MyFormatter()),
             TimeScale(day_of_month=(1,8,15,22), formatter=MyFormatter()),
             TimeScale(day_of_month=(1,15), formatter=MyFormatter()),
             TimeScale(month_of_year=range(1,13), formatter=MyFormatter()),
             TimeScale(month_of_year=range(1,13,3), formatter=MyFormatter()),
             TimeScale(month_of_year=(1,7), formatter=MyFormatter()),
             TimeScale(month_of_year=(1,), formatter=MyFormatter())]

Then, when you create the ScalesTickGenerator, you need to pass in these scales to the ScaleSystem:

euro_scale_system = CalendarScaleSystem(*(HMSScales + EuroMDYScales))
tick_gen = ScalesTickGenerator(scale=euro_scale_system)

Then you can create the axis, giving it this tick generator:

axis = PlotAxis(tick_generator = tick_gen)

HTH, sorry this is about a month lag. I don't really check StackOverflow very much. If you have other chaco questions, I'd recommend signing up on the chaco-users mailing list...



来源:https://stackoverflow.com/questions/2173632/python-chaco-axis-labels-time-formatting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!