Merging two matplotlib sliders into one

I am currently using 2 sliders to set the yrange of a plot (minimum and maximum). Can I somehow merge the two sliders into one - so that it looks like a regular windows slider may be a custom widget ? I do not want to use tkinter or Qt.

Also, when the program is run 2 windows open, the first one disappears quickly. It's caused by using Slider. Do you know any fix for only popping the main window and not a splash window....

Thirdly I am getting a UserWarning: This figure includes Axes that are not compatible with tight_layout(), so results might be incorrect. Any suggestions would be highly appreciated.

Below is the minimum reproducible example.

############################################################################

import matplotlib.pyplot as plt

import numpy as np

from matplotlib.widgets import Slider

np.random.seed(1977)

fig, ax = plt.subplots()

axes = [ax, ax.twinx(), ax.twinx()]

fig.subplots_adjust(right=0.75)

axes[-1].spines['right'].set_position(('axes', 1.2))

axes[-1].set_frame_on(True)

axes[-1].patch.set_visible(False)


colors = ('Green', 'Red', 'Blue')

for ax, color in zip(axes, colors):

       data = np.random.random(1) * np.random.random(10)

       ax.plot(data, marker='o', linestyle='none', color=color)

       ax.set_ylabel('%s Thing' % color, color=color)

       ax.tick_params(axis='y', colors=color)

axes[0].set_xlabel('X-axis')


YMIN=0

YMAX=1

def update(val): 

       ax.set_ylim(slider1.val, slider2.val)


slider_position1 = plt.axes([0.9, 0.025, 0.01, 0.35], facecolor = "green")

slider_position2 = plt.axes([0.95, 0.025, 0.01, 0.35], facecolor = "skyblue") 

slider1 = Slider(slider_position1,'From', valmin=YMIN, valmax=YMAX, valinit=YMIN, \

orientation="vertical", color = "skyblue", valfmt ='%1.1f')  

slider2 = Slider(slider_position2,'To', valmin=YMIN, valmax=YMAX, valinit=YMAX,\

orientation="vertical", slidermin= slider1, color = "green", valfmt ='%1.1f') 

slider1.slidermax = slider2



slider1.on_changed(update)

slider2.on_changed(update)


fig.tight_layout()

plt.show()
How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum