मैटप्लॉटलिब में कस्टम माउस कर्सर बनाने के लिए, हम निम्नलिखित चरण अपना सकते हैं:
चरण
- फिगर का आकार सेट करें और सबप्लॉट्स के बीच और आसपास पैडिंग एडजस्ट करें।
- एक नया फिगर बनाएं या मौजूदा फिगर को सक्रिय करें।
- *mouse_event* फंक्शन को *button_press_event* इवेंट से बाइंड करें।
- नम्पाई का उपयोग करके x और y डेटा पॉइंट्स बनाएं।
- plot() मेथड का उपयोग करके x और y डेटा पॉइंट्स प्लॉट करें।
- फिगर प्रदर्शित करने के लिए Show() मेथड का उपयोग करें।
उदाहरण
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
def mouse_event(event):
print('x: {} and y: {}'.format(event.xdata, event.ydata))
fig = plt.figure()
cid = fig.canvas.mpl_connect('button_press_event', mouse_event)
x = np.linspace(-10, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.show()
आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा:

अब, प्लॉट पर अलग-अलग पॉइंट्स पर क्लिक करें और यह कंसोल पर उनके कोऑर्डिनेट्स दिखाएगा।
x: -3.099305446290094 and y: -0.013811108549791173 x: -0.2865652183685867 and y: -0.2067543563498595 x: -3.0280968329249927 and y: -0.1844916739113902 x: -5.7696284474814 and y: 0.4240216460734405 x: -3.9182044999887626 and y: 0.6837529411889172