The 20 bars in my bar plot have labels stored in
channels=['a','b','c','d','e','f','g','h','i','j','k','m','n','o','p','q','r','s','t','u']
To each label there is associated a nonnegative integer between 0 and 100, and I want to plot 5 bars at a time, using a slider to advance through the channels. I'm trying to modify the approach at Scrollable Bar graph matplotlib to do so. Here's what I have so far:
fig,ax=plt.subplots(figsize=(10,6))
x=np.arange(1,20)
y=np.random.randint(0,100, len(x))
channels=
['a','b','c','d','e','f','g','h','i','j','k','m','n','o','p','q','r','s','t','u']
N=5
def bar(pos):
pos = int(pos)
ax.clear()
if pos+N > len(x):
n=len(x)-pos
else:
n=N
X=x[pos:pos+n]
Y=y[pos:pos+n]
ax.bar(X,Y,width=0.7,align='edge',color='green',ecolor='black')
for i,txt in enumerate(X):
ax.annotate(channels[i], (X[i],Y[i]))
barpos = plt.axes([0.18, 0.05, 0.55, 0.03], facecolor="skyblue")
slider = Slider(barpos, 'Channel', 0, len(x)-N, valinit=0,valstep=1)
slider.on_changed(bar)
bar(0)
plt.show()
This is semi-functional, but there is one bug and three minor modifications I can't seem to figure out.
- Currently, when I advance each frame on the slider, the N=5 labels are always 'a','b','c','d','e'. Of course I want the labels to change so that for the second frame, say, the labels are 'b','c','d','e','f'.
- I would like to have my y-axis labels fixed as the slider moves, running from 0 to 100 in increments of 20. In the present form the y-axis labels vary as I move the slider. The increment of the y-axis is sometimes 10, sometimes 20, etc. The maximum y-axis label is generally the maximum of all values in y.
- How can I control the font size of each label?