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

Matplotlib plots turn out blank even having values

我是分析、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" 的值仍然是 empty.If 你浏览下面的 matplotlib 文档(link),你会发现你需要先在 plt 中存储一些值,这里因为 plt 是空的,它返回空图。 基本上你没有绘制任何东西,只是显示空白图。

例如:plt.subplots(values) 或 plt.scatter(values),或它的任何函数取决于 requirements.Hope 这有帮助。

https://matplotlib.org/

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 完成的,它用于可视化 purpose.If 您正在将其用于专业应用程序,您一定要试试这个。这里 versionCreated 是日期,Polarity 类似于计数。试试这个

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