python plotly直方图不显示月份

python plotly histogram does not show months

我需要显示数月和数年的数据。 我的数据是:

我的密码是

import plotly.offline as pyo
import plotly.graph_objs as go

pyo.init_notebook_mode()

#tweets['Time'] = pd.to_datetime(tweets['timestamp'])
tweets['Time'] = pd.to_datetime(tweets['timestamp']).dt.normalize()
tweetsT = tweets['Time']

trace = go.Histogram(
    x=tweetsT,
    marker=dict(
        color='blue'
    ),
    opacity=0.75
)

layout = go.Layout(
    title='Tweet Activity Over Years',
    height=450,
    width=1200,
    xaxis=dict(
        title='Month and year'
    ),
    yaxis=dict(
        title='Tweet Quantity'
    ),
    bargap=0.2,
)

data = [trace]

fig = go.Figure(data=data, layout=layout)
fig.show(renderer="colab")

我的图表如下所示:

但我需要这个:

我不明白,怎么了。谢谢

Plotly 的默认行为是根据您的条形图的时间段确定刻度线的间距。由于您的条形图出现超过 10 年的时间,Plotly 不会显示月份(因为会有太多的刻度和太多的刻度文本导致重叠)

如果您希望范围看起来像所需的图表(并显示月份),并且您不介意图表最初呈现时 2013 年 1 月之前和 2017 年 1 月之后的柱状图未包含在图表中,则您可以手动设置 xaxes 的范围,直到范围小到足以显示月份为止。例如:

fig.update_xaxes(range=["2013-01-01","2017-02-01"])