【问题标题】:matplotlib.widgets.Slider with fill_betweenmatplotlib.widgets.Slider 与 fill_between
【发布时间】:2020-09-14 10:09:24
【问题描述】:

我正在尝试在类似于slider demo example 的图中添加滑块。

我正在绘制 fill_between,它给出了 PolyCollection 对象。

虽然我也尝试了 plot,它给出了如下图所示的 Line2D 对象,但 plot 没有像 demo 中那样按预期更新。

代码

import numpy as np
import scipy.stats as ss
import matplotlib.pyplot as plt
import matplotlib.widgets as widgets

def get_pdf(mu, sigma=1, offset=4):
    o = sigma * offset
    x = np.linspace(mu - o, mu + o, 100)
    rv = ss.norm(mu, sigma)
    return x, rv.pdf(x)

fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.25)
ax.fill_between(*get_pdf(0, 1), alpha=0.7)
# t = plt.fill_between(*get_pdf(2, 1), alpha=0.7) # this gives ployCollection
t = ax.plot(*get_pdf(2, 1), label='treatment', alpha=0.7)

a = plt.axes([0.25, 0.1, 0.5, 0.03])

slider = widgets.Slider(a, "shift", 0, 10, valinit=2, valstep=1)
def update(val):
    x, y = get_pdf(val)
    t[0].set_ydata(y)
    fig.canvas.draw_idle()

slider.on_changed(update)
plt.show()

【问题讨论】:

    标签: python matplotlib matplotlib-widget


    【解决方案1】:

    要更新线图,需要设置t[0].set_xdata(x),因为每次调用都不同。在这种特殊情况下,get_pdf 每次都返回相同的 y。

    更新fill_between 生成的polyCollection 的坐标似乎是不可能的。但是,您可以在每次更新时删除并重新创建它。请注意,这比仅更新坐标要慢。

    import numpy as np
    import scipy.stats as ss
    import matplotlib.pyplot as plt
    import matplotlib.widgets as widgets
    
    def get_pdf(mu, sigma=1, offset=4):
        o = sigma * offset
        x = np.linspace(mu - o, mu + o, 100)
        rv = ss.norm(mu, sigma)
        return x, rv.pdf(x)
    
    fig, ax = plt.subplots()
    plt.subplots_adjust(bottom=0.25)
    ax.fill_between(*get_pdf(0, 1), alpha=0.7)
    t = ax.fill_between(*get_pdf(2), color='crimson', alpha=0.7)
    a = plt.axes([0.25, 0.1, 0.5, 0.03])
    
    slider = widgets.Slider(a, "shift", 0, 10, valinit=2, valstep=1)
    def update(val):
        global t
        t.remove()
        t = ax.fill_between(*get_pdf(val), color='crimson', alpha=0.7)
        fig.canvas.draw_idle()
    
    slider.on_changed(update)
    plt.show()
    

    【讨论】:

    猜你喜欢
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    相关资源
    最近更新 更多