python plotly histogram does not show months

Viewed 92

I need to show my data for months and years. My data is :

enter image description here

My code is

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")

My graph looks like this : enter image description here

But I need this: enter image description here

I don't understand, what is wrong. Thanks

1 Answers

Plotly's default behavior is to determine how evenly the tickmarks should be spaced depending the period of time for your bars. Since your bars occur over of 10 years of time, Plotly is not going to show the months (because there would be too many ticks and too much tick text causing overlaps)

If you want the range to look like the desired chart (and show months), and you don't mind if bars from before Jan 2013 and after Jan 2017 are not included in the chart when it initially renders, then you can manually set the range of the xaxes until the range is small enough so that months show up. For example:

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