pandas - Label points in dataframe Python -
i label data points in pandas x axis value. , trying apply solution code: annotate data points while plotting pandas dataframe
i'm getting error saying:
attributeerror: 'pathcollection' object has no attribute 'text'
here's code:
def draw_scatter_plot(xaxis, yaxis, title, xaxis_label, yaxis_label, save_filename, color, figsize=(9, 7), dpi=100): fig = plt.figure(figsize=figsize, dpi=dpi) ax = plt.scatter(xaxis, yaxis, c=color) plt.xlabel(xaxis_label) plt.ylabel(yaxis_label) label_point(xaxis, yaxis, xaxis, ax) plt.title(title) fig.savefig(save_filename, dpi=100) # label code https://stackoverflow.com/questions/15910019/annotate-data-points-while-plotting-from-pandas-dataframe/15911372#15911372 def label_point(x, y, val, ax): = pd.concat({'x': x, 'y': y, 'val': val}, axis=1) i, point in a.iterrows(): ax.text(point['x'], point['y'], str(point['x']))
any advice on problem?
consider following demo:
in [6]: df = pd.dataframe(np.random.randint(100, size=(10, 2)), columns=list('xy')) in [7]: df out[7]: x y 0 44 13 1 69 53 2 52 80 3 72 64 4 66 42 5 96 33 6 31 13 7 61 81 8 98 63 9 21 95 in [8]: ax = df.plot.scatter(x='x', y='y') in [9]: df.apply(lambda r: ax.annotate(r['x'].astype(str)+'|'+r['y'].astype(str), (r.x*1.02, r.y*1.02)), axis=1) out[9]: 0 annotation(44,13,'44|13') 1 annotation(69,53,'69|53') 2 annotation(52,80,'52|80') 3 annotation(72,64,'72|64') 4 annotation(66,42,'66|42') 5 annotation(96,33,'96|33') 6 annotation(31,13,'31|13') 7 annotation(61,81,'61|81') 8 annotation(98,63,'98|63') 9 annotation(21,95,'21|95') dtype: object
result:
Comments
Post a Comment