【问题标题】:Scrollable Bar graph matplotlib可滚动条形图 matplotlib
【发布时间】:2017-06-28 14:52:55
【问题描述】:

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

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

和我的输出:

enter image description here

【问题讨论】:

  • 代码没有显示任何使用滑块的尝试。您会通过搜索“matplotlib 滑块”之类的内容来了解​​滑块。由于这是您关于 SO 的第一个问题,我仍然回答了您的问题,但请注意,在此处提出问题时,您需要付出一些努力来解决问题。另请阅读How to Ask

标签: python matplotlib scrollbar


【解决方案1】:

Matplotlib 提供了一个Slider widget。您可以使用它对数组进行切片以仅绘制和显示选定的数组部分。

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

【讨论】:

  • 非常感谢@ImportanceOfBeingErnest!这很有帮助!我会记住你的观点!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
  • 1970-01-01
  • 2016-01-02
  • 2017-03-27
  • 2012-05-08
相关资源
最近更新 更多