1

my code:

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
plot_df = pd.DataFrame({"total":[30], "matches":[16],"mismatches":[7]})
p = sns.catplot(data=plot_df, kind="bar")
for index, row in df.iterrows():
    p.text(row.name,row.total.row.matches,row.mismatches, color='black', ha="center")
plt.show()

I trying follow this solution: Seaborn Barplot - Displaying Values

But in my case I get a error:

AttributeError: 'FacetGrid' object has no attribute 'text'

What's wrong?

TeoK
  • 511
  • 6
  • 13
  • 1
    `catplot` is a figure-level interface returning a `FacetGrid`, different from `barplot`, which returns a matplotlib `Axes`. – BigBen Nov 02 '21 at 13:16
  • Also you'll want to use [`bar_label`](https://stackoverflow.com/a/68323374/9245853) if you've got the latest version of matplotlib. – BigBen Nov 02 '21 at 13:22
  • I got this: 'FacetGrid' object has no attribute 'bar_label' – TeoK Nov 02 '21 at 15:09
  • As I said, `catplot` returns a `FacetGrid`, which does not have a `bar_label` attribute. You need to work with a matplotlib `Axes`, not a `FacetGrid`. – BigBen Nov 02 '21 at 15:10
  • 3
    For example `p.axes[0,0].bar_label(p.axes[0,0].containers[0])`. – JohanC Nov 02 '21 at 16:26
  • Adapting JohanC code, this worked here for a single plot with more than 1 container: `[p.axes[0,0].bar_label(container) for container in p.axes[0,0].containers]` – Icaro Bombonato Jan 13 '22 at 15:18

0 Answers0