简体   繁体   English

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

[英]How to show timestamp x-axis in Python Plotly

I want to plot this data to evaluate data availability.我想绘制此数据以评估数据可用性。 I used the following plotting code in Plotly.我在 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', 
            )

The result is just like what I want below.结果就像我在下面想要的一样。
在此处输入图片说明

But, the x-index show numbers.但是,x-index 显示数字。 I want a timestamp (month+year) on the x-axis, instead.我想要一个在 x 轴上的时间戳(月 + 年),而不是。

Edit Adding the fllowing编辑添加流动

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

Gives

在此处输入图片说明

which seems that the x-axis is not read from the data index.这似乎不是从数据索引中读取 x 轴。

If you want to use bars it seems to me that you need to find a nice workaround.如果你想使用酒吧,在我看来你需要找到一个很好的解决方法。 Have you considered to use Heatmap ?你考虑过使用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()

在此处输入图片说明

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

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