Python Matplotlib滑块-y轴的自动缩放 为简单起见,考虑以下函数:f(x)=a*SiN(W*x)。我使用滑块来查看这两个参数更改的影响,代码运行得很好,但我有一个问题。如果振幅A太大,则函数的最大值不再出现在画布中(请参阅)。。。我尝试了一些天真的可能性(参见下面的代码):最初定义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()
有人知道怎么修吗?对不起,我只是个新手;)。Python Matplotlib滑块-y轴的自动缩放 为简单起见,考虑以下函数:f(x)=a*SiN(W*x)。我使用滑块来查看这两个参数更改的影响,代码运行得很好,但我有一个问题。如果振幅A太大,则函数的最大值不再出现在画布中(请参阅)。。。我尝试了一些天真的可能性(参见下面的代码):最初定义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(),python,matplotlib,canvas,slider,autoscaling,Python,Matplotlib,Canvas,Slider,Autoscaling,有人知道怎么修吗?对不起,我只是个新手;)。 感谢以下是一个动态更改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() 我已经解决了“问
感谢以下是一个动态更改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()
我已经解决了“问题”。这确实是一个细节,但对于像我这样的新手来说,这并不明显。
下面是运行良好的代码:
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()
谢谢你,但是。。。在您的示例中,什么是动态的?例如,您可以使用滑块动态更改y轴吗?