1

I have a Pandas DataFrame where I have plot a Seaborn Bar Plot, as shown (edit: image not appearing).

I wish to annotate on each bar, the date_install value. I have searched high and low, and can only see references to annotating the bars with the y value of the bar itself.

The price is plotted on the y-axis, with the part_no plotted on the x-axis.

Is this possible within Seaborn?

Thanks

Bar Plot requiring annotation

import pandas as pd
import seaborn as sns

data1 = {
    'date_install': ['2020-02-02','2020-04-03', '2019-03-02'],
    'part_no':['D235','S222','S211'],
    'price': ['1500', '2000', '1600']
}

df = pd.DataFrame(data1)
sns.barplot(x=df.part_no, y=df.price)

0

1 Answer 1

3
import pandas as pd
import seaborn as sns

data1 = {
    'date_install': ['2020-02-02','2020-04-03', '2019-03-02'],
    'part_no':['D235','S222','S211'],
    'price': ['1500', '2000', '1600']
}
df = pd.DataFrame(data1)

plt.figure()
ax = sns.barplot(x=df.part_no, y=df.price)

for p,date in zip(ax.patches, data1['date_install']):
    ax.annotate(date, xy=(p.get_x()+p.get_width()/2, p.get_height()),
                ha='center', va='bottom')

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

This results in TypeError: Neither the x nor y variable appears to be numeric. because 'prince' values are string type. plt.figure() is not needed, and it's easier to use _ = ax.bar_label(ax.containers[0], labels=df.date_install) instead of the for-loop.

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.