2017-07-03 68 views
2

我有一个数据集采用如何从数据透视表数据框列注释图表?

a b c d 
10-Apr-86 Jimmy 1 this is 
11-Apr-86 Minnie 2 the way 
12-Apr-86 Jimmy 3 the world 
13-Apr-86 Minnie 4 ends 
14-Apr-86 Jimmy 5 this is the 
15-Apr-86 Eliot 6 way 
16-Apr-86 Jimmy 7 the world ends 
17-Apr-86 Eliot 8 not with a bang 
18-Apr-86 Minnie 9 but a whimper 

我想打一个图表中matplotlib,看起来像这样

enter image description here

我已经计算出如何得到公正的点(没有注释)下面的代码:

df = (pd.read_csv('python.csv')) 
df_wanted = pd.pivot_table(
    df, 
    index='a', 
    columns='b', 
    values='c') 

df_wanted.index = pd.to_datetime(df_wanted.index) 

plt.scatter(df_wanted.index, df_wanted['Jimmy']) 
plt.scatter(df_wanted.index,df_wanted['Minnie']) 
plt.scatter(df_wanted.index,df_wanted['Eliot']) 

我认为要注释,我需要一个值的列表(如演示here),我的数据透视表的最后一栏

我的问题是:我怎么获取原始数据集的最后一列“d”成为我的数据透视表的最后一栏?

我试图dat1 = pd.concat([df_wanted, df['d']], axis = 1) - 但这建立了一套新的数据框我的行下面的行。我意识到轴是不一样的,所以我试图做一个新的数据透视表与d列值 - 但得到错误信息No numeric types to aggregate

我试图df_wanted2.append(df['d']) - 但是这提出了一个新列列d每个元素。

有什么建议吗?最后,我希望把它使数据标签出现在一个翻转鼠标

回答

1

在这种特定的情况来看,它似乎并不需要设置列d为您的数据透视表的最后一栏。

plt.scatter(df_wanted.index, df_wanted['Jimmy']) 
plt.scatter(df_wanted.index,df_wanted['Minnie']) 
plt.scatter(df_wanted.index,df_wanted['Eliot']) 
plt.legend(loc=0) 

for k, v in df.set_index('a').iterrows(): 
    plt.text(k, v['c'], v['d']) # or: plt.annotate(xy=(k, v['c']), s=v['d']) 

enter image description here

相关问题