【问题标题】:Cufflinks(Plotly) can't plot date time but numbers on the x-axis for the heatmapCufflinks(Plotly) 无法绘制日期时间,但热图的 x 轴上的数字
【发布时间】:2023-03-03 02:12:01
【问题描述】:

我尝试使用 Cufflinks API 绘制热图,并将我的 pandas 数据框的索引更改为 datetime

但 x 轴显示的是科学数字而不是日期:

【问题讨论】:

    标签: python heatmap plotly


    【解决方案1】:

    一种解决方案可能是从您的 pandas 索引中明确指定 x 轴刻度值。

    例如:

    import pandas as pd
    import plotly.graph_objs as go    
    import cufflinks as cf
    df = pd.DataFrame({'C1': [0, 1, 2, 3], 'C2': [1, 2, 3, 4], 'C3': [2, 3, 4, 5], 'C4': [3, 4, 5, 6]},
                 index=pd.to_datetime(['2016-11', '2016-12', '2017-01', '2017-02']))
    
    # set the tick values to the index labels directly
    layout = go.Layout(xaxis = go.XAxis(
        ticks=df.index.tolist(), 
        showticklabels=True,
        linewidth=1,
        tickvals=df.index.tolist()
    ))
    
    cf.go_offline()
    df.iplot(kind='heatmap', fill=True, colorscale='spectral', filename='cufflinks/test', layout=layout)
    

    这会产生情节

    我来到这里是为了解决与您类似的问题。这篇文章有点晚了,但也许这对未来的读者也有帮助。

    【讨论】:

      猜你喜欢
      • 2015-01-19
      • 2020-12-02
      • 1970-01-01
      • 2016-06-24
      • 1970-01-01
      • 2022-11-17
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多