简体   繁体   English

PyPlot与空闲

[英]PyPlot with IDLE

I am attempting to plot some data using matplotlib.pyplot in IDLE. 我正在尝试使用IDLE中的matplotlib.pyplot绘制一些数据。 This is my first time trying to use matplotlib outside of a classroom. 这是我第一次尝试在教室外使用matplotlib。

My code is pretty straightforward: load some data from a CSV into a dataframe and try to put it on a scatterplot. 我的代码非常简单:将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')

However, this returns a long error which I am having trouble following: 但是,这返回了一个长错误,我在以下方面遇到了麻烦:

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'

I was able to locate this bug report , which suggests that this can't be accomplished with IDLE. 我能够找到此错误报告 ,这表明IDLE无法完成。 As something of a novice, that sounds incredible to me. 作为一个新手,这对我来说真是不可思议。

Is this correct? 这个对吗? Can I really not make a scatterplot using IDLE? 我真的可以不使用IDLE绘制散点图吗? If I can, what do I need to do? 如果可以,我该怎么办?

This is a guide to debugging your problem: 这是调试问题的指南:

First, simplify your program, such that it doesn't depend on external data, such that the error is reproducible. 首先,简化程序,使其不依赖于外部数据,从而使错误可重现。 See Minimal, Complete, and Verifiable example . 请参阅最小,完整和可验证的示例 This could look like the following: 看起来可能如下所示:

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()

Now running this will produce an error, 现在运行它会产生一个错误,

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. 

This indicates that there is something wrong with the creation of the plot, not the actual showing. 这表明绘图的创建存在问题,而不是实际显示。

To see what could go wrong, (1) look at other examples and/or (2) the documentation . 要查看可能出了什么问题,请(1)查看其他示例和/或(2) 文档 From the documentation you will find that the arguments x and y should be a "label or position", but not a dataframe column itself. 从文档中,您会发现参数xy应该是“标签或位置”,而不是数据框列本身。

So changing 所以改变

plot_me = data.plot(x=data['x'], y=data['y'], kind='scatter')

to

plot_me = data.plot(x='x', y='y', kind='scatter')

where x and y are column labels from the DataFrame, will give you the desired plot. 其中xy是DataFrame中的列标签,将为您提供所需的绘图。

At the end this has nothing to do with IDLE, but with a wrong syntax used in one of the commands. 最后,这与IDLE无关,但是其中一个命令中使用了错误的语法。

About the IDLE part of the question: if you start IDLE from a command line console (Command Prompt or PowerShell on Windows) with python -m idlelib (or py -3.4 -m idlelib ), then sys.__stdout__ will be the console and the .fileno() call will succeed and return 1 and the os.get_terminal_size(1) call will return the size of the console window. 关于问题的IDLE部分:如果您使用python -m idlelib (或py -3.4 -m idlelib )从命令行控制台(Windows上的命令提示符或PowerShell)启动IDLE,则sys.__stdout__将成为控制台,并且.fileno()调用将成功并返回1,而os.get_terminal_size(1)调用将返回控制台窗口的大小。 Pyplot should then print to either the console or the IDLE shell (I do not know which). 然后,Pyplot应该打印到控制台或IDLE shell(我不知道是哪一个)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM