【问题标题】:Matplotlib Sliders - Autoscaling of the y-axisMatplotlib 滑块 - y 轴的自动缩放
【发布时间】:2020-04-06 04:08:01
【问题描述】:

为简单起见,考虑以下函数: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()

有人知道如何解决吗?对不起,我只是一个新手;)。 谢谢

【问题讨论】:

    标签: python matplotlib canvas slider autoscaling


    【解决方案1】:

    我已经解决了“问题”。这确实是一个细节,但对于像我这样的新手来说,这并不明显。 这是运行良好的代码:

    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()
    

    【讨论】:

    • 添加关于基本更改的提示 - 不要只删除完整的工作代码!
    【解决方案2】:

    这是一个在 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()
    

    【讨论】:

    • 谢谢你,但是......你的例子中什么是动态的?例如,您可以使用滑块来动态更改 y 轴吗?
    • 我投了反对票,因为答案没有回答如何在使用滑块交互更新绘图时更新轴缩放的问题。
    猜你喜欢
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多