प्लॉट पर कस्टमाइज़्ड लीजेंड सिंबल प्लॉट करने के लिए, हम निम्नलिखित चरण अपना सकते हैं:
- फिगर का आकार सेट करें और सबप्लॉट के बीच और आसपास पैडिंग समायोजित करें।
- HandlerPatch क्लास इनहेरिट करें, create_artists मेथड ओवरराइड करें, प्लॉट पर अण्डाकार पैच जोड़ें, और पैच हैंडलर लौटाएं।
- Circle क्लास का उपयोग करके प्लॉट पर सर्कल प्लॉट करें।
- वर्तमान एक्सिस पर सर्कल पैच जोड़ें।
- प्लॉट पर लीजेंड रखने के लिए legend() मेथड का उपयोग करें।
- फिगर प्रदर्शित करने के लिए show() मेथड का उपयोग करें।
उदाहरण
import matplotlib.pyplot as plt, matplotlib.patches as mpatches
from matplotlib.legend_handler import HandlerPatch
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
class HandlerEllipse(HandlerPatch):
def create_artists(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans):
center = 0.5 * width - 0.5 * xdescent, 0.5 * height - 0.5 * ydescent
p = mpatches.Ellipse(xy=center, width=width + xdescent, height=height + ydescent)
self.update_prop(p, orig_handle, legend)
p.set_transform(trans)
return [p]
c = mpatches.Circle((0.5, 0.5), 0.25, facecolor="green", edgecolor="red", linewidth=1)
plt.gca().add_patch(c)
plt.legend([c], ["An ellipse, Customized legend element"], handler_map={mpatches.Circle: HandlerEllipse()})
plt.show()आउटपुट
