FloatSlide 小部件的光标不移动
Cursor of FloatSlide widget doesn't move
最近,我一直在尝试使用小部件与我的情节进行交互。我创建了一个函数,它接受两个参数来制作我的图,它完美地工作,根据给定参数的值生成不同的图。
但是,当我尝试为这些参数创建 FloatSlider 小部件时,只有其中一个有效。另一种“卡住”——我无法移动滑块 (here is a link of a GIF showing what happens when I try to move the cursor of the slider).
你们知道是什么导致了这个问题吗?
这是我使用的代码:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import ipywidgets as widgets
import matplotlib.ticker as mtick
def plot_2_2(v_0=0,dt=7e-7):
D = 1.5e-6 # diâmetro da gota de óleo [m]
rho = 840 # densidade do líquido [kg/m^3]
g = 9.8 # aceleração da gravidade [m/s^2]
V = np.pi/6*D**3 # volume da gota [m^3]
m = rho*V # massa da partícula [kg]
beta = 1.6e-4 # viscosidade dinâmica do ar [kg/ms]
b = beta*D # coeficiente de atrito linear [kg/s]
t_0 = 0 # tempo [s]
v_0 = v_0 # velocidade inicial da gota [m/s]
n_i = 0 # número de iterações
dt = dt # intervalo de tempo entre iterações [s]
fig = plt.figure(constrained_layout=True, figsize=[14,8])
spec = gridspec.GridSpec(ncols=1,nrows=2,figure=fig)
ax1 = fig.add_subplot(spec[0,0])
ax2 = fig.add_subplot(spec[1,0])
t = np.array([t_0])
v = np.array([v_0])
while v[-1] < (1-1e-4)*m*g/b or n_i < 100:
t = np.append(t,t[-1]+dt) # Passo no tempo
v = np.append(v,v[-1]+(m*g-b*v[-1])*(dt)/m) # Cálculo da velocidade no tempo seguinte
n_i += 1 # Aumento do contador de iterações
l11, = ax1.plot(t,v,'r--') # Solução numérica
l12, = ax1.plot(t,v_0*np.exp(-b/m*t)-(-m*g/b)*(1-np.exp(-b/m*t)),'kx') # Solução analítica
l2, = ax2.plot(t,100*abs(v_0*np.exp(-b/m*t)-(-m*g/b)*(1-np.exp(-b/m*t))),'c--') # Erro relativo
ax1.set_xlabel('Tempo [s]')
ax1.set_ylabel('Velocidade da Gota [m/s]')
ax1.set_ylim(bottom=-0.5e-4,top=1.5e-4)
ax1.ticklabel_format(axis="y", style="sci", scilimits=(0,0))
ax1.set_title("Evolução Velocidade da Gota")
ax1.grid(linestyle='-.')
# ax1.legend([l11,l12],['Solução Numérica','Solução Analítica - $v(t) = v_0 \cdot \exp^{-\dfrac{b}{m}t}$'])
ax2.yaxis.set_major_formatter(mtick.PercentFormatter())
ax2.set_xlabel('Tempo [s]')
ax2.set_ylabel('Erro Relativo')
ax2.set_title("Solução Numérica vs Solução Analítica")
ax2.grid(linestyle='-.')
plt.show(fig)
widgets.interact(plot_2_2, v_0=widgets.FloatSlider(min=0,max=10e-5,step=1e-5,readout_format='.1e'), dt=widgets.FloatSlider(min=5e-7,max=10e-7,step=1e-7,readout_format='.1e'))
谢谢,感谢帮助。
首先,感谢您提供完整的可运行示例(带导入!)
我认为这可能与浮动小部件可以支持的最小值或步长有关。
尝试移动此小部件,我无法更改它:
widgets.FloatSlider(min=5e-7,max=10e-7,step=1e-7,readout_format='.1e')
我想知道这是否仅仅是由于浮点数将小数舍入为零。
我尝试将输入作为整数,并在代码主体中应用比例因子,它似乎工作正常。这可以作为替代方案吗:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import ipywidgets as widgets
import matplotlib.ticker as mtick
def plot_2_2(v_0=0,dt=7e-7):
dt = dt/10_000_000
D = 1.5e-6 # diâmetro da gota de óleo [m]
rho = 840 # densidade do líquido [kg/m^3]
g = 9.8 # aceleração da gravidade [m/s^2]
V = np.pi/6*D**3 # volume da gota [m^3]
m = rho*V # massa da partícula [kg]
beta = 1.6e-4 # viscosidade dinâmica do ar [kg/ms]
b = beta*D # coeficiente de atrito linear [kg/s]
t_0 = 0 # tempo [s]
v_0 = v_0 # velocidade inicial da gota [m/s]
n_i = 0 # número de iterações
dt = dt # intervalo de tempo entre iterações [s]
fig = plt.figure(constrained_layout=True, figsize=[14,8])
spec = gridspec.GridSpec(ncols=1,nrows=2,figure=fig)
ax1 = fig.add_subplot(spec[0,0])
ax2 = fig.add_subplot(spec[1,0])
t = np.array([t_0])
v = np.array([v_0])
while v[-1] < (1-1e-4)*m*g/b or n_i < 100:
t = np.append(t,t[-1]+dt) # Passo no tempo
v = np.append(v,v[-1]+(m*g-b*v[-1])*(dt)/m) # Cálculo da velocidade no tempo seguinte
n_i += 1 # Aumento do contador de iterações
l11, = ax1.plot(t,v,'r--') # Solução numérica
l12, = ax1.plot(t,v_0*np.exp(-b/m*t)-(-m*g/b)*(1-np.exp(-b/m*t)),'kx') # Solução analítica
l2, = ax2.plot(t,100*abs(v_0*np.exp(-b/m*t)-(-m*g/b)*(1-np.exp(-b/m*t))),'c--') # Erro relativo
ax1.set_xlabel('Tempo [s]')
ax1.set_ylabel('Velocidade da Gota [m/s]')
ax1.set_ylim(bottom=-0.5e-4,top=1.5e-4)
ax1.ticklabel_format(axis="y", style="sci", scilimits=(0,0))
ax1.set_title("Evolução Velocidade da Gota")
ax1.grid(linestyle='-.')
# ax1.legend([l11,l12],['Solução Numérica','Solução Analítica - $v(t) = v_0 \cdot \exp^{-\dfrac{b}{m}t}$'])
ax2.yaxis.set_major_formatter(mtick.PercentFormatter())
ax2.set_xlabel('Tempo [s]')
ax2.set_ylabel('Erro Relativo')
ax2.set_title("Solução Numérica vs Solução Analítica")
ax2.grid(linestyle='-.')
plt.show(fig)
widgets.interact(plot_2_2, v_0=widgets.FloatSlider(min=0,max=10e-5,step=1e-5,readout_format='.1e'), dt=widgets.FloatSlider(min=5,max=10,step=1,readout_format='.1e'))
最近,我一直在尝试使用小部件与我的情节进行交互。我创建了一个函数,它接受两个参数来制作我的图,它完美地工作,根据给定参数的值生成不同的图。
但是,当我尝试为这些参数创建 FloatSlider 小部件时,只有其中一个有效。另一种“卡住”——我无法移动滑块 (here is a link of a GIF showing what happens when I try to move the cursor of the slider).
你们知道是什么导致了这个问题吗?
这是我使用的代码:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import ipywidgets as widgets
import matplotlib.ticker as mtick
def plot_2_2(v_0=0,dt=7e-7):
D = 1.5e-6 # diâmetro da gota de óleo [m]
rho = 840 # densidade do líquido [kg/m^3]
g = 9.8 # aceleração da gravidade [m/s^2]
V = np.pi/6*D**3 # volume da gota [m^3]
m = rho*V # massa da partícula [kg]
beta = 1.6e-4 # viscosidade dinâmica do ar [kg/ms]
b = beta*D # coeficiente de atrito linear [kg/s]
t_0 = 0 # tempo [s]
v_0 = v_0 # velocidade inicial da gota [m/s]
n_i = 0 # número de iterações
dt = dt # intervalo de tempo entre iterações [s]
fig = plt.figure(constrained_layout=True, figsize=[14,8])
spec = gridspec.GridSpec(ncols=1,nrows=2,figure=fig)
ax1 = fig.add_subplot(spec[0,0])
ax2 = fig.add_subplot(spec[1,0])
t = np.array([t_0])
v = np.array([v_0])
while v[-1] < (1-1e-4)*m*g/b or n_i < 100:
t = np.append(t,t[-1]+dt) # Passo no tempo
v = np.append(v,v[-1]+(m*g-b*v[-1])*(dt)/m) # Cálculo da velocidade no tempo seguinte
n_i += 1 # Aumento do contador de iterações
l11, = ax1.plot(t,v,'r--') # Solução numérica
l12, = ax1.plot(t,v_0*np.exp(-b/m*t)-(-m*g/b)*(1-np.exp(-b/m*t)),'kx') # Solução analítica
l2, = ax2.plot(t,100*abs(v_0*np.exp(-b/m*t)-(-m*g/b)*(1-np.exp(-b/m*t))),'c--') # Erro relativo
ax1.set_xlabel('Tempo [s]')
ax1.set_ylabel('Velocidade da Gota [m/s]')
ax1.set_ylim(bottom=-0.5e-4,top=1.5e-4)
ax1.ticklabel_format(axis="y", style="sci", scilimits=(0,0))
ax1.set_title("Evolução Velocidade da Gota")
ax1.grid(linestyle='-.')
# ax1.legend([l11,l12],['Solução Numérica','Solução Analítica - $v(t) = v_0 \cdot \exp^{-\dfrac{b}{m}t}$'])
ax2.yaxis.set_major_formatter(mtick.PercentFormatter())
ax2.set_xlabel('Tempo [s]')
ax2.set_ylabel('Erro Relativo')
ax2.set_title("Solução Numérica vs Solução Analítica")
ax2.grid(linestyle='-.')
plt.show(fig)
widgets.interact(plot_2_2, v_0=widgets.FloatSlider(min=0,max=10e-5,step=1e-5,readout_format='.1e'), dt=widgets.FloatSlider(min=5e-7,max=10e-7,step=1e-7,readout_format='.1e'))
谢谢,感谢帮助。
首先,感谢您提供完整的可运行示例(带导入!)
我认为这可能与浮动小部件可以支持的最小值或步长有关。
尝试移动此小部件,我无法更改它:
widgets.FloatSlider(min=5e-7,max=10e-7,step=1e-7,readout_format='.1e')
我想知道这是否仅仅是由于浮点数将小数舍入为零。
我尝试将输入作为整数,并在代码主体中应用比例因子,它似乎工作正常。这可以作为替代方案吗:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import ipywidgets as widgets
import matplotlib.ticker as mtick
def plot_2_2(v_0=0,dt=7e-7):
dt = dt/10_000_000
D = 1.5e-6 # diâmetro da gota de óleo [m]
rho = 840 # densidade do líquido [kg/m^3]
g = 9.8 # aceleração da gravidade [m/s^2]
V = np.pi/6*D**3 # volume da gota [m^3]
m = rho*V # massa da partícula [kg]
beta = 1.6e-4 # viscosidade dinâmica do ar [kg/ms]
b = beta*D # coeficiente de atrito linear [kg/s]
t_0 = 0 # tempo [s]
v_0 = v_0 # velocidade inicial da gota [m/s]
n_i = 0 # número de iterações
dt = dt # intervalo de tempo entre iterações [s]
fig = plt.figure(constrained_layout=True, figsize=[14,8])
spec = gridspec.GridSpec(ncols=1,nrows=2,figure=fig)
ax1 = fig.add_subplot(spec[0,0])
ax2 = fig.add_subplot(spec[1,0])
t = np.array([t_0])
v = np.array([v_0])
while v[-1] < (1-1e-4)*m*g/b or n_i < 100:
t = np.append(t,t[-1]+dt) # Passo no tempo
v = np.append(v,v[-1]+(m*g-b*v[-1])*(dt)/m) # Cálculo da velocidade no tempo seguinte
n_i += 1 # Aumento do contador de iterações
l11, = ax1.plot(t,v,'r--') # Solução numérica
l12, = ax1.plot(t,v_0*np.exp(-b/m*t)-(-m*g/b)*(1-np.exp(-b/m*t)),'kx') # Solução analítica
l2, = ax2.plot(t,100*abs(v_0*np.exp(-b/m*t)-(-m*g/b)*(1-np.exp(-b/m*t))),'c--') # Erro relativo
ax1.set_xlabel('Tempo [s]')
ax1.set_ylabel('Velocidade da Gota [m/s]')
ax1.set_ylim(bottom=-0.5e-4,top=1.5e-4)
ax1.ticklabel_format(axis="y", style="sci", scilimits=(0,0))
ax1.set_title("Evolução Velocidade da Gota")
ax1.grid(linestyle='-.')
# ax1.legend([l11,l12],['Solução Numérica','Solução Analítica - $v(t) = v_0 \cdot \exp^{-\dfrac{b}{m}t}$'])
ax2.yaxis.set_major_formatter(mtick.PercentFormatter())
ax2.set_xlabel('Tempo [s]')
ax2.set_ylabel('Erro Relativo')
ax2.set_title("Solução Numérica vs Solução Analítica")
ax2.grid(linestyle='-.')
plt.show(fig)
widgets.interact(plot_2_2, v_0=widgets.FloatSlider(min=0,max=10e-5,step=1e-5,readout_format='.1e'), dt=widgets.FloatSlider(min=5,max=10,step=1,readout_format='.1e'))