如何在Python Plotly中显示时间戳X轴 [英] How to show timestamp x-axis in Python Plotly
本文介绍了如何在Python Plotly中显示时间戳X轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想绘制此数据,以评估数据的可用性.我在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.
但是,x索引显示数字.我要在x轴上放一个时间戳(月+年).
But, the x-index show numbers. I want a timestamp (month+year) on the x-axis, instead.
修改 添加蓬松性
fig.update_layout(yaxis=dict(title=''),
xaxis=dict(
title='Timestamp',
tickformat = '%Y-%b',
)
)
给予
似乎没有从数据索引中读取x轴.
which seems that the x-axis is not read from the data index.
推荐答案
如果您想使用酒吧,在我看来,您需要找到一个不错的解决方法.您是否考虑过使用Heatmap
?
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()
这篇关于如何在Python Plotly中显示时间戳X轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文