【问题标题】:python plot error when reading .csv with pandas: 'Series' object has no attribute 'find'使用熊猫读取 .csv 时出现 python 绘图错误:“系列”对象没有属性“查找”
【发布时间】:2015-01-31 05:25:47
【问题描述】:

我正在尝试读取几个 .csv 文件并使用此代码做一些线图(x,y):

import numpy as np
import pandas
from matplotlib import pyplot as plt
%matplotlib inline

colnames = ['X','Y']

xfmr_x_y_file = pandas.read_csv('AMI_X_Y.csv', names=colnames)
gnode_x_y_file = pandas.read_csv('AMI_GNODE_X_Y.csv', names=colnames)
node_x_y_file = pandas.read_csv('AMI_NODE_X_Y.csv', names=colnames)

EX_XFMR_X_meas = (xfmr_x_y_file.X)
EX_XFMR_Y_meas = (xfmr_x_y_file.Y)
DB_GNODE_X_meas = (gnode_x_y_file.X)
DB_GNODE_Y_meas = (gnode_x_y_file.Y)
DB_NODE_X_meas = (node_x_y_file.X)
DB_NODE_Y_meas = (node_x_y_file.Y)

plt.plot(EX_XFMR_X_meas[1:],EX_XFMR_Y_meas[1:],label='XFMR')
plt.title('TUR117')
plt.xlabel('X')
plt.ylabel('Y')

plt.gcf().set_size_inches(18, 6)
#plt.savefig('TUR117.png')#,dpi=300
plt.show()

但它产生了一个奇怪的错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-24-0428c97a1c49> in <module>()
     17 DB_NODE_Y_meas = (node_x_y_file.Y)
     18 
---> 19 plt.plot(EX_XFMR_X_meas[1:],EX_XFMR_Y_meas[1:],label='XFMR')
     20 plt.title('TUR117')
     21 plt.xlabel('X')

C:\Program Files (x86)\ActivePython 2.7.8\lib\site-packages\matplotlib\pyplot.pyc in plot(*args, **kwargs)
   2985         ax.hold(hold)
   2986     try:
-> 2987         ret = ax.plot(*args, **kwargs)
   2988         draw_if_interactive()
   2989     finally:

C:\Program Files (x86)\ActivePython 2.7.8\lib\site-packages\matplotlib\axes.pyc in plot(self, *args, **kwargs)
   4135         lines = []
   4136 
-> 4137         for line in self._get_lines(*args, **kwargs):
   4138             self.add_line(line)
   4139             lines.append(line)

C:\Program Files (x86)\ActivePython 2.7.8\lib\site-packages\matplotlib\axes.pyc in _grab_next_args(self, *args, **kwargs)
    315                 return
    316             if len(remaining) <= 3:
--> 317                 for seg in self._plot_args(remaining, kwargs):
    318                     yield seg
    319                 return

C:\Program Files (x86)\ActivePython 2.7.8\lib\site-packages\matplotlib\axes.pyc in _plot_args(self, tup, kwargs)
    274         ret = []
    275         if len(tup) > 1 and is_string_like(tup[-1]):
--> 276             linestyle, marker, color = _process_plot_format(tup[-1])
    277             tup = tup[:-1]
    278         elif len(tup) == 3:

C:\Program Files (x86)\ActivePython 2.7.8\lib\site-packages\matplotlib\axes.pyc in _process_plot_format(fmt)
     97     # handle the multi char special cases and strip them from the
     98     # string
---> 99     if fmt.find('--') >= 0:
    100         linestyle = '--'
    101         fmt = fmt.replace('--', '')

C:\Program Files (x86)\ActivePython 2.7.8\lib\site-packages\pandas\core\generic.pyc in __getattr__(self, name)
   1934                 return self[name]
   1935             raise AttributeError("'%s' object has no attribute '%s'" %
-> 1936                                  (type(self).__name__, name))
   1937 
   1938     def __setattr__(self, name, value):

AttributeError: 'Series' object has no attribute 'find'

如果我只是简单地做plt.plot(EX_XFMR_X[1:]),它的绘图效果很好,而且由于某种原因它似乎无法模拟plt.plot(x,y) 格式。以前有人遇到过这个问题吗?是不是我做的不对?

【问题讨论】:

  • 您的输入可能有问题。手动生成系列并进行测试。
  • @bejota 你能详细说明你的评论吗?
  • 您正在使用我们无法访问的输入。从一个简单的例子开始,看看你是否可以重现这个问题。例如:EX_XFMR_X_meas = range(10).
  • 贝约塔。我有同样的问题。您应该尝试将您的“pandas.Series”转换为列表。

标签: python csv pandas plot


【解决方案1】:

我遇到了类似的问题,在这种情况下确实不尊重plt.plot(x,y)。现在您的输入EX_XFMR_X_meas[1:]EX_XFMR_Y_meas[1:] 仍然是pandas.Series,因此plt.plot(x,y) 将Series 的索引设为x,将Series 的值设为y。如果第一个变量的值描述了x,第二个变量的值描述了y,那么:

plt.plot(EX_XFMR_X_meas[1:].values,EX_XFMR_Y_meas[1:].values,label='XFMR')

将它们传递为numpy.ndarray

我猜这个奇怪的错误源于plt.plot() 不知道如何处理第二个pandas.Series 作为输入。

【讨论】:

    猜你喜欢
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 2019-02-28
    • 2015-04-07
    • 2014-05-05
    相关资源
    最近更新 更多