新手求助,通过滑块控制abc三个变量,来控制二次函数图像。
import tkinter as tkimport matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
from mpl_toolkits import axisartist
window = tk.Tk()
window.title("二次函数")
window.geometry("600x600")
figure = Figure(figsize=(5, 4), dpi=100)
axes = axisartist.Subplot(figure, 111)
figure.add_axes(axes)
canvas = FigureCanvasTkAgg(figure, master=window)
toolbar = NavigationToolbar2Tk(canvas, window)
toolbar.update()
canvas.get_tk_widget().pack()
l = tk.Label(window,bg = "yellow",width = 20,text = "一元二次函数")
l.pack()
def Repaint(source):
a_value = a_scale.get()
b_value = b_scale.get()
c_value = c_scale.get()
# 计算绘图中线
mid = 0
if a_value != 0:
mid = -b_value / 2 / a_value
x = np.linspace(mid - 10, mid + 10, 200)
y = np.zeros(x.shape)
for i in range(x.shape):
y = a_value * x ** 2 + b_value * x + c_value
y_min, y_max = -10, 10
if a_value > 0:
y_min, y_max = min(y) - 5, 50
elif a_value < 0:
y_min, y_max = -50, max(y) + 5
ax = plt.gca()
ax.spines["right"].set_color("none")
ax.spines["top"].set_color("none")
ax.xaxis.set_ticks_position("bottom")
ax.yaxis.set_ticks_position("left")
ax.spines["bottom"].set_position(("data",0))
ax.spines["left"].set_position(("data",0))
a_scale = tk.Scale(window,label = "a",from_ = -10,to = 10,orient = tk.HORIZONTAL,
length = 200,showvalue = 0,tickinterval = 2,resolution = 1,command = Repaint)
b_scale = tk.Scale(window,label = "b",from_ = -10,to = 10,orient = tk.HORIZONTAL,
length = 200,showvalue = 0,tickinterval = 2,resolution = 1,command = Repaint)
c_scale = tk.Scale(window,label = "c",from_ = -10,to = 10,orient = tk.HORIZONTAL,
length = 200,showvalue = 0,tickinterval = 2,resolution = 1,command = Repaint)
a_scale.pack()
b_scale.pack()
c_scale.pack()
a = a_scale.get()
b = b_scale.get()
c = c_scale.get()
l = tk.Label(window,text = "二次函数图像",font = ("Arial",12),width = 15,height = 2)
l.pack()
plt.show()
window.mainloop() 兄弟们为什么我写完的代码,会出现两张只有坐标的图,并没有一次函数。 ? 怎么发照片呀。。 . 超级赛亚人哦哦 发表于 2020-8-5 21:05
怎么发照片呀。。
【技巧】新鱼油怎么传图片?
https://fishc.com.cn/thread-168190-1-1.html
(出处: 鱼C论坛)
您现在无权限上传本地图片
但我们需要看图片解决问题,请您根据以下步骤正确上传图片!
static/image/hrline/1.gif
图床地址:路过图床 - 免费图片上传, 专业图片外链, 免费公共图床
http://m.qpic.cn/psc?/V1046bnt0Whegz/fFWTTr*vfq2.Z2Ez3gOL3YrkNNI6nsAV404E460eFwTWRhMzPWnEDfBP.zkLauZ9.9*n.uDHpbS4nyangdo3gw!!/b&bo=agcPAwAAAAADB0M!&rf=viewer_4&t=5 https://imgchr.com/i/aybNHf Hello. 发表于 2020-8-5 21:08
您现在无权限上传本地图片
但我们需要看图片解决问题,请您根据以下步骤正确上传图片!
谢谢 liuzhengyuan 发表于 2020-8-5 21:08
【技巧】新鱼油怎么传图片?
https://fishc.com.cn/thread-168190-1-1.html
(出处: 鱼C论坛)
谢谢 https://imgchr.com/i/aybYut 以上是最后跑出来的图 https://imgchr.com/i/ayLgtU 这个是最后想要的 兄弟萌可以解答一下吗 本帖最后由 sunrise085 于 2020-8-6 19:27 编辑
看39行到55行
import tkinter as tk
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
from mpl_toolkits import axisartist
window = tk.Tk()
window.title("二次函数")
window.geometry("600x600")
figure = Figure(figsize=(5, 4), dpi=100)
axes = axisartist.Subplot(figure, 111)
figure.add_axes(axes)
canvas = FigureCanvasTkAgg(figure, master=window)
toolbar = NavigationToolbar2Tk(canvas, window)
toolbar.update()
canvas.get_tk_widget().pack()
l = tk.Label(window,bg = "yellow",width = 20,text = "一元二次函数")
l.pack()
def Repaint(source):
a_value = a_scale.get()
b_value = b_scale.get()
c_value = c_scale.get()
# 计算绘图中线
mid = 0
if a_value != 0:
mid = -b_value / 2 / a_value
x = np.linspace(mid - 10, mid + 10, 200)
y = np.zeros(x.shape)
for i in range(x.shape):
y = a_value * x ** 2 + b_value * x + c_value
y_min, y_max = -10, 10
if a_value > 0:
y_min, y_max = min(y) - 5, 50
elif a_value < 0:
y_min, y_max = -50, max(y) + 5
#这里添加这四句,第一句清除原有图形,第二句根据新的xy值图形,第三句和第四句重绘图形。
axes.clear()
axes.plot(x,y)
figure.canvas.draw()
figure.canvas.flush_events()
#下面这几句导致又绘制了一张图
'''ax = plt.gca()
ax.spines["right"].set_color("none")
ax.spines["top"].set_color("none")
ax.xaxis.set_ticks_position("bottom")
ax.yaxis.set_ticks_position("left")
ax.spines["bottom"].set_position(("data",0))
ax.spines["left"].set_position(("data",0)) '''
a_scale = tk.Scale(window,label = "a",from_ = -10,to = 10,orient = tk.HORIZONTAL,
length = 200,showvalue = 0,tickinterval = 2,resolution = 1,command = Repaint)
b_scale = tk.Scale(window,label = "b",from_ = -10,to = 10,orient = tk.HORIZONTAL,
length = 200,showvalue = 0,tickinterval = 2,resolution = 1,command = Repaint)
c_scale = tk.Scale(window,label = "c",from_ = -10,to = 10,orient = tk.HORIZONTAL,
length = 200,showvalue = 0,tickinterval = 2,resolution = 1,command = Repaint)
a_scale.pack()
b_scale.pack()
c_scale.pack()
a = a_scale.get()
b = b_scale.get()
c = c_scale.get()
l = tk.Label(window,text = "二次函数图像",font = ("Arial",12),width = 15,height = 2)
l.pack()
plt.show()
window.mainloop() 今天我又看了一下昨天的这个程序,找到重绘曲线的方法了。我把16楼的答案更新了。可以直接用了
页:
[1]