我试图使用 pandas 绘图方法绘制线图,如果我使用 matplotlib 方法,则相同的数据和完全相同的方法运行良好,但是如果我使用
df.plot
然后注释会给我错误 ValueError: Missing category information for StrCategoryConverter; this might be caused by unintendedly mixing categorical and numeric data
假设我有一个数据框,
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)
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()
上面的代码工作得很好。
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 '
预期输出:
带注释的图表
为什么在使用 pandas 绘图时会出现错误,以及如何解决该错误
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')
正如您所指出的,当我们在
annotate()
之后调用 matplotlib.axes.Axes.plot
时,就没有 ConversionError
。但如果我们在 annotate()
之后调用 pandas.DataFrame.plot
,就会有 ConversionError
。
我最好的猜测是,使用 matplotlib 绘图后,注释上将不会出现 conversion 。但对于 pandas 情节,将尝试进行转换。这可能是由于两种情况下 x 刻度的表示不同。
为了演示,如果我们尝试以下代码,将会抛出相同的错误:
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')
我在提取方法时收到此错误消息,这些方法对于许多图表来说都是相同的。
当数据点的绘制 (
plt.plot(x_values, y_values)
) 不再集成在活动代码中时,会出现该消息。