python chaco轴标签时间格式

ban*_*ana 5 python chaco

在Enthought的Chaco中,TimeFormatter该类用于格式化刻度标签的时间字符串.有没有办法指定时间格式(类似time.strftime()).

源代码现在硬编码显示美国风格(MMDD)的月份和日期时的格式.我想添加一些灵活性,以便时间/日期格式提示将以某种方式传递给TimeFormatter

我不知道有什么好办法(除了更改源代码本身(TimeFormatter._formats字典))

Pet*_*ang 4

老实说,最简单的方法是对 TimeFormatter 的 _formats 字典进行猴子补丁:

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

如果您不想这样做,那么您需要子类化 TimeFormatter。这很容易。更麻烦的是让 chaco.scales 包创建的所有现有缩放系统使用新的子类而不是内置的 TimeFormatter。如果您查看scales.time_scale.TimeScale,它在构造函数中接受“formatter”关键字参数。因此,在 time_scale.py 的底部,当构建 MDYScales 列表时,您必须创建自己的:

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())]
Run Code Online (Sandbox Code Playgroud)

然后,当您创建 ScalesTickGenerator 时,您需要将这些比例传递给 ScaleSystem:

euro_scale_system = CalendarScaleSystem(*(HMSScales + EuroMDYScales))
tick_gen = ScalesTickGenerator(scale=euro_scale_system)
Run Code Online (Sandbox Code Playgroud)

然后你可以创建轴,给它这个刻度生成器:

axis = PlotAxis(tick_generator = tick_gen)
Run Code Online (Sandbox Code Playgroud)

HTH,抱歉,这大约滞后了一个月。我不太常查看 StackOverflow。如果您还有其他 chaco 问题,我建议您在 chaco 用户邮件列表上注册......