1

I want to know why it will be very slow when I use matplotlib to draw lines? How to fix it?

Belows are the demo code. It used plot() to draw a line between two randomly generated points.

On my computer, 'END=100/200/500' results 'FPS=36.9/28.6/20'. I need to endless draw lines and it will get worse while time being. How to solve it? Thanks!

import numpy as np
import matplotlib.pyplot as plt
import time

def draw_demo():
    x = 100
    plt.axis([0, x, 0, 1])
    plt.ion()

    last = 50
    TIME = 5
    END = 1000
    time_start = time.time()

    for i in range(0, END):
        random_num = np.random.random()

        if i > 70:
            plt.axis([x - 100, x + 1, 0, 1])
            x += 1

        plt.plot([i, i + 1], [last, random_num])
        last = random_num

        plt.pause(0.0001)

    print ('FPS:', END/(time.time()-time_start))
    raw_input()

if __name__ == '__main__':
    draw_demo()
2
  • The code posted does not give the text output you describe. Please edit your question to include the exact code you are running. Commented Jan 8, 2016 at 17:55
  • Modified def draw_demo2(): into def draw_demo():. Commented Jan 9, 2016 at 1:54

2 Answers 2

2

Try something like:

import numpy as np
import matplotlib.pyplot as plt
import time

def draw_demo2(ax):
    x = 100
    ax.set_xlim([x-100, x + 250])
    ax.set_ylim([0, 1])

    END = 250
    time_start = time.time()

    ln, = ax.plot([], [])
    x_data = []
    y_data = []
    for i in range(0, END):
        random_num = np.random.random()

        if i%100 == 99:
            cur_xlim = ax.get_xlim()
            ax.set_xlim(np.array(cur_xlim) + 100)
        x += 1
        x_data.append(x)
        y_data.append(random_num)
        ln.set_data(x_data, y_data)
        ax.figure.canvas.draw_idle()
        ax.figure.canvas.flush_events()

    print ('FPS:', END/(time.time()-time_start))


if __name__ == '__main__':
    draw_demo()

It might be worth truncating the x and y data buffers to the view range (as the lists are converted to arrays every time the screen is drawn.

If you need to go really fast look into blitting, however that does not interact well with changing the limits, redrawing the text is one of the slowest parts of drawing the figure.

Also try qt instead of tk, I saw a 4x speed up from that change.

Sign up to request clarification or add additional context in comments.

Comments

0

matplotlib is getting slower as the script progresses because it is redrawing all of the lines that you have previously plotted - even the ones that have scrolled off the screen.

3 Comments

Please do not reach in and touch ax.lines. There is a chance that will be made private in the future (it should have been, but it's naming predates the _-prefix-is-private convention).
If you want to remove the last line you drew keep an ref to it.
Ahh good to know in future. When this change is made will you be keeping the ax.get_lines() part of the API which allows you to still do essentially this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.