你如何为 a*sinx 编写更新函数,其中 a 是滑块值和振幅
How do you write the update function for a*sinx where a is the slider value and the amplitude
我对 matplotlib 动态可视化和 GUI 还很陌生。那么,你能帮我写下更新函数吗,因为我得到的图绝对不是正弦图。这段代码是我看到很多类似的代码和视频后写的。让我知道我哪里出错了。谢谢。
\\\
import random
import matplotlib
import matplotlib.pyplot as plt
import math
import numpy as np
import tkinter as tk
from matplotlib.widgets import Slider
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
matplotlib.use('TkAgg')
root = tk.Tk()
root.wm_title("Sine graph")
fig = plt.Figure()
canvas = FigureCanvasTkAgg(fig,root)
canvas.get_tk_widget().pack(fill = tk.BOTH, expand =1)
ax = fig.add_subplot(111)
fig.subplots_adjust(bottom = 0.25)
y_values = []
x_values = [random.randrange(-10,11) for _ in range(10)]
for i in range(len(x_values)):
y_values.append(np.sin(2*np.pi*x_values[i]))
ax.axis([(-np.pi-2), (np.pi+2), -10, 10])
ax_value = fig.add_axes([0.12, 0.1, 0.78, 0.03])
s_time = Slider(ax_value, 'Value', 0, 30, valinit=0)
(l,) = ax.plot(x_values, y_values)
def update(val):
pos = s_time.val
l.set_ydata(pos*np.sin(l.get_xdata()))
fig.canvas.draw_idle()
s_time.on_changed(update)
tk.mainloop()
\\\
对我来说所有的问题是你在 x_values
和 y_values
中使用了错误的值
x_values
应该是 -10, 10
范围内的值
y_values
应该是值 sin(x_values)
- 它不需要转换 2*PI
您也可以使用 numpy.array 生成值,这样以后您就不必使用 for
-loop
x_values = np.arange(-10, 10, 0.1)
y_values = np.sin(x_values)
并且在更新中您还应该使用 y_values
而不是 l.get_xdata()
并再次计算 sin()
顺便说一句:您可以使用 val
而不是 s_time.val
def update(val):
new_y_values = val*y_values
line.set_ydata(new_y_values)
fig.canvas.draw_idle()
我也会改axis
。 x_values
在 -10, 10
范围内,y_values
在 -1, 1
范围内,slider
可以有值 0 30
,所以 pos*y_values
可以在范围 -30, 30
ax.axis([-10, 10, -30, 30])
最终我会在顶部和底部添加边距
ax.axis([-10, 10, -31, 31])
完整的工作代码:
import tkinter as tk
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
# --- functions ---
def update(val):
new_y_values = val*y_values
line.set_ydata(new_y_values)
fig.canvas.draw_idle()
# --- main ---
matplotlib.use('TkAgg')
root = tk.Tk()
root.wm_title("Sine graph")
fig = plt.Figure()
canvas = FigureCanvasTkAgg(fig, root)
canvas.get_tk_widget().pack(fill='both', expand =True)
ax = fig.add_subplot(111)
fig.subplots_adjust(bottom = 0.25)
x_values = np.arange(-10, 10, 0.1)
y_values = np.sin(x_values)
print(x_values)
print(y_values)
(line,) = ax.plot(x_values, y_values)
ax.axis([-10, 10, -31, 31])
ax_value = fig.add_axes([0.12, 0.1, 0.78, 0.03])
s_time = Slider(ax_value, 'Value', 0, 30, valinit=1)
s_time.on_changed(update)
root.mainloop()
我对 matplotlib 动态可视化和 GUI 还很陌生。那么,你能帮我写下更新函数吗,因为我得到的图绝对不是正弦图。这段代码是我看到很多类似的代码和视频后写的。让我知道我哪里出错了。谢谢。 \\\
import random
import matplotlib
import matplotlib.pyplot as plt
import math
import numpy as np
import tkinter as tk
from matplotlib.widgets import Slider
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
matplotlib.use('TkAgg')
root = tk.Tk()
root.wm_title("Sine graph")
fig = plt.Figure()
canvas = FigureCanvasTkAgg(fig,root)
canvas.get_tk_widget().pack(fill = tk.BOTH, expand =1)
ax = fig.add_subplot(111)
fig.subplots_adjust(bottom = 0.25)
y_values = []
x_values = [random.randrange(-10,11) for _ in range(10)]
for i in range(len(x_values)):
y_values.append(np.sin(2*np.pi*x_values[i]))
ax.axis([(-np.pi-2), (np.pi+2), -10, 10])
ax_value = fig.add_axes([0.12, 0.1, 0.78, 0.03])
s_time = Slider(ax_value, 'Value', 0, 30, valinit=0)
(l,) = ax.plot(x_values, y_values)
def update(val):
pos = s_time.val
l.set_ydata(pos*np.sin(l.get_xdata()))
fig.canvas.draw_idle()
s_time.on_changed(update)
tk.mainloop()
\\\
对我来说所有的问题是你在 x_values
和 y_values
x_values
应该是 -10, 10
范围内的值
y_values
应该是值 sin(x_values)
- 它不需要转换 2*PI
您也可以使用 numpy.array 生成值,这样以后您就不必使用 for
-loop
x_values = np.arange(-10, 10, 0.1)
y_values = np.sin(x_values)
并且在更新中您还应该使用 y_values
而不是 l.get_xdata()
并再次计算 sin()
顺便说一句:您可以使用 val
而不是 s_time.val
def update(val):
new_y_values = val*y_values
line.set_ydata(new_y_values)
fig.canvas.draw_idle()
我也会改axis
。 x_values
在 -10, 10
范围内,y_values
在 -1, 1
范围内,slider
可以有值 0 30
,所以 pos*y_values
可以在范围 -30, 30
ax.axis([-10, 10, -30, 30])
最终我会在顶部和底部添加边距
ax.axis([-10, 10, -31, 31])
完整的工作代码:
import tkinter as tk
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
# --- functions ---
def update(val):
new_y_values = val*y_values
line.set_ydata(new_y_values)
fig.canvas.draw_idle()
# --- main ---
matplotlib.use('TkAgg')
root = tk.Tk()
root.wm_title("Sine graph")
fig = plt.Figure()
canvas = FigureCanvasTkAgg(fig, root)
canvas.get_tk_widget().pack(fill='both', expand =True)
ax = fig.add_subplot(111)
fig.subplots_adjust(bottom = 0.25)
x_values = np.arange(-10, 10, 0.1)
y_values = np.sin(x_values)
print(x_values)
print(y_values)
(line,) = ax.plot(x_values, y_values)
ax.axis([-10, 10, -31, 31])
ax_value = fig.add_axes([0.12, 0.1, 0.78, 0.03])
s_time = Slider(ax_value, 'Value', 0, 30, valinit=1)
s_time.on_changed(update)
root.mainloop()