Matplotlib绘图结果为空白尽管数据有值

我是数据分析、Python和机器学习的新手,目前正在研究时间序列预测。使用以下代码,我能够获取训练和测试数据的值,但绘制出来的图表却是空白的。

import pandas as pd import numpy as np import matplotlib.pyplot as pltimport statsmodels.tsa.api as ExponentialSmoothing#Importing datadf = pd.read_csv('international-airline-passengers - Copy.csv')#Printing headprint(df.head())#Printing tailprint(df.tail())df = pd.read_csv('international-airline-passengers - Copy.csv', nrows = 11856)#Creating train and test set #Index 10392 marks the end of October 2013 train=df[0:20]test=df[20:]#Aggregating the dataset at daily leveldf.Timestamp = pd.to_datetime(df.Month,format='%m/%d/%Y %H:%M') df.index = df.Timestamp df = df.resample('D').mean()train.Timestamp = pd.to_datetime(train.Month,format='%m/%d/%Y %H:%M')print('1')print(train.Timestamp)train.index = train.Timestamp train = train.resample('D').mean() test.Timestamp = pd.to_datetime(test.Month,format='%m/%d/%Y %H:%M') test.index = test.Timestamp test = test.resample('D').mean()train.Count.plot(figsize=(15,8), title= 'Result', fontsize=14)test.Count.plot(figsize=(15,8), title= 'Result', fontsize=14)plt.show()

即使训练和测试数据有值,我还是不明白为什么图表会显示为空白。提前感谢您的帮助。


回答:

我想我找到了问题所在。你在这里使用了train.Count.plot,而“plt”的值仍然是空的。如果你查阅matplotlib的文档(链接如下),你会发现你需要先在plt中存储一些值,因为这里plt是空的,所以它返回了一个空图。你实际上并没有绘制任何东西,只是显示了一个空白图表。

例如:plt.subplots(values) 或 plt.scatter(values),或者根据需求使用它的任何其他函数。希望这对你有帮助。

https://matplotlib.org/

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注