पायथन मैटप्लोटलिब लाइब्रेरी उन अनुप्रयोगों में उपयोगी है जहां हमें डेटा बिंदुओं की कल्पना करने और डेटा का विश्लेषण करने के लिए ग्राफ़ और प्लॉट बनाने की आवश्यकता होती है। मान लें कि हम एक टिंकर एप्लिकेशन बनाना चाहते हैं जहां हम लाटेक्स सिंटैक्स को संसाधित कर सकें।
LaTex सिंटैक्स का उपयोग वैज्ञानिक दस्तावेज़ीकरण जैसे सूत्र, वैज्ञानिक संकेतन, गणितीय वर्ण और विराम चिह्न तैयार करने के लिए किया जाता है। एप्लिकेशन तैयार करने के लिए, हमें matplotlib . का उपयोग करना होगा और TkAgg (टिंकर में Matplotlib के लिए बैकएंड एपीआई) मॉड्यूल। एप्लिकेशन फ़ंक्शंस और विजेट्स की संरचना के लिए निम्न चरणों का उपयोग किया जाता है,
- आवश्यक पुस्तकालयों जैसे Matplotlib, Tkinter, Ttk (विजेट को स्टाइल करने के लिए), TkAgg, आदि आयात करें।
- एक फ़्रेम जोड़ें और उसमें एक लेबल और एक एंट्री विजेट परिभाषित करें।
- आकृति () . का उपयोग करके चित्र के आकार को परिभाषित करें Matplotlib में विधि। इस आकृति का उपयोग कैनवास में सिंटैक्स को प्लॉट करने के लिए किया जा सकता है।
- अब, एक कैनवास विजेट बनाएं जहां हम TkAgg . का उपयोग करके अपने फिगर को परिभाषित करेंगे ।
- प्रविष्टि विजेट की सामग्री प्राप्त करने के लिए एक फ़ंक्शन को परिभाषित करें और Matplotlib में पूर्वनिर्धारित फ़ंक्शन का उपयोग करके टेक्स्ट को एक आकृति में परिवर्तित करें, अर्थात, text() ।
- फ़ंक्शन के साथ रिटर्न या क्लिक ईवेंट को बाध्य करने के बाद कैनवास पर आउटपुट प्रदर्शित करें।
उदाहरण
# Import required libraries
from tkinter import *
from tkinter import ttk
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
# Use TkAgg in the backend of tkinter application
matplotlib.use('TkAgg')
# Create an instance of tkinter frame
win = Tk()
# Set the size of the window
win.geometry("700x350")
# Set the title of the window
win.title("LaTex Viewer")
# Define a function to get the figure output
def graph(text):
# Get the Entry Input
tmptext = entry.get()
tmptext = "$"+tmptext+"$"
# Clear any previous Syntax from the figure
wx.clear()
wx.text(0.2, 0.6, tmptext, fontsize = 20)
canvas.draw()
# Create a Frame object
frame = Frame(win)
frame.pack()
# Create an Entry widget
var = StringVar()
entry = Entry(frame, width=70, textvariable=var)
entry.pack()
# Add a label widget in the frame
label = Label(frame)
label.pack()
# Define the figure size and plot the figure
fig = matplotlib.figure.Figure(figsize=(7, 4), dpi=100)
wx = fig.add_subplot(111)
canvas = FigureCanvasTkAgg(fig, master=label)
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=1)
# Set the visibility of the Canvas figure
wx.get_xaxis().set_visible(False)
wx.get_yaxis().set_visible(False)
win.bind('<Return>', graph)
win.mainloop() आउटपुट
उपरोक्त कोड को निष्पादित करने पर एक एंट्री विजेट और एक फिगर प्लॉट के साथ एक विंडो प्रदर्शित होगी। अब, परिणामी आउटपुट को LaTex प्रारूप में दिखाने के लिए कुछ वैज्ञानिक व्यंजक टाइप करें।
