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

时间:2018-06-06 10:11:16

标签: 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-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()

即使列车和测试数据具有价值,也无法理解使图表变为空白的原因。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

我想我在这里找到了这个问题。问题是你在这里使用 train.Count.plot ,而" plt" 的值仍然是空的。如果你查看了matplotlib(在下面链接),你会发现你需要先在plt中存储一些值,因为plt是空的,它会返回空图。 基本上你没有绘制任何东西,只是显示空白的情节。

例如:plt.subplots(values)或plt.scatter(values),或者它的任何函数取决于要求。希望这会有所帮助。

https://matplotlib.org/

答案 1 :(得分:0)

import holoviews as hv
import pandas as pd
import numpy as np
data=pd.read_csv("C:/Users/Nisarg.Bhatt/Documents/data.csv", engine="python")

train=data.groupby(["versionCreated"])["Polarity Score"].mean()
table=hv.Table(train)
print(table)
bar=hv.Bars(table).opts(plot=dict(width=1500))
renderer = hv.renderer('bokeh')
app = renderer.app(bar)
print(app)

from bokeh.server.server import Server
server = Server({'/': app}, port=0)
server.start()
server.show("/")

这是通过使用Holoviews完成的,它用于可视化目的。如果您正在使用专业应用程序,您一定要试试这个。此处 versionCreated 是日期,极性与count类似。试试这个

或者,如果你想坚持使用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

,也可以使用它