【问题标题】:how to plot train and test together using matplotlib如何使用 matplotlib 一起绘制训练和测试
【发布时间】:2020-08-31 20:28:18
【问题描述】:

我正在尝试将 (y_train, y_test) 然后 (y_train_pred, y_test_pred) 绘制在一个图形中,我使用以下代码来执行此操作

#plot 
plt.plot(y_test)
plt.plot(y_pred)
plt.plot(y_train)
plt.plot(train)
plt.legend(['y_train','y_train_pred', 'y_test', 'y_test_pred'])

运行上面给出了下面的图表

但这不是我想要的。我希望它是连续的,如下所示(y_testy_train 之后继续/开始,y_pred 在之后开始 y_train_pred

任何帮助都会很棒。

【问题讨论】:

  • 提供明确的 x 值
  • @PaulH 你是什么意思?

标签: python matplotlib


【解决方案1】:

您可以传递x 参数:

plt.plot(np.arange(len(y_pred)) + len(y_train),y_test)
plt.plot(np.arange(len(y_pred)) + len(y_train), y_pred)
plt.plot(y_train)
plt.plot(train)

# you seemed to mess up the labels
plt.legend(['y_test', 'y_test_pred', 'y_train','y_train_pred'])

【讨论】:

  • 这会在第一行给出值错误ValueError: x and y must have same first dimension, but have shapes (157,) and (52, 1)
  • @Tamarie 我搞砸了traintest。查看更新。
  • 如何更改所有线条的颜色?似乎我只能更改 2 id 我在末尾添加颜色参数
  • 您可以将color= 传递给plt.plot
  • plt.plot(np.arange(len(y_pred)) + len(y_train),y_test, color='steelblue')plt.plot(np.arange(len(y_pred)) + len(y_train), y_pred, color='grey') 仅更改 y_test 和 y_test_pred。但我也想改变 y_train 和 y_train_pred
猜你喜欢
  • 2021-12-29
  • 1970-01-01
  • 2019-11-23
  • 2017-06-13
  • 2020-09-10
  • 2016-05-11
  • 2020-02-22
  • 2019-02-20
  • 1970-01-01
相关资源
最近更新 更多