可滚动条形图matplotlib [英] Scrollable Bar graph matplotlib

查看:99
本文介绍了可滚动条形图matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,正在尝试绘制一些框架ID的图形,框架ID的数量可能从大约10个到600个以上不等. 目前,我有这个,它可以工作并一起显示37个ID,但是如果我假设有500个ID,它将使它们混乱并使文本数据重叠.我希望能够以一种方式创建它,让我一次只显示前20个ID,并且有一个滚动条显示接下来的20个ID,依此类推. 到目前为止,我的代码:

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

和我的输出:

在此处输入图片描述

推荐答案

Matplotlib提供了滑块小部件.您可以使用它来对数组进行切片,以绘制并仅显示所选数组的一部分.

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

这篇关于可滚动条形图matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆