为什么有些数据点在时间序列的错误日期槽中?

2024-04-26 07:38:17 发布

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

我正在使用python版本的plotly来构建tweet的时间序列图。但我只想包括最近五天的推特。因此,我有这个代码,我可以告诉(这是一个简化的版本,不可复制,因为我非常确定我的数据帧格式是正确的,非常确定的错误在下面的代码中的某个地方):

# Set range to use to limit to recent dates 
min_day = tweet_dataframe['day'].max() - timedelta(days = 5)

reduced_df = tweet_dataframe.loc[tweet_dataframe['date'] > min_day]

# Plot time series
time_series = go.Scatter(
    x = reduced_df['date'],
    y = reduced_df['vader_polarity'],
    name = topic,
    mode = 'markers'
    hoverinfo = 'x+text',
    text = reduced_df['custom_text'],
    )

fig.append_trace(time_series) 
offline_plot.plot(fig, filename = path, auto_open = True)

这将生成一个显示日期和一些自定义文本的交互式时间序列。在手动检查悬停信息之后,看起来数据点与我期望的数据帧匹配。你知道吗

但是,使用下面的方法,在不定义缩减的_df的情况下,一些数据点显示错误的悬停信息或绘制在错误的日期栏中。当我不包括> min_day位时,绘图就可以了。你知道吗

time_seres = go.Scatter(
    x = tweet_dataframe['date'].loc[tweet_dataframe['date'] > min_day],
    y = tweet_dataframe['vader_polarity'].loc[tweet_dataframe['day'] > min_day, 
    name = topic,
    mode = 'markers',
    hoverinfo = 'x+text',
    text = tweet_dataframe['custom_text']
    )

是否有人在plotly中绘制时间序列时遇到过类似问题,或者我的plotly/pandas逻辑中是否存在明显错误?你知道吗


Tags: to数据textdataframedfdatetime错误
1条回答
网友
1楼 · 发布于 2024-04-26 07:38:17

我找到了我的窃听器。我只需要在plotly的text参数中指定日期范围

time_seres = go.Scatter(
    x = tweet_dataframe['date'].loc[tweet_dataframe['date'] > min_day],
    y = tweet_dataframe['vader_polarity'].loc[tweet_dataframe['day'] > min_day], 
    name = topic,
    mode = 'markers',
    hoverinfo = 'x+text',
    text = tweet_dataframe['custom_text'].loc[tweet_dataframe['day'] > min_day]
    )

相关问题 更多 >