0

I have a histogram plot and I want to add the values of the VAL in the middle of the bar plot with a color which are fitted with the color of the bar. Thank you. Like the following image I only use the black color to show the number enter image description here

import numpy as np
import seaborn as sns

VAL = [8, 4, 5, 20]
objects = ['h', 'b', 'c', 'a']
y_pos = np.arange(len(objects))

cmap = plt.get_cmap('RdYlGn_r')
norm = plt.Normalize(vmin=min(VAL), vmax=max(VAL))
ax = sns.barplot(x=VAL, y=objects, hue=VAL, palette='RdYlGn_r', dodge=False)

plt.yticks(y_pos, objects)
plt.show()
Sadcow
  • 680
  • 5
  • 13

2 Answers2

1

You just have to add the Axis.text() method before showing the plot to add the text on the chart. Given below the code will help you to understand the method more properly:

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

VAL = [8, 4, 5, 20]
objects = ['h', 'b', 'c', 'a']
y_pos = np.arange(len(objects))

cmap = plt.get_cmap('RdYlGn_r')
norm = plt.Normalize(vmin=min(VAL), vmax=max(VAL))
ax = sns.barplot(x=VAL, y=objects, hue=VAL, palette='RdYlGn_r', dodge=False)

for i, v in enumerate(VAL):
    ax.text(v/2, i, str(v),
            color = 'blue', fontweight = 'bold')

plt.yticks(y_pos, objects)
plt.show()

You can styling the font as well can give the color to the font

Hope it will help you to get the result

Manvi
  • 171
  • 11
0

With regards to adding the value to the middle of the barplot, you can accomplish it with the text function:

for i, v in enumerate(VAL):
    ax.text(v/2, i, str(v))

As for coloring the text with the same color as the bar, I think you wouldn't be able to see the text.

beh aaron
  • 168
  • 7