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()
目前的情况是运行后,Y轴的刻度不会自动适应图表,如何改成Y轴刻度随着 SLIDER 滚动条滚动而改变
这样就行了
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