S
S
Sergey Pavlov2014-01-23 17:41:45
Python
Sergey Pavlov, 2014-01-23 17:41:45

How to ignore empty lines when entering a graph function?

There is code in python 2.7, using tkinter-a and matplotlib-a to plot 5 plots in one window, on a single coordinate scale. The program receives function values ​​from 5 input fields of the Entry type. When all the fields are filled in, the program displays the correct chart, but when a field is omitted, an error occurs.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import pylab
from Tkinter import *
 
window=Tk()
window.geometry('400x300')
function_1=Entry(window)
function_1.pack()
function_2=Entry(window)
function_2.pack()
function_3=Entry(window)
function_3.pack()
function_4=Entry(window)
function_4.pack()
function_5=Entry(window)
function_5.pack()
 
def plot(event):
    f_1=str(function_1.get())
    f_2=str(function_2.get())
    f_3=str(function_3.get())
    f_4=str(function_4.get())
    f_5=str(function_5.get())
    x=np.arange(0,10,0.1)
    y_1=eval(f_1)
    y_2=eval(f_2)
    y_3=eval(f_3)
    y_4=eval(f_4)
    y_5=eval(f_5)
    plt.plot(x,y_1,x,y_2,x,y_3,x,y_4,x,y_5)
    plt.show()
 
but=Button(window)
but.pack()
but.bind('<Button-1>',plot)
window.mainloop()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question