1
$\begingroup$

I would like to annotate the bars for the plot based on this groupby in a static fashion. Given that the cluster groups all happen to have genders that roll up to the same value per cluster, static annotations will look the least busy.

How are the static annotations applied to this plot? Basically, I am looking to label in this way: if cluster group in cluster 0,2, then annotate that bar to the right with "Male", and if 1 then "Female". I've tried many things and just can't get it.

import pandas as pd
import matplotlib.pyplot as plt

data = [[0,'speciesA', 'Male'], [1,'speciesB', 'Female'], [2,'speciesC', 'Male'],
       [0,'speciesA', 'Male'], [1,'speciesC', 'Female'], [2,'speciesA', 'Male'],
       [1,'speciesA', 'Female'], [1,'speciesC', 'Female'], [2,'speciesA', 'Male']]

df = pd.DataFrame(data, columns=['cluster', 'species','gender'])

df.groupby(by=['cluster', 'species']).size().unstack().plot(kind='barh', stacked=True, title='Cluster Makeup').invert_yaxis()

This is what it currently looks like, without annotations

$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.