通过for循环中更新的字典制作可滚动绘图 - python

我有一个由for循环更新的字典,我试图在键值对的数量不断更新的情况下绘制移动或滑动的图中的键值对,并且该图仅显示了该字典中的50个当前值。

到目前为止,我已经做出了:

for k,v in plot.items():

    plt.barh(k,v , color='blue')
    plt.pause(0.3)


plt.show()

问题在于值不断增加,图也不断增长。有什么方法可以显示字典中的最后50 k,v对,并继续更新绘图。我也尝试了一个功能:

def live_plotter(x_vec,y1_data,line1,identifier='',pause_time=0.1):
    if line1==[]:
        # this is the call to matplotlib that allows dynamic plotting
        plt.ion()
        #fig = plt.figure(figsize=(13,6))
        #ax = fig.add_subplot(111)
        # create a variable for the line so we can later update it
        line1, = plt.plot(x_vec,y1_data,'-o', alpha=0.8)
        #update plot label/title
        plt.ylabel('Cross-correlation')
        plt.title('Title: {}'.format(identifier))


    line1.set_data(x_vec, y1_data)

    plt.pause(pause_time)

    return line1 

但这也不会更新绘图,而只是像上面的代码一样追加到绘图中。

我也尝试了动画功能:

fig = plt.figure()
    def animate():
        for k, v in plot:
            print(k, v)
            plt.barh(k, v, color='blue')
            plt.pause(0.3)

ani = matplotlib.animation.FuncAnimation(fig, animate, interval=1000)

plt.show()

可重现的示例:

import matplotlib.pyplot as plt
import matplotlib.animation

plot_base={'00002200': 986.11152642551519,
       '00002201': 989.90915858383369,
       '00002202': 992.87095144990781,
       '00002203': 994.89971071876084,
       '00002204': 992.92424216660561,
       '00002205': 993.19845750930426,
       '00002206': 988.88001766153957,
       '00002207': 988.95062195981848,
       '00002208': 993.17138084871465,
       '00002209': 993.9863425202193,
       '00002210': 993.43551440410283,
       '00002211': 993.04540624076844,
       '00002212': 991.40048097057559,
       '00002213': 993.16124311517319,
       '00002214': 994.06785011666204,
       '00002215': 985.24294182260996,
       '00002216': 990.5369409623512,
       '00002217': 991.83512034302737,
       '00002218': 993.43756392913269,
       '00002219': 989.77919409784511,
       '00002220': 988.09683378239572,
       '00002221': 992.19961090836773,
       '00002222': 992.69477342507912,
       '00002223': 992.37890673842412,
       '00002224': 991.55520651752556,
       '00002225': 992.15414070360941,
       '00002226': 991.49292821478309,
       '00002227': 994.75013161999084,
       '00002228': 991.54858727670728,
       '00002229': 993.22846583401292,
       '00002230': 993.88719133150084,
       '00002231': 992.89934842358855,
       '00002232': 991.10582582918869,
       '00002233': 993.24750746833467,
       '00002234': 992.6478137931806,
       '00002235': 991.2614284514957,
       '00002236': 994.38800336488725}

plot={}

for k,v in plot_base.items():
    plot.update({k: v})


for k,v in plot.items():
    bar, = plt.bar(k, v, color='blue')
    plt.pause(0.3)
plt.plot()


'''
def animate(i):
    bars = []
    for k, v in plot.items():
        bar, = plt.bar(k, v, color='blue')
        bars.append(bar)
    return bars


fig=plt.figure(figsize=(12 ,7))

ani = matplotlib.animation.FuncAnimation(fig, animate, interval=1000, blit=True)

plt.show()
'''

当我们运行此代码时,字典中的值始终附加到x轴上,这就是为什么我要使其能够滚动(自动滚动),三重引用的代码显示了动画部分,这使整个图形立即出现。

参考方案

我认为我不了解这里使用的数据结构,因此本文重点关注动画部分。假设您有一个函数store.get_last_20_values,可从中获取要绘制的数据。然后动画将如下所示:

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np

fig, ax = plt.subplots()

def animate(i):
    cat, val = store.get_last_20_values()
    ax.clear()
    ax.barh(cat, val)

ani = FuncAnimation(fig, animate, interval=1000)
plt.show()

为了完整起见,这里是该函数的外观,使上面的代码可运行。

class Store():
    x = []
    y = []

    def update(self):
        n = np.random.randint(0,5)
        x = np.random.randint(100000,999999, size=n).astype(str)
        y = np.random.randint(50,999, size=n)
        self.x.extend(x)
        self.y.extend(y)
        self.x = self.x[-20:]
        self.y = self.y[-20:]

    def get_last_20_values(self):
        # Some function to return 20 values. Need to adapt to custom needs.
        self.update()
        return self.x, self.y

store = Store()

Matplotlib'粗体'字体 - python

跟随this example:import numpy as np import matplotlib.pyplot as plt fig = plt.figure() for i, label in enumerate(('A', 'B', 'C', 'D')): ax = f…

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python ThreadPoolExecutor抑制异常 - python

from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…