为什么 matplotlib 滑块在 Colab 中不起作用?

Why matplotlib slider not working in Colab?

我遇到了一个问题,我有一个滑块,但它不是交互式的,我遵循了文档,但即使是现成的解决方案也没有用,如何解决?不适用于 Google Colab 和 Jupyter Notebook。 我已经尝试将 matplotlib 后端内核从 qt 更改为 ktinker 但什么都没有

我的代码:

%matplotlib inline
!pip install --upgrade matplotlib

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button


# The parametrized function to be plotted
def reliability_graph (t,mu_,lambda_):
  return (mu_/(lambda_+mu_))+(lambda_/(lambda_+mu_))*np.exp(-(lambda_+mu_)*t)

t = np.linspace(0, 10,1000)

# Define initial parameters
init_mu = 0
init_lambda = 0.1

# Create the figure and the line that we will manipulate
fig, ax = plt.subplots()
line, = plt.plot(t, reliability_graph (t,init_mu,init_lambda), lw=2)
ax.set_xlabel('Relibility')

# adjust the main plot to make room for the sliders
plt.subplots_adjust(left=0.25, bottom=0.25)

# Make a horizontal slider to control the frequency.
axmu = plt.axes([0.25, 0.1, 0.65, 0.03])
mu_slider = Slider(
    ax=axmu,
    label='Mu',
    valmin=0,
    valmax=1,
    valinit=init_mu,
)

# Make a vertically oriented slider to control the amplitude
axlambda = plt.axes([0.1, 0.25, 0.0225, 0.63])
lambda_slider = Slider(
    ax=axlambda,
    label="Lambda",
    valmin=0,
    valmax=1,
    valinit=init_lambda,
    orientation="vertical"
)


# The function to be called anytime a slider's value changes
def update(val):
    line.set_ydata(reliability_graph(t, mu_slider.val, lambda_slider.val))
    fig.canvas.draw_idle()


# register the update function with each slider
mu_slider.on_changed(update)
lambda_slider.on_changed(update)

# Create a `matplotlib.widgets.Button` to reset the sliders to initial values.
resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', hovercolor='0.975')


def reset(event):
    mu_slider.reset()
    lambda_slider.reset()
button.on_clicked(reset)

plt.show()

我的目标是获得一个带有工作滑块的图表,以便我可以交互地更改参数值。问题是滑块不起作用,它们显示为图片而不是交互式对象

我从未在 Colab 中使用过滑块或按钮,但是通过 运行 您的代码并从错误消息中引入 two-point 库,图形和滑块按钮现在已启用。这里是 link 到 Colab.

#!pip install ipympl
#from google.colab import output
#output.enable_custom_widget_manager()

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button
%matplotlib widget

def reliability_graph (t,mu_,lambda_):
  return (mu_/(lambda_+mu_))+(lambda_/(lambda_+mu_))*np.exp(-(lambda_+mu_)*t)

t = np.linspace(0, 10,1000)

# Define initial parameters
init_mu = 0
init_lambda = 0.1

# Create the figure and the line that we will manipulate
fig, ax = plt.subplots()
line, = plt.plot(t, reliability_graph (t,init_mu,init_lambda), lw=2)
ax.set_xlabel('Relibility')

# adjust the main plot to make room for the sliders
plt.subplots_adjust(left=0.25, bottom=0.25)

# Make a horizontal slider to control the frequency.
axmu = plt.axes([0.25, 0.1, 0.65, 0.03])
mu_slider = Slider(
    ax=axmu,
    label='Mu',
    valmin=0,
    valmax=1,
    valinit=init_mu,
)

# Make a vertically oriented slider to control the amplitude
axlambda = plt.axes([0.1, 0.25, 0.0225, 0.63])
lambda_slider = Slider(
    ax=axlambda,
    label="Lambda",
    valmin=0,
    valmax=1,
    valinit=init_lambda,
    orientation="vertical"
)


# The function to be called anytime a slider's value changes
def update(val):
    line.set_ydata(reliability_graph(t, mu_slider.val, lambda_slider.val))
    fig.canvas.draw_idle()


# register the update function with each slider
mu_slider.on_changed(update)
lambda_slider.on_changed(update)

# Create a `matplotlib.widgets.Button` to reset the sliders to initial values.
resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', hovercolor='0.975')


def reset(event):
    mu_slider.reset()
    lambda_slider.reset()

button.on_clicked(reset)

plt.show()