0

ax.annotate is not working for this particular column. However, it is working for all the other columns in the dataset. Unable to understand why. Any help would be appreciated.

plt.figure(figsize=(15, 8))
sns.countplot(x = 'How many employees does your company or organization have?', data = df)
plt.xticks(rotation = 45, fontsize = 15, ha = "right")
plt.yticks(fontsize = 15)
plt.xlabel('How many employees does your company or organization have?', fontsize = 15)
plt.ylabel('Count', fontsize = 15)
plt.title('Entire Data')
for p in ax.patches:
    width, height = p.get_width(), p.get_height()
    x, y = p.get_xy() 
    ax.annotate(f'{height/df.shape[0]:.0%}', (x + width/2, y + height + 1), ha='center', fontsize = 15)
plt.show()
Beso
  • 1,176
  • 5
  • 12
  • 26
  • can you please describe what "is not working" mean? you get some error or unexpected output? What is your data and desired output? – Ruli Dec 24 '20 at 20:57
  • One problem is that you didn't define `ax`. You could define it as the return value of `countplot`: `ax = sns.countplot(...)`. – JohanC Dec 24 '20 at 22:47
  • @Ruli No, I am getting the output as a bar chart as this is a countplot. But the annotations are not visible in this 'How many employees does your company or organization have' column. While the same code is showing annotations if the column is changed. Is there any way I can send you the ipynb file so that you will have a better understanding? – Brajesh Mohapatra Dec 25 '20 at 07:12
  • @JohanC I doubt that might be the case, as the same code is running fine for the other columns in the dataset. Is there any way I can send you the ipynb file so that you will have a better understanding? – Brajesh Mohapatra Dec 25 '20 at 07:17
  • Well, did you try `ax = sns.countplot(...)`? Did it work that way? It is important to know where you gave a value to `ax`. Possibly your other code works because you really did give a value to `ax`, and that `ax` still points to your previous plot. You might want to read up on [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – JohanC Dec 25 '20 at 11:58

0 Answers0