【问题标题】:Select specific part of x-axis in Plot在绘图中选择 x 轴的特定部分
【发布时间】:2021-02-20 13:58:23
【问题描述】:

我正在寻找一种只能绘制时间序列特定部分的方法。 我有一个通过 FB Prophet 功能的预测模型。 另请参阅下面的图表和下面的代码。有什么想法我只能绘制 2019 年到 2021 年之间的日期吗?

# plotting of predictions with actual values from 30-09-2020 until 30-10-2020; Combine Prediction and actual values
m_data_DJI_recent.plot(prediction_2)
plt.plot(df_DJI, color = "red")
plt.title("Prediction using the Prophet")
plt.xlabel("Date")
plt.ylabel("Price")
plt.show()

【问题讨论】:

  • plt.xlim() 的哪一部分有问题?
  • 我也尝试过 plt.xlim() 但很难找到正确的值。然后该图只显示了一个木板输出。我真的不知道要为 xlim 赋予什么价值。使用的日期格式是年-月-日。 @Mr.T
  • 这能回答你的问题吗? stackoverflow.com/a/21424306/8881141 如果不提​​供 MCVE,这很难说。

标签: python matplotlib plot jupyter-notebook


【解决方案1】:

您可以像这样直接限制数据(仅使用模拟数据,因此是一个简单的索引,而不是 df,蓝色供参考):

# plotting of predictions with actual values from 30-09-2020 until 30-10-2020; Combine Prediction and actual values
#m_data_DJI_recent.plot(prediction_2)
df_DJI = np.random.randint(0,10,size=20)
plt.plot(df_DJI, color = "blue")
plt.plot(df_DJI[3:], color = "red") # limit range of plot
plt.title("Prediction using the Prophet")
plt.xlabel("Date")
plt.ylabel("Price")
plt.show()

对于数据框,日期看起来像这样:

df.loc['2020-01-01':'2020-02-01']

(或查看 Filtering Pandas DataFrames on dates 了解 df 日期限制)

【讨论】:

  • 这仍然在同一张图表上绘制,我相信 OP 意味着如何将图表过滤到那个时间范围
【解决方案2】:

T 先生的评论你快到了,他们的链接评论有详细信息。你可以使用

plt.xlim([datetime.date(2020, 1, 1), datetime.date(2021, 3, 30)])

将日期替换为您案件的适用日期

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 2019-11-16
    • 2012-12-10
    • 2018-05-25
    • 2017-04-20
    • 2013-01-02
    相关资源
    最近更新 更多