python - 使用 x 轴作为 bin 进行绘图时,如何修复“AttributeError:'Series' 对象没有属性'find'”错误?
问题描述
我有一个包含 2 列的数据框,x
其中y
包含 10 个其他数据框的列及其各自的值。我正在尝试将x
s 与 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]
值转换为浮点数。但为什么?它们是字符串,应该这样绘制。我一直在为其他数据集绘制这些图,而我现在才在这个特定的数据集上得到这个错误。但是我无法确定这个数据集中到底有什么不同导致了这个错误。
现在,我通过将y
s 与默认的 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()
但这是一个乏味的修复,如果不是一个复杂的修复,而且这个问题甚至不应该首先出现,就像它没有出现在我的其他数据集中一样。那么,为什么会发生这种情况以及如何正确修复它?
解决方案
推荐阅读
- javascript - Nerlify:lambda 响应未定义。再次检查您的功能代码在 583 毫秒内以状态 500 响应
- sql - how to transform a number in binary representation to a Snowflake number
- typescript - 如何使用 AWS CDK 在 typescript 中获取路由表 ID
- google-sheets - 具有多个结果的 Google 表格索引/匹配
- django - 创建一个“post”对象并使用 post id 和 author id 初始化一个 m2m 属性
- flutter - 将侦听器添加到列表
- javascript - EPERM 阻止的文件写入权限 - 删除然后写入新文件。节点.js
- flutter - 如何在浏览列表时使用速度
- php - 如何通过 Goutte 来处理图像?
- node.js - 我们如何为 nodejs Kafka 生产者中的模式配置 value.subject.name.strategy?