繁体   English   中英

Pandas plot 错误:缺少 StrCategoryConverter 的类别信息; 这可能是由于无意中混合了分类数据和数字数据造成的

[英]Pandas plot error : Missing category information for StrCategoryConverter; this might be caused by unintendedly mixing categorical and numeric data

I was trying to plot a line plot using pandas plot method, the same data with exactly same method runs fine if I use matplotlib methods, however if I use df.plot then annotate gives me error ValueError: Missing category information for StrCategoryConverter; this might be caused by unintendedly mixing categorical and numeric data ValueError: Missing category information for StrCategoryConverter; this might be caused by unintendedly mixing categorical and numeric data

假设我有一个 dataframe,

data = {'Unit': {0: 'Admin ', 1: 'C-Level', 2: 'Engineering', 3: 'IT', 4: 'Manufacturing', 5: 'Sales'}, 'Mean': {0: 4.642857142857143, 1: 4.83, 2: 4.048, 3: 4.237317073170732, 4: 4.184319526627219, 5: 3.9904545454545453}}
result=pd.DataFrame(data)

使用 matplotlib 时

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(18,9))                                  
ax.plot(results['Unit'],results['Mean'])

for i, val in enumerate(zip(results['Unit'],results['Mean'])):
    label = str(results.loc[i, 'Mean'])
    ax.annotate(label, val, ha='center')
                             
plt.show()

上面的代码工作得很好。

使用 pandas plot 函数(这给了我错误)

results.plot(x = 'Unit', y = 'Mean', marker = 'o', figsize=(8,5))
ax = plt.gca()
for i, val in enumerate(zip(results['Unit'],results['Mean'])):
    label = str(results.loc[i, 'Mean'])
    ax.annotate(label, val, ha='center')
plt.show()

这给了我错误:

Traceback (most recent call last):
  File "C:\Users\hpoddar\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axis.py", line 1506, in convert_units
    ret = self.converter.convert(x, self.units, self)
  File "C:\Users\hpoddar\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\category.py", line 49, in convert
    raise ValueError(
ValueError: Missing category information for StrCategoryConverter; this might be caused by unintendedly mixing categorical and numeric data

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\hpoddar\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\backends\backend_qt.py", line 477, in _draw_idle
    self.draw()
  File "C:\Users\hpoddar\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\backends\backend_agg.py", line 436, in draw
    self.figure.draw(self.renderer)
  File "C:\Users\hpoddar\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\artist.py", line 73, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File "C:\Users\hpoddar\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\artist.py", line 50, in draw_wrapper
    return draw(artist, renderer)
  File "C:\Users\hpoddar\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\figure.py", line 2837, in draw
    mimage._draw_list_compositing_images(
  File "C:\Users\hpoddar\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "C:\Users\hpoddar\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\artist.py", line 50, in draw_wrapper
    return draw(artist, renderer)
  File "C:\Users\hpoddar\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axes\_base.py", line 3091, in draw
    mimage._draw_list_compositing_images(
  File "C:\Users\hpoddar\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "C:\Users\hpoddar\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\artist.py", line 50, in draw_wrapper
    return draw(artist, renderer)
  File "C:\Users\hpoddar\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\text.py", line 1969, in draw
    if not self.get_visible() or not self._check_xy(renderer):
  File "C:\Users\hpoddar\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\text.py", line 1559, in _check_xy
    xy_pixel = self._get_position_xy(renderer)
  File "C:\Users\hpoddar\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\text.py", line 1552, in _get_position_xy
    return self._get_xy(renderer, x, y, self.xycoords)
  File "C:\Users\hpoddar\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\text.py", line 1419, in _get_xy
    x = float(self.convert_xunits(x))
  File "C:\Users\hpoddar\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\artist.py", line 252, in convert_xunits
    return ax.xaxis.convert_units(x)
  File "C:\Users\hpoddar\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axis.py", line 1508, in convert_units
    raise munits.ConversionError('Failed to convert value(s) to axis '
matplotlib.units.ConversionError: Failed to convert value(s) to axis units: 'Admin '

预期 output:

注释图

为什么在 pandas plot 的情况下出现错误,我该如何解决

matplotlib.axes.Axes.annotate()接受参数xy如下:

xy(浮动,浮动)

要注释的点 (x, y)。 坐标系由 xycoords 确定。

因此,要解决此问题,您可以创建一个具有(i, val[1])的元组,而不是传递包含字符串类型的val

ax = results.plot(x = 'Unit', y = 'Mean', marker = 'o', figsize=(8,5))
for i, val in enumerate(zip(results['Unit'],results['Mean'])):
    label = str(results.loc[i, 'Mean'])
    ax.annotate(text=label, xy=(i, val[1]), ha='center')

另一种选择是使用matplotlib.axes.Axes.text()

ax = results.plot(x = 'Unit', y = 'Mean', marker = 'o', figsize=(8,5))
for i, val in enumerate(zip(results['Unit'],results['Mean'])):
    label = str(results.loc[i, 'Mean'])
    ax.text(x=i, y=val[1], s=label, ha='center')

如您所述,当我们在matplotlib.axes.Axes.plot之后调用annotate()时,没有ConversionError 但是如果我们在pandas.DataFrame.plot之后调用annotate() ,就会出现ConversionError

我最好的猜测是,在使用 matplotlib 到 plot 之后,注释将不会发生转换 但是对于 pandas plot 将尝试转换。 这可能是由于两种情况下 x-ticks 的表示不同。

为了演示,如果我们尝试以下代码,将抛出相同的错误:

fig, ax = plt.subplots(figsize=(18,9))
# Call annotate without plotting! Throws ConversionError
# ax.plot(results['Unit'],results['Mean'])

for i, val in enumerate(zip(results['Unit'],results['Mean'])):
    label = str(results.loc[i, 'Mean'])
    ax.annotate(label, val, ha='center')

而如果我们执行以下操作,则不会引发错误:

fig, ax = plt.subplots(figsize=(18,9))
# Call annotate without plotting! No error
# ax.plot(results['Unit'],results['Mean'])

for i, val in enumerate(zip(results['Unit'],results['Mean'])):
    label = str(results.loc[i, 'Mean'])
    ax.annotate(text=label, xy=(i, val[1]), ha='center')

暂无
暂无

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

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