【问题标题】:python plotly histogram does not show monthspython plotly直方图不显示月份
【发布时间】:2022-01-05 11:51:57
【问题描述】:

我需要展示几个月和几年的数据。 我的数据是:

我的代码是

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

我的图表如下所示:

但我需要这个:

我不明白,有什么问题。谢谢

【问题讨论】:

  • 你能不能也把数据加到列表里去?
  • @OrangoMorango,第一张图片是我的数据样本。

标签: python plotly


【解决方案1】:

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

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

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

【讨论】:

  • 成功了,非常感谢!
  • 没问题——很高兴能帮上忙!
猜你喜欢
  • 1970-01-01
  • 2011-12-22
  • 2021-11-23
  • 1970-01-01
  • 2018-10-01
  • 1970-01-01
  • 2019-06-25
  • 2022-07-30
  • 1970-01-01
相关资源
最近更新 更多