बार प्लॉट के प्रत्येक बार के ऊपर उसका मान दिखाने के लिए पैचेज़ को इन्यूमरेट करके ax.text() का उपयोग करें।
चरण
- फिगर साइज़ और ऑटोलेआउट सेट करें।
- वर्ष, जनसंख्या डेटा और x पोजीशन ऐरे बनाएं।
ax.bar()से बार्स प्लॉट करें और पैचेज़ प्राप्त करें।- प्रत्येक पैच के लिए
get_height(),get_x(),get_width()से पोजीशन कैलकुलेट करें। ax.text()से बार के ऊपर मान लिखें।plt.show()से दिखाएं।
उदाहरण कोड
from matplotlib import pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
years = [1901, 1911, 1921, 1931, 1941, 1951, 1961, 1971, 1981, 1991, 2001, 2011]
population = [237.4, 238.4, 252.09, 251.31, 278.98, 318.66, 361.09, 439.23, 548.16, 683.33, 846.42, 1028.74]
x = np.arange(len(years))
width = 0.35
fig, ax = plt.subplots()
ax.set_ylabel('Population (in million)')
ax.set_title('Years')
ax.set_xticks(x)
ax.set_xticklabels(years)
pps = ax.bar(x - width / 2, population, width, label='population')
for p in pps:
height = p.get_height()
ax.text(x=p.get_x() + p.get_width() / 2, y=height + .10, s=f"{height}", ha='center')
plt.show()
आउटपुट
बार चार्ट जहां प्रत्येक बार के ऊपर जनसंख्या मान प्रदर्शित है।