Skip to content Skip to sidebar Skip to footer

How To Label Data Points In Matplotlib Scatter Plot While Looping Through Pandas Dataframes?

I have a pandas dataframe including the following columns: label = ('A' , 'D' , 'K', 'L', 'P') x = (1 , 4 , 9, 6, 4) y = (2 , 6 , 5, 8, 9) plot_id = (1 , 1 , 2, 2, 3) I want to c

Solution 1:

updated answer save separate image files

def annotate(row, ax):
    ax.annotate(row.label, (row.x, row.y),
                xytext=(10, -5), textcoords='offset points')

for pid, grp in df.groupby('plot_id'):
    ax = grp.plot.scatter('x', 'y')
    grp.apply(annotate, ax=ax, axis=1)
    plt.savefig('{}.png'.format(pid))
    plt.close()

1.pngenter image description here

2.pngenter image description here

3.pngenter image description here

old answer for those who want something like this

def annotate(row, ax):
    ax.annotate(row.label, (row.x, row.y),
                xytext=(10, -5), textcoords='offset points')

fig, axes = plt.subplots(df.plot_id.nunique(), 1)
for i, (pid, grp) in enumerate(df.groupby('plot_id')):
    ax = axes[i]
    grp.plot.scatter('x', 'y', ax=ax)
    grp.apply(annotate, ax=ax, axis=1)
fig.tight_layout()

enter image description here

setup

label = ('A' , 'D' , 'K', 'L', 'P')
x = (1 , 4 , 9, 6, 4)
y = (2 , 6 , 5, 8, 9)
plot_id = (1 , 1 , 2, 2, 3)

df = pd.DataFrame(dict(label=label, x=x, y=y, plot_id=plot_id))

Solution 2:

Here is a simple way to deal with your problem :

zipped = zip(zip(zip(df.x, df.y), df.plot_id), df.label)
# Result : [(((1, 2), 1), 'A'),#           (((4, 6), 1), 'D'),#           (((9, 5), 2), 'K'),#           (((6, 8), 2), 'L'),#           (((4, 9), 3), 'P')]

To retrieve the positions, the plot index and the labels, you can loop as below :

for (pos, plot), label in zipped:
    ...
    printposprint plot
    print label

Now here is what you can do in your case :

import matplotlib.pyplot as plt

for (pos, plot), label in zipped:
    plt.figure(plot)
    x, y = pos
    plt.scatter(x, y)
    plt.annotate(label, xy=pos)

It will create as much figures as plot_ids and for each figure display the scatter plot of the points with the corresponding plot_ids value. What's more it overlays the label on each point.

Post a Comment for "How To Label Data Points In Matplotlib Scatter Plot While Looping Through Pandas Dataframes?"