如何在Python Plotly中显示时间戳X轴

问题描述 投票:0回答:1

我想绘制this data以评估数据可用性。我在Plotly中使用了以下绘图代码。

import datetime
import plotly.express as px

fig = px.bar(df, x=df.index, y="variable", color='value', orientation="h",
             hover_data=[df.index],
             height=350,
             color_continuous_scale=['firebrick', '#2ca02c'],
             title='',
             template='plotly_white', 
            )

结果与下面我想要的一样。enter image description here

但是,x索引显示数字。我要在x轴上放一个时间戳(月+年)。

编辑添加绒毛

fig.update_layout(yaxis=dict(title=''), 
                  xaxis=dict(
                      title='Timestamp', 
                      tickformat = '%Y-%b',
                  )
                 )

礼物

enter image description here

似乎未从数据索引中读取x轴。

python pandas plotly
1个回答
0
投票

如果您想使用酒吧,在我看来您需要找到一个不错的解决方法。您是否考虑过使用Heatmap


import pandas as pd
import plotly.graph_objs as go

df = pd.read_csv("availability3.txt",
                 parse_dates=["Timestamp"])\
       .drop("Unnamed: 0", axis=1)

# you want to have variable as columns
df = pd.pivot_table(df,
                    index="Timestamp",
                    columns="variable",
                    values="value")
fig = go.Figure()
fig.add_trace(
    go.Heatmap(
        z=df.values.T,
        x=df.index,
        y=df.columns,
        colorscale='RdYlGn',
        xgap=1,
        ygap=2)
      )

fig.show()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.