[英]Matplotlib Sliders - Autoscaling of the y-axis
為簡單起見,請考慮以下函數: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()
有人知道如何解決嗎? 對不起,我只是個新手;)。 謝謝
以下是在 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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.