Scrollable Bar graph matplotlib

不想你离开。 提交于 2019-12-24 20:12:05

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/44791466/scrollable-bar-graph-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!