如何在x轴上只画月和日?(忽略年份)

2024-10-01 09:34:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图绘制时间序列数据,希望x轴只是月和日。Plotly要求格式为yyyy-mm-dd,但我有几年来的数据集的日平均值,所以我只想在x轴上绘制mm-dd。当我发送的日期时间仅仅是mm-dd时,它假设mm是年份。我能不能让它绕过一年,只用mm-dd?在

df_en_ave1.index = df_en_ave1.index.strftime('%m-%d') #convert my index to month and day datetime

trace1=go.Scatter(x=df_en_ave1.index, y=df_en_ave1.evap) #need to bypass year in date here somehow

data = [trace1]

plotly.offline.iplot(data)

Tags: to数据dfdataindex格式时间绘制
2条回答

请尝试以下方法:

dcc.Graph(figure={
'data': [{
    'x': ['2015-01-01', '2015-01-10 15:30:12', '2015-04-01'],
    'y': [2, 1, 5]
}],
'layout': {
    'xaxis': {
        'tickformat': '%d/%m'
    }
}})

其他格式

^{pr2}$

多亏了 https://community.plot.ly/t/how-to-make-the-messy-date-ticks-organized/7477/3

答案:

fig.update_layout(xaxis=dict(tickformat="%d-%m"))

示例图:

enter image description here

完整代码:

^{pr2}$

相关问题 更多 >