Python Chaco坐标轴标签时间格式化

5
在 Enthought 的 Chaco 中,TimeFormatter 类被用来格式化刻度标签的时间字符串。是否有一种方法可以指定时间格式(比如类似于time.strftime())。
源代码现在将月份和日期硬编码为美国风格(MMDD)进行显示。我希望添加一些灵活性,使得时间/日期格式提示能够以某种方式传递给 TimeFormatter
我不知道有什么好方法来实现这一点(除了更改源代码本身TimeFormatter._formats 字典)。
1个回答

4
老实说,最简单的方法就是对TimeFormatter的_formats字典进行猴子补丁操作:
from enthought.chaco.scales.formatters import TimeFormatter
TimeFormatter._formats['days'] = ('%d/%m', '%d%a',)

如果你不想这么做,那么你需要子类化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())]

然后,当您创建ScalesTickGenerator时,需要将这些刻度传递给ScaleSystem:
euro_scale_system = CalendarScaleSystem(*(HMSScales + EuroMDYScales))
tick_gen = ScalesTickGenerator(scale=euro_scale_system)

然后,您可以创建轴,将其赋予此刻度生成器:

axis = PlotAxis(tick_generator = tick_gen)

抱歉,这个问题已经延迟了一个月。我并不经常查看StackOverflow。如果您有其他关于Chaco的问题,我建议您在Chaco用户邮件列表上注册...


请注意,如果您使用TimeFormatter._format解决方案,则必须在拥有其他chaco导入语句之前设置它,否则它将无效。 - Eric Yung

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接