自动调整数据大小以使折线图看起来平滑

问题描述 投票:0回答:1

我正在做一个项目, 从 websocket 中提取数据的地方。

  1. 我正在更新从 websocket 获取的所有数据到图表
  2. 当数据为 < 100 points line chart looks good
  3. 但是随着数据点的添加,它看起来很复杂。
  4. 我希望我的图表保持在 100 点左右

是否有任何实用程序可以自动调整数据点以保持 100。

class MultiColoredLineChartClass:
    """ Multi-Colored Line Chart Class """
    
    def __init__(self):
        pass

    def render_chart(self, parent):
        self.figure = plt.figure(figsize=(5, 4), dpi=60)
        self.ax = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(parent, -1, self.figure)
        return self.canvas

    def update_chart(self, new_data=[]):
        # import random
        # new_data = [random.randint(-10000, 10000) for x in range(0,10)]

        self.ax.clear()
        self.ax.plot(new_data)
        self.ax.set_xlabel('Time')
        self.ax.set_ylabel('PNL')
        self.ax.set_title('Live PNL Chart')
        
        # Redraw the canvas
        self.canvas.draw()
        # thread = threading.Thread(target=self.canvas.draw)
        # thread.daemon = True
        # thread.start()
python matplotlib matplotlib-basemap
1个回答
0
投票

上图显示了绘图的最终状态,你必须运行我的代码来检查它......

首先,我们的网络套接字

def websocket(tmax=1000, dt=0.005):
    from numpy import cos, sin
    w1 = 0.3 ; w2 = 0.4 ; slope = 0.0025
    for t in range(0, tmax):
        sleep(dt)
        yield slope*t + 0.5*(sin(w1*t)-sin(w2*t))
        

接下来我们用它

import matplotlib.pyplot as plt

t, y = [], []
tmin = 0 ; dtmax = 101

fig, ax = plt.subplots(figsize=(5, 2), dpi=96, layout='tight')
line = ax.plot(0, 0)[0] # we need a dummy Line2d object
ax.set_ylim(-1, 4) # do you have an a priori idea of the range of values?
ax.set_xlim(tmin, tmin+dtmax)

for t_, y_ in enumerate(websocket()):
    t.append(t_), y.append(y_)
    tmin = max(tmin, t_-dtmax)
    line.remove()
    ax.set_xlim(tmin, tmin+dtmax)
    line = matplotlib.lines.Line2D(t[tmin:tmin+dtmax],
                                   y[tmin:tmin+dtmax])
    ax.add_line(line)
    fig.canvas.draw()
    plt.pause(0.0001)
© www.soinside.com 2019 - 2024. All rights reserved.