The best way to implement it using matplotlib.pyplot.bar(range, height, tick_label)
where the range provides scalar values for the positioning of the corresponding bar in the graph. tick_label
does the same work as xticks()
. One can replace it with an integer also and use multiple plt.bar(integer, height, tick_label)
. For detailed information please refer the documentation.
import matplotlib.pyplot as plt
data = {'apple': 67, 'mango': 60, 'lichi': 58}
names = list(data.keys())
values = list(data.values())
#tick_label does the some work as plt.xticks()
plt.bar(range(len(data)),values,tick_label=names)
plt.savefig('bar.png')
plt.show()
Additionally the same plot can be generated without using range()
. But the problem encountered was that tick_label
just worked for the last plt.bar()
call. Hence xticks()
was used for labelling:
data = {'apple': 67, 'mango': 60, 'lichi': 58}
names = list(data.keys())
values = list(data.values())
plt.bar(0,values[0],tick_label=names[0])
plt.bar(1,values[1],tick_label=names[1])
plt.bar(2,values[2],tick_label=names[2])
plt.xticks(range(0,3),names)
plt.savefig('fruit.png')
plt.show()