Python Matplotlib绘图即使有值也会变成空白

Python Matplotlib绘图即使有值也会变成空白,python,machine-learning,analytics,Python,Machine Learning,Analytics,我是分析学、python和机器学习的新手,我正在从事时间预测工作。使用下面的代码,我得到了列车和测试数据的值,但是图形是空白的 import pandas as pd import numpy as np import matplotlib.pyplot as plt import statsmodels.tsa.api as ExponentialSmoothing #Importing data df = pd.read_csv('international-airline-passen

我是分析学、python和机器学习的新手,我正在从事时间预测工作。使用下面的代码,我得到了列车和测试数据的值,但是图形是空白的

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt
import statsmodels.tsa.api as ExponentialSmoothing
#Importing data
df = pd.read_csv('international-airline-passengers - Copy.csv')
#Printing head
print(df.head())
#Printing tail
print(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 level
df.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是空的,它将返回空的plot。 基本上,你不是在策划任何事情,只是在展示空白的情节。

plt.子图(值)或plt.散点(值),或其任何功能取决于需求。希望这能有所帮助

这是通过使用Holoviews来完成的,它用于可视化目的。如果您使用的是专业应用程序,您一定要尝试一下。这里创建的版本是日期,极性与计数类似。试试这个

或者,如果要坚持使用matplotlib,请尝试以下方法:

fig, ax = plt.subplots(figsize=(16,9))

ax.plot(msft.index, msft, label='MSFT')
ax.plot(short_rolling_msft.index, short_rolling_msft, label='20 days rolling')
ax.plot(long_rolling_msft.index, long_rolling_msft, label='100 days rolling')

ax.set_xlabel('Date')
ax.set_ylabel('Adjusted closing price ($)')
ax.legend()

如果您想继续使用matplotlib,也可以使用此选项,感谢您确定问题所在。但我不知道如何在plt中绘制该序列和测试值。如果您能帮助解决这个问题,这将非常有帮助。@shivanijosh我将发送到数据集的链接给我,这样我就可以处理进一步的数据集,包括月份和列数。例如:月份:2018年5月22日23:56计数:4此数据是否仅为dd/mm/yyyy时间?是。现在是dd/mm/yyyy时间感谢您的回复。但我试过你的motplotlib代码。它仍在显示包含我的数据的空白图形。请将整个matplotlib脚本发送给我,地址为:nisargbhatt1998@gmail.com
fig, ax = plt.subplots(figsize=(16,9))

ax.plot(msft.index, msft, label='MSFT')
ax.plot(short_rolling_msft.index, short_rolling_msft, label='20 days rolling')
ax.plot(long_rolling_msft.index, long_rolling_msft, label='100 days rolling')

ax.set_xlabel('Date')
ax.set_ylabel('Adjusted closing price ($)')
ax.legend()