0

I am a newbie to python and am trying to plot a graph for some frame ids, the frame ids can vary from just about 10 in number to 600 or above in number. Currently, I have this and it works and displays 37 ids together but if I have suppose 500 ids, it clutters them and overlaps the text data. I want to be able to create it in such a way that in one go I only display first 20 ids and there is a scroll bar that displays the next 20 ids and so on.. My code so far:

import matplotlib.pyplot as plt;
import numpy as np

fig,ax=plt.subplots(figsize=(100,2))

x=range(1,38)
y=[1]*len(x)

plt.bar(x,y,width=0.7,align='edge',color='green',ecolor='black')

for i,txt in enumerate(x):

   ax.annotate(txt, (x[i],y[i]))

current=plt.gca()

current.axes.xaxis.set_ticks([])

current.axes.yaxis.set_ticks([])


plt.show()

and my output:

enter image description here

Y. Luo
  • 5,622
  • 1
  • 18
  • 25
cbhush
  • 21
  • 1
  • 5
  • The code does not show any attempt to use a slider. You would have found out about sliders by searching for something like "matplotlib slider". Since this is your first question on SO, I still answered your question, but be aware that you need to show some effort to solve the problem when asking a question here. Also read [ask]. – ImportanceOfBeingErnest Jun 28 '17 at 08:05

1 Answers1

2

Matplotlib provides a Slider widget. You can use this to slice the array to plot and display only the part of the array that is selected.

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

fig,ax=plt.subplots(figsize=(10,6))

x=np.arange(1,38)
y=np.random.rand(len(x))

N=20

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(txt, (X[i],Y[i]))

    ax.xaxis.set_ticks([])
    ax.yaxis.set_ticks([])

barpos = plt.axes([0.18, 0.05, 0.55, 0.03], facecolor="skyblue")
slider = Slider(barpos, 'Barpos', 0, len(x)-N, valinit=0)
slider.on_changed(bar)

bar(0)

plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Thank you so much @ImportanceOfBeingErnest! This is very helpful! I will keep your point in mind! – cbhush Jun 28 '17 at 17:15