简体   繁体   中英

Scrollable Bar graph matplotlib

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

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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM