Matplotlib में थ्रेशोल्ड लाइन के साथ बार चार्ट बनाने के लिए axhline() मेथड का उपयोग करना होगा।
चरण
- फिगर का आकार सेट करें और सबप्लॉट्स के बीच पैडिंग एडजस्ट करें।
- वेरिएबल threshold इनिशियलाइज़ करें।
- bars मानों के लिए लिस्ट्स बनाएं।
- थ्रेशोल्ड मान के आधार पर नीचे और ऊपर के बार मान प्राप्त करें।
- subplots() मेथड का उपयोग करके फिगर और सबप्लॉट्स का सेट बनाएं।
- x, a_threshold और b_threshold मानों के साथ बार्स प्लॉट करें।
- axhline() मेथड का उपयोग करके एक्सिस के पार हॉरिजॉन्टल लाइन जोड़ें।
- फिगर डिस्प्ले करने के लिए show() मेथड का उपयोग करें।
उदाहरण
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
threshold = 10
values = np.array([8.0, 10.0, 15.0, 9.0, 12.0])
x = range(len(values))
a_threshold = np.maximum(values - threshold, 0)
b_threshold = np.minimum(values, threshold)
fig, ax = plt.subplots()
ax.bar(x, b_threshold, 0.35, color="blue")
ax.bar(x, a_threshold, 0.35, color="yellow", bottom=b_threshold)
plt.axhline(threshold, color='red', ls='dotted')
plt.show()
आउटपुट
स्टैक्ड बार चार्ट जहां नीला भाग थ्रेशोल्ड (10) से नीचे और पीला भाग ऊपर है, लाल डॉटेड लाइन थ्रेशोल्ड दर्शाती है।