python - 绘图中 x 轴上的时间

标签 python plotly

我的 x 轴值采用以下格式:['May 23 2018 06:31:52 GMT', 'May 23 2018 06:32:02 GMT', 'May 23 2018 06:32: 12 GMT', '2018 年 5 月 23 日 06:32:22 GMT', '2018 年 5 月 23 日 06:32:32 GMT'] 以及 y 轴的相应值,它们是一些数字。 但是当我使用plotly绘制这些时,x轴仅显示每个点的部分日期(2018年5月23日)。未显示每个点的时间。

我尝试在布局中设置刻度格式,但它似乎不起作用。

            layout = go.Layout(
                title=field+ "_its diff_value chart",
                xaxis = dict(
                    tickformat = '%b %d %Y %H:%M:%S'
                )
            )

感谢任何帮助。

这是所制作图表的屏幕截图。

enter image description here

最佳答案

  • 尝试将 x 值转换为 datetime 对象
  • 然后告诉plotly使用固定的刻度距离

enter image description here

import random
import datetime
import plotly

plotly.offline.init_notebook_mode()

x = [datetime.datetime.now()]
for d in range(100):
    x.append(x[0] + datetime.timedelta(d))
y = [random.random() for _ in x]

scatter = plotly.graph_objs.Scatter(x=x, y=y)
layout = plotly.graph_objs.Layout(xaxis={'type': 'date', 
                                         'tick0': x[0], 
                                         'tickmode': 'linear', 
                                         'dtick': 86400000.0 * 14}) # 14 days
fig = plotly.graph_objs.Figure(data=[scatter], layout=layout)
plotly.offline.iplot(fig)

关于python - 绘图中 x 轴上的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51207906/

相关文章:

python - Plotly 未显示所有数据

javascript - 可视化分布随时间的变化

Python:遍历嵌套列表列表中的每个项目并替换特定项目

python - Django 测试 : separate unit and integration tests on Travis CI

python - 编程错误 : column "X" is of type double precision but expression is of type numeric[]

python - Pandas 重采样 : TypeError: Only valid with DatetimeIndex, TimedeltaIndex 或 PeriodIndex,但得到了 'RangeIndex' 的实例

python - 使用plotly在mapbox上绘制多边形

python - 根据标签值垂直重新排列 Sankey 图

r - 在 slidify 中调整 plotly 图表的大小

python - 如何确定语料库中的哪些文本包含Python中的NLTK套件生成的错误?