matplotlib 如何让图表的Y轴刻度值 随着 轴的滚动而自动调整

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图

import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

x = range(1000)
y = range(1000)

fig,ax = plt.subplots()
ax.plot(x,y,'b')

showbars = 200

def slidebar(pos):
ax.set_xlim(pos - 1, pos + showbars + 1)

plt.grid(alpha=0.4)
slidebarpos = plt.axes([0.2, 0.01, 0.5, 0.03], facecolor="skyblue")
slider = Slider(slidebarpos, '', 0, len(x) - showbars, valinit=0)
slider.on_changed(slidebar)
slidebar(0)
plt.show()

运行结果及报错内容

img

我的解答思路和尝试过的方法
我想要达到的结果

目前的情况是运行后,Y轴的刻度不会自动适应图表,如何改成Y轴刻度随着 SLIDER 滚动条滚动而改变

这样就行了

img

img


import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

x = range(1000)
y = range(1000)

fig,ax = plt.subplots()
ax.plot(x,y,'b')

showbars = 200

def slidebar(pos):
    ax.set_xlim(pos - 1, pos + showbars + 1)
    try:
        ax.set_ylim(y[int(pos)],y[int(pos)+showbars])
    except:
        pass

plt.grid(alpha=0.4)
slidebarpos = plt.axes([0.2, 0.01, 0.5, 0.03], facecolor="skyblue")
slider = Slider(slidebarpos, '', 0, len(x) - showbars, valinit=0)
slider.on_changed(slidebar)
slidebar(0)
plt.show()

设置y轴刻度_Matplotlib最详细的刻度调整
https://blog.csdn.net/weixin_34498545/article/details/112631706