How to add a single marker in a bar graph

Viewed 178

I have the following code generating a bar graph. However, for the last bar, I need a star marker to show that there is no data for the last bar, here in the graph it's number 10.

data = pd.read_csv('data.csv')

df = pd.DataFrame(data)

plt.figure(figsize=(3,2))

X = list(df.iloc[:, 0])
Y = list(df.iloc[:, 1])
Z= list(df.iloc[:, 2])

X_axis = np.arange(len(X))
plt.bar(X_axis - 0.2, Y, 0.4, label='Actual',color='#436bad')
plt.bar(X_axis + 0.2, Z, 0.4, label='Predicted',color='#c5c9c7')

plt.legend(loc=2, prop={'size': 6.5})

labels=['1','2','3','4','5','6','7','8','9','10']
plt.xticks(X,labels,rotation=60)
plt.xlabel("Node no")
plt.ylabel("Accuracy (%)")
plt.ylim(60,95)

graph

1 Answers

You can use plt.text and set * where do you want, like below:

(Because I can't run your code. I send an example)

import matplotlib.pyplot as plt
import numpy as np

x = np.array([1,3,7])
y = [2, 3, 2]
z = [1, 2, 3]

plt.bar(x-0.1, y, width=0.2, color='b', align='center')
plt.bar(x+0.1, z, width=0.2, color='g', align='center')

labels=[1,2,3,4,5,6,7,8,9,10]
plt.xticks(range(1,11),labels,rotation=60)


str_x = [l for l in labels if not l in x]
for s_x in str_x:
    plt.text(s_x, 0.1, '*', ha='center', fontsize=26)

Output:

enter image description here

Related