0

I want to print an asterisks on top of certain bars (for example, bars with the biggest difference or by name). ax.text is the command I know, but I could not do what I wanted to do.

import numpy as np
import matplotlib.pyplot as plt


group_A = {'a': 0.2939404171023072, 'b': 0.18718821401609165, 'c': 0.07680315854941605, 'd': 0.018221751437274128, 'e': 0.12271298549876533, 'f': 0.25195575250726593, 'g': 0.10341280474995126}
group_B = {'a': 0.3253155986361845, 'b': 0.21567173672424556, 'c': 0.11965759369697668, 'd': 0.010456735282581807, 'e': 0.10248715529720853, 'f': 0.22193945872515902, 'g': 0.11353246654852137}

X = np.arange(len(group_A))
ax = plt.subplot(111)
ax.bar(X, group_A.values(), width=0.2, color='r', align='center')
ax.bar(X - 0.2, group_B.values(), width=0.2, color='b', align='center')
ax.legend(('A', 'B'))
plt.xticks(X, group_A.keys())
plt.xticks(rotation="45")
plt.grid()

# Here
# ax.text(X, group_B.values(), '*', fontsize=12)

plt.show()

What I want is something like this: How to show a sign on top of bar plot

Flodude
  • 255
  • 2
  • 12
  • Does this help: https://stackoverflow.com/questions/19917587/matplotlib-advanced-bar-plot/19919397#19919397 – Paul H Jan 04 '21 at 23:04

1 Answers1

2
for x in [0,2,5]:
    ax.text(x, .95, '*', fontsize=12, transform=ax.get_xaxis_transform())

get_xaxis_transform gets a transformation where x is in data coordinates and y in axis coodinates, so that the asterisks are at 95 % of axes height, not depending on the concrete bar heights.

Stef
  • 28,728
  • 2
  • 24
  • 52