【问题标题】:How to plot two functions with two separate sliders?如何用两个单独的滑块绘制两个函数?
【发布时间】:2022-09-25 02:52:07
【问题描述】:

我试图用 python 提高我不存在的能力,所以我试图复制一个简单的图,它有两个可以交互的函数,基本上是两个函数和两个滑块。这就是我想出的:

%matplotlib inline
from ipywidgets import interactive
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, num=100)

def CovA(m):
    CA = m*x/(1+m*x)
    return CA
    plt.plot(x, CovA(m))
    plt.show()
    
def CovB(n):
    CB = n*x/(1+n*x)
    return CB
    plt.plot(x, CovB(n))
    plt.show()

PlotCovA = interactive(CovA, m=(-2.0, 2.0))
PlotCovB = interactive(CovB, n=(-2.0, 2.0))

但是什么也没有出现,我也尝试过使用滑块,因为这看起来更专业但也更难。我真正要复制的是这段用mathematica编写的代码:https://www.wolframcloud.com/objects/demonstrations/LangmuirIsothermsForABinaryMixture-source.nb 这个使用了在同一张图中绘制的两个函数,其中 5 个滑块改变了函数内部的参数。

  • 你是在 Jupyter 还是 Google Colab 中运行它?

标签: python function matplotlib slider interactive


【解决方案1】:

我自己设法做到了,如果有人找到另一种方法,请告诉我,以便我学习。

我也想使用辅助变量,这样我的函数就不会那么长和凌乱,但我做不到。这是我写的:

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

# Crear subplot
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.2)

# Ajustar el subplot para que quepan los sliders
fig.subplots_adjust(left=0.15, bottom=0.5)

# Crear variable independiente (Presion) y ctes
x = np.linspace(0, 10, num=100)
ko = 1e-08
R = 8.314
m = 1000

# Crear funcion A
koa = (ko*np.exp((50*m)/(R*300)))
FA = (ko*np.exp((50*m)/(R*300)))*(x/(1+2))/(1+(ko*np.exp((50*m)/(R*300)))*(x/(1+2))+(ko*np.exp((80*m)/(R*300)))*(x*4*2/(1+2)))
pFA, = plt.plot(x, FA, label='Cobertura de A')

# Crear funcion B
kob = (ko*np.exp((45*m)/(R*300)))
FB = (ko*np.exp((80*m)/(R*300)))*(x*2/(1+2))/(1+(ko*np.exp((50*m)/(R*300)))*(x/(1+2))+(ko*np.exp((80*m)/(R*300)))*(x*4*2/(1+2)))
pFB, = plt.plot(x, FB, label='Cobertura de B')
 
# Crear axes para ubicar los sliders
ax_TE = plt.axes([0.25, 0.35, 0.65, 0.03])
ax_KA = plt.axes([0.25, 0.30, 0.65, 0.03])
ax_KB = plt.axes([0.25, 0.25, 0.65, 0.03])
ax_alf = plt.axes([0.25, 0.20, 0.65, 0.03])
ax_sit = plt.axes([0.25, 0.15, 0.65, 0.03])

# Crear primer slider desde 200 a 500
# con valor inicial 300
TE = Slider(ax_TE, 'Temperatura', 200, 500, 300)

# Crear segundo slider desde 10 a 80
# con valor inicial 50
KA = Slider(ax_KA, 'Entalpia de A', 10.0, 80.0, 50)
 
# Crear tercer slider desde 10 a 80
# con valor inicial 45
KB = Slider(ax_KB, 'Entalpia de B', 10.0, 80.0, 45)

# Crear cuarto slider desde 0.0 a 10.0
# con valor inicial 2
AL = Slider(ax_alf, 'Razon presion PB/PA', 0.0, 10.0, 2)

# Crear quinto slider desde 0.5 a 10.0
# con valor inicial 1
SI = Slider(ax_sit, 'Razon sitios nB/nA', 0.5, 10.0, 1)

# Funcion que actualiza el valor de las fxes cuando se mueve el slider
def update(val):
    a = KA.val
    b = KB.val
    c = AL.val
    d = SI.val
    e = TE.val

    pFA.set_ydata((ko*np.exp((a*m)/(R*e)))*(x/(1+c))/(1+(ko*np.exp((a*m)/(R*e)))*(x/(1+c))+(ko*np.exp((b*m)/(R*e)))*(x*d*c/(1+c))))
    pFB.set_ydata((ko*np.exp((b*m)/(R*e)))*(x*c/(1+c))/(1+(ko*np.exp((a*m)/(R*e)))*(x/(1+c))+(ko*np.exp((b*m)/(R*e)))*(x*d*c/(1+c))))
    
# Llama a la funcion actualizadora
KA.on_changed(update)
KB.on_changed(update)
AL.on_changed(update)
SI.on_changed(update)
TE.on_changed(update)

# Crear boton de reseteo con 'matplotlib.widgets.Button'
resetax = fig.add_axes([0.8, 0.05, 0.11, 0.05])
button = Button(resetax, 'Resetear', hovercolor='0.975')

def reset(event):
    KA.reset()
    KB.reset()
    AL.reset()
    SI.reset()
    TE.reset()

button.on_clicked(reset)

# Graficar

#print("El valor de KA es: {}".format(koa))
#print("El valor de KB es: {}".format(kob))

plt.suptitle('Isoterma tipo Langmuir')
ax.set_xlabel('Presion (PA+PB) [bar]')
ax.set_ylabel('N de moleculas por sitio')
ax.set_xlim([-0.25, 10.25])
ax.set_ylim([0, 1.2])
leg = ax.legend(loc="best", numpoints=1)
leg.get_frame().set_linewidth(1.0)
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多