I’m trying to add labels to a few values in my matplotlib/seaborn plot. Not all, just those above a certain value (below, using iris from sklearn, labels for values greater than 3.6 on the x-axis).
Here, from @Scinana, last year is a discussion of doing that when both axes are numeric. But while it includes an accepted answer, I’m having trouble adapting it to my situation. The links provided in the accepted answer don't help either.
The code below works until the last step (the labeling), which throws: 'TypeError: 'FacetGrid' object is not callable'
Additionally, the outliers need to be annotated with values from dfiris['sepal length (cm)']
, not just 'outliers'
.
import sklearn as sklearn
from sklearn.datasets import load_iris
dfiris = load_iris()
dfiris = pd.DataFrame(data=dfiris.data, columns=dfiris.feature_names)
dfiris['name'] = np.where(dfiris['sepal width (cm)'] < 3, 'Amy', 'Bruce') # adding a fake categorical variable
dfiris['name'] = np.where((dfiris.name != 'Amy') & (dfiris['petal length (cm)'] >= 3.4), 'Charles', dfiris.name) # adding to that fake categorical variable
a_viz = sns.catplot(x='sepal width (cm)', y= 'name', kind = 'swarm', data=dfiris)
a_viz.fig.set_size_inches(5, 6)
a_viz.fig.subplots_adjust(top=0.81, right=0.86)
for x, y in zip(dfiris['sepal width (cm)'], dfiris['name']):
if x > 3.6:
a_viz.text(x, y, 'outlier', horizontalalignment='left', size='medium', color='black')
The following duplicates didn't completely address the issue with adding annotations from a different column, nor how to prevent the annotations from overlapping.