Python Pandas 数据框错误:matplotlib.axes._subplots.AxesSubplot

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41898485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:48:47  来源:igfitidea点击:

Pandas dataframe error: matplotlib.axes._subplots.AxesSubplot

pythonpandasmatplotlibbokeh

提问by vbd

import pandas as pd
import matplotlib.pyplot as plt

file = 'd:\a\pandas\test.xlsx'
data = pd.ExcelFile(file)
df1 = data.parse('Link')
df2 = df1[['dataFor', 'total']]
df2

returns:

返回:

enter image description here

在此处输入图片说明

 print (type(df2))

tells me

告诉我

class 'pandas.core.frame.DataFrame'

trying

df2.plot(kind='line')

returns

返回

matplotlib.axes._subplots.AxesSubplot at 0xe4241d0

Could it be the environment?

会不会是环境问题?

Jupyter notebook > Help > About

The version of the notebook server is 4.2.3 and is running on:
Python 3.5.2 |Anaconda 4.2.0 (32-bit)| (default, Jul  5 2016, 11:45:57) [MSC    v.1900 32 bit (Intel)]

Where is the fault? Is matplotlib still the standard or should beginners go for Bokeh or for both?

错在哪里?matplotlib 仍然是标准还是初学者应该选择 Bokeh 或两者兼而有之?

回答by ImportanceOfBeingErnest

In case you want to see the plot inline, use

如果您想查看内联绘图,请使用

%matplotlib inline

in the header (before the imports).

在标题中(在导入之前)。

If you want to show the graphic in a window, add the line

如果要在窗口中显示图形,请添加行

plt.show()

at the end (make sure you have imported import matplotlib.pyplot as pltin the header).

最后(确保您已import matplotlib.pyplot as plt在标题中导入)。

回答by epattaro

## importing libraries
## notice to import %matplotlib inline to plot within notebook
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
import datetime


## making a DF like yours
df2 = pd.DataFrame([], columns=['dataFor','total'])
df2['dataFor'] = [datetime.datetime(2013, 9, 11),datetime.datetime(2013, 9, 12),datetime.datetime(2013, 9, 13),datetime.datetime(2013, 9, 14),datetime.datetime(2013, 9, 15),datetime.datetime(2013, 9, 16),datetime.datetime(2013, 9, 17)]
df2['total'] = [11,15,17,18,19,20,21]

## notice date are datetimes objects and not strings
df2.plot(kind='line')

output:

输出:

enter image description here

在此处输入图片说明

if one wants to improve graph layout:

如果想改进图形布局:

plt.figure(figsize=(20,10))
plt.plot(df2.dataFor, df2.total, linewidth=5)
plt.plot(df2.dataFor, df2.total, '*', markersize=20, color='red')
plt.xticks(fontsize=20, fontweight='bold',rotation=90)
plt.yticks(fontsize=20, fontweight='bold')
plt.xlabel('Dates',fontsize=20, fontweight='bold')
plt.ylabel('Total Count',fontsize=20, fontweight='bold')
plt.title('Counts per time',fontsize=20, fontweight='bold')
plt.tight_layout()

enter image description here

在此处输入图片说明

回答by Qi Wang

I got the same problem on performing the plot but I got it solved by running the import clause followed by %matplotlib. I think I'm using the latest version. I've tried the " %matplotlib inline " but for some reasons, it doesn't work on my end.

我在执行绘图时遇到了同样的问题,但我通过运行 import 子句后跟 %matplotlib 来解决它。我想我正在使用最新版本。我试过“ %matplotlib inline ”,但由于某些原因,它对我不起作用。