Python 图形函数的格式?
这是我的函数图示器程序,它绘制图形:Python 图形函数的格式?,python,function,graph,matplotlib,Python,Function,Graph,Matplotlib,这是我的函数图示器程序,它绘制图形: import wx import numpy import matplotlib from matplotlib.figure import Figure from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas class p1(wx.Panel): def __init__(self , parent): wx.Panel.__i
import wx
import numpy
import matplotlib
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
class p1(wx.Panel):
def __init__(self , parent):
wx.Panel.__init__(self , parent)
self.minimuml = wx.StaticText(self , label = "x,min.:" , pos = (650 , 20))
self.minimum = wx.TextCtrl(self , pos = (700 , 20) , size = (100 , -1))
self.maximuml = wx.StaticText(self , label = "x,max.:" , pos = (650 , 60))
self.maximum = wx.TextCtrl(self , pos = (700 , 60) , size = (100 , -1))
self.pacel = wx.StaticText(self , label = "x,pace:" , pos = (650 , 100))
self.pace = wx.TextCtrl(self , pos = (700 , 100) , size = (100 , -1))
self.functionl = wx.StaticText(self , label ="f(x):" , pos = (650 , 140))
**self.function = wx.TextCtrl(self, pos = (700 , 140) , size = (100 , -1))**
self.button_plot = wx.Button(self , label = "PLOT" , pos = (725 , 200))
self.Bind(wx.EVT_BUTTON , self.Onbutton_plot , self.button_plot)
self.figure = matplotlib.figure.Figure()
self.axes = self.figure.add_subplot(111)
self.canvas = FigureCanvas(self , -1 , self.figure)
def Onbutton_plot(self , event):
self.minimum_Get = float(self.minimum.GetValue())
self.maximum_Get = float(self.maximum.GetValue())
self.pace_Get = float(self.pace.GetValue())
**self.function_Get = int(float(self.function.GetValue()))**
x = numpy.arange(self.minimum_Get , self.maximum_Get + self.maximum_Get/self.maximum_Get , self.pace_Get)
**y = self.function_Get**
self.axes.plot(x , y)
self.canvas = FigureCanvas(self , -1 , self.figure)
class MainFrame(wx.Frame):
def __init__(self , parent , title):
wx.Frame.__init__(self , parent , title = title , size = (800 , 500))
self.CreateStatusBar()
filemenu = wx.Menu()
menuExit = filemenu.Append(wx.ID_EXIT , "Exit" , "End the program")
menuBar = wx.MenuBar()
menuBar.Append(filemenu , "program")
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU , self.Exit , menuExit)
def Exit(self , event):
self.Close(True)
app = wx.App(redirect = False)
frame = MainFrame(None , "grapher")
p1 = p1(frame)
frame.Maximize()
frame.Show()
app.MainLoop()
它获取x值(self.minimum、maximum…)的整个数组,然后使用图形修改现有的void坐标系
我正在为y变量的图形搜索合适的格式。它希望在self.function TextCtrl中输入图形(p.ex.2*x+2),并在按下按钮(def OnButton_plot)时读取+打印它。p、 self.function\u Get变量获取self.function TextCtrl的内容。所以我需要的是这个图形的合适格式,类似于当我在TextCtrl中输入一个数字时,它被转换成一个字符串,然后我可以简单地将它转换成浮点或整数。区别在于,在这种情况下,我没有一个数字,而是一个图形函数,比如:2*x+2或sin(x)
提前感谢为什么前九行缩进?您能给我们一个完整的代码示例吗?和/或删除与问题无关的代码?设身处地为我着想。我可能知道你的问题的答案,但因为我不知道这些变量是什么,我不想花一整天的时间去猜测……我只想在TextCtrl中键入一个图形函数,比如2*x+2,然后稍后使用这个输入来绘制图形。问题是TextCtrl将输入转换为字符串,因此我需要一种格式,可以将字符串函数转换为该格式,以便再次使用它,就像在TextCtrl中输入数字时,我可以稍后将其转换为浮点或整数。这里的区别是我有一个这样的函数:2*x+2编辑你的问题,而不是评论你自己的问题,这样更容易理解。这是我的完整代码。提前谢谢