python - 如何在Python中进行时间序列分析时组合两个折线图进行数据验证

标签 python pandas validation matplotlib timeserieschart

我已经分离了我的训练数据来进行验证,并试图遵循一个示例来说明如何在一个时间序列图表中绘制训练和验证数据的图表,其中线条将连接并更改验证数据的颜色(参见图片)我正在关注的示例)。

我非常密切地关注这个示例(根据我自己的数据进行调整),并且得到了两个单独的图表。 示例如下编码和输出:

Train.Count.plot(figsize=(15,8), title= 'Daily Ridership', fontsize=14, label='train') 
valid.Count.plot(figsize=(15,8), title= 'Daily Ridership', fontsize=14, label='valid') 
plt.xlabel("Datetime") plt.ylabel("Passenger count") plt.legend(loc='best') plt.show()

example followed

我的代码(使用设置的不同数据来提供pandas数据帧bananas_train和bananas_val中的值之和)如下:

bananas_train.plot(figsize=(15,8), title= 'Bananas Daily', fontsize=14, label='train')
bananas_val.plot(figsize=(15,8), title= 'Bananas Daily', fontsize=14, label='valid')
plt.xlabel("Date")
plt.ylabel("Quantity Picked")
plt.legend(loc='best')
plt.show()

由于收到语法错误,与原始代码相比,底行已被分开。

我尝试使用以下代码重做,但仍然得到两个单独的图表:

#attempt 2
plt.figure(figsize=(28,8))
ax1 = bananas_train.plot(color='blue', grid=True, label='train')
ax2 = bananas_val.plot(color='red', grid=True, label='valid')
plt.xlabel("Date")
plt.ylabel("Quantity Picked")
h1, l1 = ax1.get_legend_handles_labels()
h2, l2 = ax2.get_legend_handles_labels()
plt.legend(h1+h2, l1+l2, loc='best')
plt.show()

我的结果显示了两个独立的时间序列图表,而不是一个。关于如何合并这两个图表有什么想法吗?

编辑:如果我关注被列为该问题重复项的页面... Plotting multiple dataframes using pandas functionality Plot different DataFrames in the same figure 我得到一个包含两条线的图表,但 x 轴从头开始匹配,而不是从最后一行停止的位置继续。 这是我的代码,我尝试遵循这些指示并得到了这个结果:

#attempt 3
ax = bananas_train.plot()
bananas_val.plot(ax=ax)
plt.show()

我需要我的结果继续用新合并的数据绘制的线,而不是重叠线。

最佳答案

下次最好在您的问题中包含一些示例数据。 我确信有更好的方法可以实现您想要的目标,但我将在这里列出一种:

# get the length of your training and checking set assuming your data are 
# in 2 dataframes (test and check respectively) with a column named after 'count'
t_len=len(test['count'].index)
c_len=len(check['count'].index)

# concaternate your dataframes
test_check= pd.concat([test['count'],check['count']],ignore_index=True)

#create 2 dataframes assigning 0s to training and checking set respectively
test_1=test_check.copy()
check_1= test_check.copy()

test_1.iloc[0:t_len]=0
check_1.iloc[c_len:]=0

#make the plot
test_1.plot(figsize=(15,8),color='blue')
check_1.plot(figsize=(15,8),color='red')

关于python - 如何在Python中进行时间序列分析时组合两个折线图进行数据验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56755147/

相关文章:

python - 如果图形刷新,则 FigureCanvasTkAgg 调整大小

python - Python中的管道字符

python - 将 autopy 位图转换为 Pillow 图像

pandas - 将几列转换为 epoch pandas

forms - HTML5 验证

java - 正则表达式部分验证表达式

python - 在 Django 中使用 virtualenv 时出现 ImportError

python - 使用 .loc 和日期检索值

python - 使用字符串在数据框中进行迭代

javascript - 为什么这个输入有效