python chaco轴标签时间格式 [英] python chaco axis labels time formatting

查看:118
本文介绍了python chaco轴标签时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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()).

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

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

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

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

推荐答案

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

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',)

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

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())]

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

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,抱歉,这大约是一个月的延迟.我并不是真的非常检查StackOverflow.如果您还有其他chaco问题,建议您在chaco用户邮件列表中注册...

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...

这篇关于python chaco轴标签时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆