简体   繁体   English

pandas - 如何将 object 索引转换为轴单位的数字形式?

[英]pandas - How to covert an object index to numeric form for axis units?

I'm trying to annotate a simple bar chart that I coded in seaborn.我正在尝试注释我在 seaborn 中编码的简单条形图。 The problem is that the index value is being unable to get converted to an axis unit.问题是索引值无法转换为轴单位。 I'm not sure why this is happening because I have annotated a previous chart in matplotlib.我不确定为什么会发生这种情况,因为我已经在 matplotlib 中注释了之前的图表。 Here's a sample data frame:这是一个示例数据框:

# Note Q5 is the index

print(df)

Q5  Man   Woman
A   50    55
B   10    11
C   20    30

The code is just plain simple.代码很简单。 Based on Q5 as index, I've plotted values for men and women in two separate subplots.基于 Q5 作为索引,我在两个单独的子图中绘制了男性和女性的值。 Now the annotating part.现在是注释部分。 I'm simplifying the reproduced code here and also skipping the for loop, but say I use the first value for "A" for Man for annotation.我在这里简化了复制的代码并跳过了 for 循环,但是说我使用Man的“A”的第一个值作为注释。

fig, ax = plt.subplots(2,1, figsize=(6, 8))
sb.barplot(x=df.Man, y=df.index, data=df, color='lightblue', ax=ax[0])


ax[0].annotate(format(df.Man["A"], xy=(age_occu.Man["A"], "A"))

# And I get this error
matplotlib.units.ConversionError: Failed to convert value(s) to axis units: 'A'

As usual I've tried several other ways, like instead of mentioning "A" as the y parameter, I specify 0 for the top most y entry.像往常一样,我尝试了其他几种方法,比如没有提到“A”作为 y 参数,而是将 0 指定为最顶部的 y 条目。 But I'm not getting past this error.但我没有克服这个错误。

Note: Also saving the plot is returning the same error.注意:同时保存 plot 会返回相同的错误。

This is because the annotate() does not recognize 'A' as the coordinate.这是因为 annotate() 无法将“A”识别为坐标。 You need to retrieve the actual location of the data in plot:您需要检索 plot 中数据的实际位置:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb

# Note Q5 is the index
df = pd.DataFrame({'Man':[50, 10, 20], 'Woman':[55,11,30]}, index = pd.Index(['A','B','C'], name='Q5'))
fig, ax = plt.subplots(2,1, figsize=(6, 8))
s = sb.barplot(x=df.Man, y=df.index, data=df, color='lightblue', ax=ax[0])


for i in range(len(df)):
    ax[0].annotate(s.patches[i].get_width(),
                   xy=(s.patches[i].get_width(),
                       s.patches[i].get_y() + s.patches[i].get_height()/2))

在此处输入图像描述

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

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