简体   繁体   中英

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.

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. I want a timestamp (month+year) on the x-axis, instead.

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.

If you want to use bars it seems to me that you need to find a nice workaround. Have you considered to use 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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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