python chaco轴标签时间格式

发布于 2024-08-19 18:50:25 字数 285 浏览 5 评论 0原文

在Enthought的Chaco中,TimeFormatter类用于格式化tick的时间字符串 标签。有没有办法指定时间格式(例如 time.strftime())。

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

我不知道有什么好的方法可以做到这一点(除了更改源代码本身(>TimeFormatter._formats 字典))

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老子叫无熙 2024-08-26 18:50:25

老实说,最简单的方法是对 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)

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文