I have written code to make a 3D surfaceplot with sliders in Python. I also have a colorbar to better visualise what is happening. However, when I move a slider, the colorbar duplicates. How do I fix it, such that the colorbar is also updated and does not duplicate?
I have tried things as fig1.clear() but this results in empty figures
This is my code:
import matplotlib.pyplot as plt
import numpy as np
import sympy as sp
from matplotlib import cm
from matplotlib import widgets as mpl_widgets
import matplotlib.colors as colors
C = 1.6e-19
a = 6.282e-10 #m
b = 3.496e-10 #m
areacell = a * b #m^2
atoms_unit = 2
areaflux = atoms_unit / areacell
ml_s = 7.035 #A/s
eta, N0, R, nu, ii, Ei, Ed, kb, T = sp.symbols("eta N0 R nu ii Ei Ed kb T")
expression = eta * N0 * (R / (nu * N0))**(ii/(ii+2)) * sp.exp((Ei + ii * Ed)/(ii + 2)/(kb * T))
numpy_function = sp.lambdify(
args = (eta, N0, R, nu, ii, Ei, Ed, kb, T),
expr = expression,
modules = "numpy",
)
N0_min, N0_max = 0.00001, 40 #nm^-2
Ed_min, Ed_max = 0.01, 0.5 #eV
flux_min, flux_max = 0.0001, 1000
N0_values = np.linspace(N0_min, N0_max, num = 100)
# N0_values = np.logspace(0, 2, num=100)
Ed_values = np.linspace(Ed_min, Ed_max, num = 100)
X, Y = np.meshgrid(N0_values, Ed_values)
eta_init = 0.1
R_init = 75 #nm^-2 s^-1
nu_init = 1e11 #s^-1
ii_init = 2.5
Ei_init = 0.0715 #eV
kb_init = 8.617e-5 #eV/K
T_init = 250 + 273 #K
Z = numpy_function(eta=eta_init, N0=X, R=R_init, nu=nu_init, ii=ii_init, Ei=Ei_init, Ed = Y, kb=kb_init, T=T_init)
fig1, ax1 = plt.subplots(ncols=1, subplot_kw={"projection": "3d"}, figsize=(8,6))
T_slider = mpl_widgets.Slider(
ax=plt.axes([0.2, 0.1, 0.65, 0.03]),
label=f"${sp.latex(T)}$",
valmin = 175 + 273,
valmax = 400 + 273,
valinit = T_init,
)
R_slider = mpl_widgets.Slider(
ax=plt.axes([0.2, 0.05, 0.65, 0.03]),
label=f"${sp.latex(R)}$",
valmin = flux_min / ml_s * areaflux / 1e18,
valmax = flux_max / ml_s * areaflux / 1e18,
valinit = R_init,
)
ii_slider = mpl_widgets.Slider(
ax=plt.axes([0.2, 0.0, 0.65, 0.03]),
label=f"${sp.latex(ii)}$",
valmin = 2,
valmax = 10,
valinit = ii_init,
)
nu_slider = mpl_widgets.Slider(
ax=plt.axes([0.2, 0.15, 0.65, 0.03]),
label=f"${sp.latex(nu)}$",
valmin = 1e11,
valmax = 1e13,
valinit = nu_init,
)
Ei_slider = mpl_widgets.Slider(
ax=plt.axes([0.2, 0.2, 0.65, 0.03]),
label=f"${sp.latex(Ei)}$",
valmin = 0.001,
valmax = 0.1,
valinit = Ei_init,
)
eta_slider = mpl_widgets.Slider(
ax=plt.axes([0.2, 0.25, 0.65, 0.03]),
label=f"${sp.latex(eta)}$",
valmin = 0,
valmax = 1,
valinit = eta_init,
)
def update_plot(val=None):
global colorbar
T_new = T_slider.val
R_new = R_slider.val
ii_new = ii_slider.val
nu_new = nu_slider.val
Ei_new = Ei_slider.val
eta_new = eta_slider.val
ax1.clear()
Z = numpy_function(eta=eta_new, N0=X, R=R_new, nu=nu_new, ii=ii_new, Ei=Ei_new, Ed=Y, kb=kb_init, T=T_new)
surface = ax1.plot_surface(
X,
Y,
Z,
rstride=3,
cstride=1,
cmap=cm.viridis,
antialiased=False,
norm = colors.LogNorm(vmin=Z.min(), vmax=Z.max())
)
colorbar = fig1.colorbar(surface, ax=ax1, shrink=0.5, aspect=7, pad=0.13)
ax1.set_xlabel(f"${sp.latex(N0)}$ [nm^-2]")
ax1.set_ylabel(f"${sp.latex(Ed)} [eV]$")
ax1.set_zlabel("N [nm^-2]", labelpad=20)
ax1.set_xticks(np.linspace(N0_min, N0_max, num=5)) # Adjust these values according to your preference
ax1.set_yticks(np.linspace(Ed_min, Ed_max, num=5)) # Adjust these values according to your preference
ax1.set_zticks(np.linspace(Z.min(), Z.max(), num=5)) # Adjust these values according to your preference
ax1.set_xticklabels(["{:.3f}".format(x) for x in np.linspace(N0_min, N0_max, num=5)]) # Format tick labels as desired
ax1.set_yticklabels(["{:.3f}".format(y) for y in np.linspace(Ed_min, Ed_max, num=5)]) # Format tick labels as desired
ax1.set_zticklabels(["{:.2e}".format(z) for z in np.linspace(Z.min(), Z.max(), num=5)]) # Format tick labels as desired
ax1.tick_params(axis="z", pad=10)
# ax1.set_xscale('log')
# ax1.set_yscale('log')
# ax1.set_zscale('log')
ax1.set_facecolor("white")
fig1.canvas.draw_idle()
T_slider.on_changed(update_plot)
R_slider.on_changed(update_plot)
ii_slider.on_changed(update_plot)
nu_slider.on_changed(update_plot)
Ei_slider.on_changed(update_plot)
eta_slider.on_changed(update_plot)
update_plot()
plt.subplots_adjust(left=0.05, right=0.95, top=1, bottom=0.3)
plt.show()
Janiek Weening is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.