mpldatacursor
mpldatacursor copied to clipboard
point labels in pandas.DataFrame.plot
Is there a way to make point labels work with pandas.DataFrame.plot?
I am using WinPython64-3.6.5.0Qt5 which contains mpldatacursor 0.6.2 and pandas 0.22.0. I have tried the following (with graphics backend set to "Qt5"):
import pandas as pd
from mpldatacursor import datacursor
df = pd.DataFrame([[5.1, 3.5, 0], [4.9, 3.0, 0], [7.0, 3.2, 1],
[6.4, 3.2, 1], [5.9, 3.0, 2]],
columns=['length', 'width', 'species'])
ax1 = df.plot.scatter(x='length', y='width')
def cursor_format(x, y, event, ind, label, point_label, s, z, c):
return str(point_label[0])
datacursor(ax1, formatter=cursor_format, point_labels=list(df['species']))
Selecting a point gives me TypeError: cursor_format() missing 3 required positional arguments: 's', 'z', and 'c', and when I remove these three arguments from the function definition, I get TypeError: 'NoneType' object is not subscriptable, because point_label is not defined.
I also tried:
datacursor(ax1, formatter='{point_label}'.format, point_labels=list(df['species']))
This gives me Labels saying "None".
My workaround is to directly use the matplotlib instead:
import matplotlib.pyplot as plt
ax1 = plt.scatter(df['length'], df['width'])
With this, both formatters work fine.