首页 > 解决方案 > 使用 x 轴作为 bin 进行绘图时,如何修复“AttributeError:'Series' 对象没有属性'find'”错误?

问题描述

我有一个包含 2 列的数据框,x其中y包含 10 个其他数据框的列及其各自的值。我正在尝试将xs 与 s 进行对比y,但出现错误:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(columns=['x','y'])

df['x'] = ['19.0-23.0', '24.0-28.0', '29.0-33.0', '34.0-38.0', '39.0-43.0', '44.0-48.0', '49.0-53.0', '54.0-58.0', '59.0-63.0', '64.0-68.0']
df['y'] = ['0.065217', '0.039548', '0.06015', '0.054945', '0.085366', '0.071429', '0.137931', '0.129032', '0.190476', '0']

plt.plot([str(i) for i in df['x']], df['y']) 

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-28-001ebdb8bf81> in <module>()
      7 df['y'] = ['0.065217', '0.039548', '0.06015', '0.054945', '0.085366', '0.071429', '0.137931', '0.129032', '0.190476', '0']
      8 
----> 9 plt.plot([str(i) for i in df['x']], df['y'])

~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\pyplot.py in plot(*args, **kwargs)
   3315                       mplDeprecation)
   3316     try:
-> 3317         ret = ax.plot(*args, **kwargs)
   3318     finally:
   3319         ax._hold = washold

~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs)
   1896                     warnings.warn(msg % (label_namer, func.__name__),
   1897                                   RuntimeWarning, stacklevel=2)
-> 1898             return func(ax, *args, **kwargs)
   1899         pre_doc = inner.__doc__
   1900         if pre_doc is None:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\axes\_axes.py in plot(self, *args, **kwargs)
   1404         kwargs = cbook.normalize_kwargs(kwargs, _alias_map)
   1405 
-> 1406         for line in self._get_lines(*args, **kwargs):
   1407             self.add_line(line)
   1408             lines.append(line)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\axes\_base.py in _grab_next_args(self, *args, **kwargs)
    405                 return
    406             if len(remaining) <= 3:
--> 407                 for seg in self._plot_args(remaining, kwargs):
    408                     yield seg
    409                 return

~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs)
    355         ret = []
    356         if len(tup) > 1 and is_string_like(tup[-1]):
--> 357             linestyle, marker, color = _process_plot_format(tup[-1])
    358             tup = tup[:-1]
    359         elif len(tup) == 3:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\axes\_base.py in _process_plot_format(fmt)
     92     # handle the multi char special cases and strip them from the
     93     # string
---> 94     if fmt.find('--') >= 0:
     95         linestyle = '--'
     96         fmt = fmt.replace('--', '')

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   4374             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   4375                 return self[name]
-> 4376             return object.__getattribute__(self, name)
   4377 
   4378     def __setattr__(self, name, value):

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

出现此错误是因为它要将df[x]值转换为浮点数。但为什么?它们是字符串,应该这样绘制。我一直在为其他数据集绘制这些图,而我现在才在这个特定的数据集上得到这个错误。但是我无法确定这个数据集中到底有什么不同导致了这个错误。

现在,我通过将ys 与默认的 x 轴绘制来修复它,然后手动标记 x 刻度:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(columns=['x','y'])

df['x'] = ['19.0-23.0', '24.0-28.0', '29.0-33.0', '34.0-38.0', '39.0-43.0', '44.0-48.0', '49.0-53.0', '54.0-58.0', '59.0-63.0', '64.0-68.0']
df['y'] = ['0.065217', '0.039548', '0.06015', '0.054945', '0.085366', '0.071429', '0.137931', '0.129032', '0.190476', '0']

fig, ax = plt.subplots()
plt.plot(df['y'])
labels = ['19.0-23.0', '24.0-28.0', '29.0-33.0', '34.0-38.0', '39.0-43.0', '44.0-48.0', '49.0-53.0', '54.0-58.0', '59.0-63.0', '64.0-68.0']
ax.set_xticklabels(labels)
plt.show()

但这是一个乏味的修复,如果不是一个复杂的修复,而且这个问题甚至不应该首先出现,就像它没有出现在我的其他数据集中一样。那么,为什么会发生这种情况以及如何正确修复它?

标签: pythonpandasmatplotlibplotbinning

解决方案


推荐阅读