Matplotlib-এ থ্রেশহোল্ড লাইন সহ বার চার্ট তৈরির পদ্ধতি
Matplotlib-এ থ্রেশহোল্ড লাইন সহ বার চার্ট তৈরির জন্য axhline() মেথড ব্যবহার করতে হবে।
ধাপসমূহ
- ফিগার আকার নির্ধারণ করুন এবং সাবপ্লটের মধ্যবর্তী ও চারপাশের প্যাডিং সমন্বয় করুন।
- একটি ভেরিয়েবল, threshold ইনিশিয়ালাইজ করুন।
- bars মানগুলির জন্য লিস্ট তৈরি করুন।
- থ্রেশহোল্ড মান alapján নিচে এবং উপরের বার মান বের করুন।
- 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()
আউটপুট
