为简单起见,考虑以下函数: f(x) =A* sin(w * x)。我使用滑块来查看这两个参数更改的影响,代码运行得很好,但我有一个问题。如果振幅A太大,则画布中不再有函数的最大值(参见Picture of the resulting situation)……我尝试了一些天真的可能性(参见下面的代码):最初定义y限制,并与函数本身同时更新它们,但只有函数的大小发生变化,y轴没有任何变化……
from matplotlib import pyplot as plt
from matplotlib.widgets import Slider, Button
import numpy as np
## Slider parameters
w_init = 1 # Initial value for slider
w_min = 1 # Minimum value of slider
w_max = 10 # Maximum value of slider
A_init = 1 # Initial value for slider
A_min = 1 # Minimum value of slider
A_max = 10 # Maximum value of slider
## Calculation
x = np.arange(0,10,0.1)
f =A_init * np.sin(w_init*x)
## Plot & Slider
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.15, bottom=0.3)
tot_plot, = plt.plot(x,f, 'b')
plt.ylim(-A_init,A_init)
# Creates the axes for each slider
slidercolor = "blue"
w_slider_axe = plt.axes([0.15, 0.17, 0.7, 0.02])
A_slider_axe = plt.axes([0.15, 0.11, 0.7, 0.02])
# Creates the slider
w_slider = Slider(w_slider_axe, "w", w_min, w_max, valinit = w_init, valfmt="%.1E", color=slidercolor)
A_slider = Slider(A_slider_axe, "A", A_min, A_max, valinit = A_init, valfmt="%.1E", color=slidercolor)
# This function updates all the values of the function and draws the plot again
def update(val):
f = A_slider.val * np.sin(w_slider.val * x)
tot_plot.set_ydata(f)
fig.canvas.draw_idle()
plt.ylim(-A_slider.val,A_slider.val)
w_slider.on_changed(update)
A_slider.on_changed(update)
plt.show()有人知道怎么修吗?对不起,我只是个新手;)。谢谢
发布于 2019-12-13 01:54:29
下面是一个在pyplot中动态改变轴范围的例子:
import matplotlib.pyplot as plt
import time
import random
x = range(0, 10, 1)
y = range(-50, 50, 10)
xdata = []
ydata = []
plt.show()
plt.plot(x, y)
plt.axis([0, 200, 0, 200])
plt.show()发布于 2019-12-20 21:35:44
我已经解决了“问题”。这确实是一个细节,但对于像我这样的新手来说,这并不明显。下面是运行得很好的代码:
from matplotlib import pyplot as plt
from matplotlib.widgets import Slider, Button
import numpy as np
## Slider parameters
w_init = 1 # Initial value for slider
w_min = 1 # Minimum value of slider
w_max = 10 # Maximum value of slider
A_init = 1 # Initial value for slider
A_min = 1 # Minimum value of slider
A_max = 10 # Maximum value of slider
## Calculation
x = np.arange(0,10,0.1)
f =A_init * np.sin(w_init*x)
## Plot & Slider
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.15, bottom=0.3)
tot_plot, = plt.plot(x,f, 'b')
ax.set_ylim(-A_init,A_init)
# Creates the axes for each slider
slidercolor = "blue"
w_slider_axe = plt.axes([0.15, 0.17, 0.7, 0.02])
A_slider_axe = plt.axes([0.15, 0.11, 0.7, 0.02])
# Creates the slider
w_slider = Slider(w_slider_axe, "w", w_min, w_max, valinit = w_init, valfmt="%.1E", color=slidercolor)
A_slider = Slider(A_slider_axe, "A", A_min, A_max, valinit = A_init, valfmt="%.1E", color=slidercolor)
# This function updates all the values of the function and draws the plot again
def update(val):
f = A_slider.val * np.sin(w_slider.val * x)
tot_plot.set_ydata(f)
fig.canvas.draw_idle()
ax.set_ylim(-A_slider.val,A_slider.val)
w_slider.on_changed(update)
A_slider.on_changed(update)
plt.show()https://stackoverflow.com/questions/59310132
复制相似问题