简体   繁体   English

在 Plotly Express 时间线中使用自定义时间格式

[英]Using a custom time format in Plotly Express timeline

I am using plotly express to make a custom timeline graph object and I want to edit the x(time) axis to display a different time format, but it will only allow for 'datetime'.我正在使用 plotly express 制作自定义时间线图 object 并且我想编辑 x(时间)轴以显示不同的时间格式,但它只允许“日期时间”。 Is there any way to use a custom time type in plotly using the timeline object?有没有办法使用时间线 object 在 plotly 中使用自定义时间类型? Is there a better framework for this, such as matplotlib or something else?有没有更好的框架,比如 matplotlib 或者别的什么?

It is possible to customize time format with plotly, see plotly documentation .可以使用 plotly 自定义时间格式,请参阅plotly 文档 Examples shown there use px.line() , but formatting works with px.timeline() the same way.此处显示的示例使用px.line() ,但格式与px.timeline()的使用方式相同。

Example:例子:

presidents = pd.DataFrame([{"name": "Clinton", "start": '1993-01-20', 'end': '2001-01-20'},
                           {"name": "Bush", "start": '2001-01-20', 'end': '2009-01-20'},
                           {"name": "Obama", "start": '2009-01-20', 'end': '2017-01-20'},
                           {"name": "Trump", "start": '2017-01-20', 'end': '2021-01-20'}
                          ])

fig = px.timeline(presidents,
                  x_start="start",
                  x_end="end",
                  y="name"
                 )
fig.update_xaxes(
    dtick="M48",   # display x-ticks every 24 months months
    tickformat="Date:%Y-%m-%d"  # date format
) 
fig.show()

This gives:这给出了:

情节时间表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM