使用 matplotlib 滑块进行动态绘图

发布于 2025-01-20 00:50:05 字数 1152 浏览 0 评论 0原文

我想在 matplotlib Slider 的帮助下制作动态图。我的函数获取用于绘制 beta 分布函数的 a、b 值数组,并且滑块中的每个值应使用新的 a、b 值更新绘图。 这是我的代码。

def plot_dynamic_beta(a_b_list: np.array, label_name: str):
    def update(val):
        timestamp = time_slider.val
        a, b = a_b_list[timestamp] # new a, b values to draw the new distribution 
        rv = beta(a, b)
        Plot.set_ydata(rv.pdf(np.linspace(0, 1, 100))) # i guess here is the wrong part 
        plt.draw()

    fig, ax = plt.subplots()
    plt.subplots_adjust(bottom=0.25)

    rv = beta(1, 1)
    Plot, = plt.plot(rv.pdf(np.linspace(0, 1, 100)), 'k-', lw=2, label=label_name)
    plt.axis([0, 1, -10, 10])

    # slider_x = np.arange(0, len(a_b_list))
    slider_x = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)
    time_slider = Slider(slider_x, 'timepoint',
                         0, len(a_b_list) - 1, valinit=0, valstep=np.arange(0, len(a_b_list)))

    time_slider.on_changed(update)
    plt.show()

所以它正确地绘制了第一个图,但更改滑块值并没有绘制出我需要的内容。示例 a_b_list= np.array([(1,2),(2,2),(3,2),(4,2)]) 当我更改滑块值时,它不会使用我给出的 a、b 值绘制 beta 分布。例如,如果我将幻灯片更改为 2,它应该绘制 a=3 和 b=2 的 beta 分布,但它不会这样做。 我做错了什么?

I want to make a dynamic plot with the help of matplotlib Slider. My function gets an array of a, b values for plotting beta distribution functions, and each value from slider should update the plot with the new a, b values.
Here is my code.

def plot_dynamic_beta(a_b_list: np.array, label_name: str):
    def update(val):
        timestamp = time_slider.val
        a, b = a_b_list[timestamp] # new a, b values to draw the new distribution 
        rv = beta(a, b)
        Plot.set_ydata(rv.pdf(np.linspace(0, 1, 100))) # i guess here is the wrong part 
        plt.draw()

    fig, ax = plt.subplots()
    plt.subplots_adjust(bottom=0.25)

    rv = beta(1, 1)
    Plot, = plt.plot(rv.pdf(np.linspace(0, 1, 100)), 'k-', lw=2, label=label_name)
    plt.axis([0, 1, -10, 10])

    # slider_x = np.arange(0, len(a_b_list))
    slider_x = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)
    time_slider = Slider(slider_x, 'timepoint',
                         0, len(a_b_list) - 1, valinit=0, valstep=np.arange(0, len(a_b_list)))

    time_slider.on_changed(update)
    plt.show()

so it plots the first plot correctly but changing the slider value doesn't plot what I need. Example a_b_list= np.array([(1,2),(2,2),(3,2),(4,2)])
When I change the slider value it doesn't plot the beta distribution with the a,b values I gave. So for example if I change the slide to 2 it should plot beta distribution with a=3 and b=2 but it doesn't do it.
What did I do wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

书信已泛黄 2025-01-27 00:50:05

我已经绘制了它。这是我的代码,以获取问题中描述的图。

def plot_dynamic_beta(a_b_list: np.array or list, label_name: str):
    fig = go.Figure()
    for a, b in a_b_list:
        beta_new = beta(a, b)
        fig.add_trace(go.Scatter(visible=False,
                                 line=dict(color="#00CED1", width=6),
                                 name="t = " + str(np.where(a_b_list == (a, b))),
                                 x=np.linspace(0, 1, 100),
                                 y=beta_new.pdf(np.linspace(0, 1, 100))))

    # Create and add slider
    steps = []
    for i in range(len(fig.data)):
        step = dict(
            method="update",
            args=[{"visible": [False] * len(fig.data)},
                  {"title": "Beta distribution switched to timestamp: " + str(i)}],  # layout attribute
        )
        step["args"][0]["visible"][i] = True  # Toggle i'th trace to "visible"
        steps.append(step)

    sliders = [dict(
        active=0,
        currentvalue={"prefix": "Frequency: "},
        pad={"t": 100},
        steps=steps
    )]

    fig.update_layout(
        sliders=sliders
    )

    fig.show()

I have got it with plotly. Here is my code to get a plot described in the question.

def plot_dynamic_beta(a_b_list: np.array or list, label_name: str):
    fig = go.Figure()
    for a, b in a_b_list:
        beta_new = beta(a, b)
        fig.add_trace(go.Scatter(visible=False,
                                 line=dict(color="#00CED1", width=6),
                                 name="t = " + str(np.where(a_b_list == (a, b))),
                                 x=np.linspace(0, 1, 100),
                                 y=beta_new.pdf(np.linspace(0, 1, 100))))

    # Create and add slider
    steps = []
    for i in range(len(fig.data)):
        step = dict(
            method="update",
            args=[{"visible": [False] * len(fig.data)},
                  {"title": "Beta distribution switched to timestamp: " + str(i)}],  # layout attribute
        )
        step["args"][0]["visible"][i] = True  # Toggle i'th trace to "visible"
        steps.append(step)

    sliders = [dict(
        active=0,
        currentvalue={"prefix": "Frequency: "},
        pad={"t": 100},
        steps=steps
    )]

    fig.update_layout(
        sliders=sliders
    )

    fig.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文