我试图在IDLE中使用matplotlib.pyplot绘制一些数据。这是我第一次尝试在教室外使用matplotlib。
我的代码非常简单:将CSV中的一些数据加载到数据框中并尝试将其放在散点图上。
import matplotlib.pyplot as plt
import pandas as pd
filename = 'data.csv'
data = pd.read_csv(filename, delimiter=',')
plot_me = data.plot(x=data['Density'], y=data['Cost per Pupil'], kind='scatter')
然而,这会返回一个很长的错误,我遇到了以下问题:
Traceback (most recent call last):
File "C:/Users/dmccarville/Desktop/DJM Figures/plot.py", line 10, in <module>
plot_me = data.plot(x=data['Density'], y=data['Cost per Pupil'], kind='scatter')
File "C:\Python34\lib\site-packages\pandas\plotting\_core.py", line 2617, in __call__
sort_columns=sort_columns, **kwds)
File "C:\Python34\lib\site-packages\pandas\plotting\_core.py", line 1859, in plot_frame
**kwds)
File "C:\Python34\lib\site-packages\pandas\plotting\_core.py", line 1684, in _plot
plot_obj.generate()
File "C:\Python34\lib\site-packages\pandas\plotting\_core.py", line 240, in generate
self._make_plot()
File "C:\Python34\lib\site-packages\pandas\plotting\_core.py", line 833, in _make_plot
scatter = ax.scatter(data[x].values, data[y].values, c=c_values,
File "C:\Python34\lib\site-packages\pandas\core\frame.py", line 1958, in __getitem__
return self._getitem_array(key)
File "C:\Python34\lib\site-packages\pandas\core\frame.py", line 2002, in _getitem_array
indexer = self.loc._convert_to_indexer(key, axis=1)
File "C:\Python34\lib\site-packages\pandas\core\indexing.py", line 1168, in _convert_to_indexer
return labels.get_loc(obj)
File "C:\Python34\lib\site-packages\pandas\core\indexes\base.py", line 2442, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5280)
File "pandas\_libs\index.pyx", line 134, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:4819)
File "C:\Python34\lib\site-packages\pandas\core\base.py", line 51, in __str__
return self.__unicode__()
File "C:\Python34\lib\site-packages\pandas\core\series.py", line 982, in __unicode__
width, height = get_terminal_size()
File "C:\Python34\lib\site-packages\pandas\io\formats\terminal.py", line 33, in get_terminal_size
return shutil.get_terminal_size()
File "C:\Python34\lib\shutil.py", line 1071, in get_terminal_size
size = os.get_terminal_size(sys.__stdout__.fileno())
AttributeError: 'NoneType' object has no attribute 'fileno'
我找到了this bug report,这表明IDLE无法做到这一点。作为一个新手,这对我来说听起来很不可思议。
这是对的吗?我真的不能使用IDLE创建散点图吗?如果可以的话,我需要做什么?
答案 0 :(得分:3)
这是调试问题的指南:
首先,简化您的程序,使其不依赖于外部数据,以便错误可重现。见Minimal, Complete, and Verifiable example。这可能如下所示:
import matplotlib.pyplot as plt
import pandas as pd
data = pd.DataFrame({"x" : [1,2,3],"y":[3,2,4]})
plot_me = data.plot(x=data['x'], y=data['y'], kind='scatter')
plt.show()
现在运行它会产生错误,
Traceback (most recent call last):
File "D:\Data\Computer\Entwicklung\python\SO_plot_dataframe.py", line 13, in <module>
plot_me = data.plot(x=data['x'], y=data['y'], kind='scatter')
# and so on.
这表示绘图的创建有问题,而不是实际显示。
要查看可能出现的问题,(1)查看其他示例和/或(2)the documentation。从文档中,您会发现参数x
和y
应该是&#34;标签或位置&#34;,而不是数据框列本身。
如此改变
plot_me = data.plot(x=data['x'], y=data['y'], kind='scatter')
到
plot_me = data.plot(x='x', y='y', kind='scatter')
其中x
和y
是DataFrame中的列标签,会为您提供所需的图表。
最后,这与IDLE无关,但在其中一个命令中使用了错误的语法。
答案 1 :(得分:0)
关于问题的IDLE部分:如果您使用python -m idlelib
(或py -3.4 -m idlelib
)从命令行控制台(Windows上的命令提示符或PowerShell)启动IDLE,则sys.__stdout__
将作为控制台,.fileno()
调用将成功并返回1,os.get_terminal_size(1)
调用将返回控制台窗口的大小。然后Pyplot应该打印到控制台或IDLE shell(我不知道哪个)。