I have the following codes which use slider to choose which plots I want to show.
The input are three .xlsx files. The codes read the files and plot each of them by using slider to choose it.
Now I have the problem of fixing the position of the title of the plot.
The codes are shown below
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import ipywidgets as wg
import pandas as pd
import math
from matplotlib.ticker import LinearLocator
%matplotlib widget
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
rm = pd.read_excel("test_3d.xlsx", header = None)
rm1 = pd.read_excel("test_3d1.xlsx", header = None)
rm2 = pd.read_excel("test_3d2.xlsx", header = None)
rec = np.shape(rm)
rmm = np.concatenate([rm,rm1,rm2])
np.min(rmm)
X = np.arange(1,rec[1]+1,1)
Y = np.arange(1,rec[0]+1,1)
x , y = np.meshgrid(X,Y)
# Design a slider to choose which simulation to show
slider = wg.IntSlider(value=1, min=0, max=2, description='this is slider')
slideroutput = wg.Output()
display(slider, slideroutput)
# Initial plots
ax.cla()
plt.title(f"Noise Result {str(slider.value)}")
surf = ax.plot_surface(x, y, rm1, cmap = cm.coolwarm, linewidth = 10, antialiased = False)
ax.set_zlim(math.floor(np.min(rmm)), math.ceil(np.max(rmm)))
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter('{x:.02f}')
# After your choice
numberonslider = []
def on_value_change(change):
with slideroutput:
numberonslider.append(change['new'])
if numberonslider[-1] == 0:
ax.cla()
plt.title(f"Noise Result {str(slider.value)}")
surf = ax.plot_surface(x, y, rm, cmap = cm.coolwarm, linewidth = 10, antialiased = False)
ax.set_zlim(math.floor(np.min(rmm)), math.ceil(np.max(rmm)))
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter('{x:.02f}')
elif numberonslider[-1] == 1:
ax.cla()
plt.title(f"Noise Result {str(slider.value)}")
surf = ax.plot_surface(x, y, rm1, cmap = cm.coolwarm, linewidth = 10, antialiased = False)
ax.set_zlim(math.floor(np.min(rmm)), math.ceil(np.max(rmm)))
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter('{x:.02f}')
elif numberonslider[-1] == 2:
ax.cla()
plt.title(f"Noise Result {str(slider.value)}")
surf = ax.plot_surface(x, y, rm2, cmap = cm.coolwarm, linewidth = 10, antialiased = False)
ax.set_zlim(math.floor(np.min(rmm)), math.ceil(np.max(rmm)))
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter('{x:.02f}')
# Customize the z axis.
slider.observe(on_value_change, names='value')
# Add a color bar which maps values to colors.
cbaxes = fig.add_axes([0.87, 0.25, 0.01, 0.5])
cb = fig.colorbar(surf, cax = cbaxes)
cb.set_label('dBm', rotation=270)
plt.show()
My question is when I run the code (before move the slider), it shows
Now, after moving the slider, the title (Noise Result) of the plot moves to the top of the color bar.
How to fix this problem? Thanks!

