ItGo.me Focus on IT Recommend

Home > python - Dynamically update plot in iPython notebook

python - Dynamically update plot in iPython notebook

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1074

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

As referred in this question, I am trying to update a plot dynamically in an iPython notebook (in one cell). The difference is that I don't want to plot new lines, but that my x_data and y_data are growing at each iteration of some loop.

What I'd like to do is:

import numpy as np
import time
plt.axis([0, 10, 0, 100]) # supoose I know what the limits are going to be
plt.ion()
plt.show()
x = []
y = []
for i in range(10):
     x = np.append(x, i)
     y = np.append(y, i**2)
     # update the plot so that it shows y as a function of x
     time.sleep(0.5) 

but I want the plot to have a legend, and if I do

from IPython import display
import time
import numpy as np
plt.axis([0, 10, 0, 100]) # supoose I know what the limits are going to be
plt.ion()
plt.show()
x = []
y = []
for i in range(10):
    x = np.append(x, i)
    y = np.append(y, i**2)
    plt.plot(x, y, label="test")
    display.clear_output(wait=True)
    display.display(plt.gcf())
    time.sleep(0.3)
plt.legend()

I end up with a legend which contains 10 items. If I put the plt.legend() inside the loop, the legend grows at each iteration... Any solution?

python matplotlib ipython-notebook
|
  this question
edited Aug 18 '15 at 9:07 asked Aug 17 '15 at 13:15 P. Camilleri 3,439 2 14 38

 | 

1 Answers
1

---Accepted---Accepted---Accepted---

Currently, you are creating a new Axes object for every time you plt.plot in the loop.

Recommend:python - Plot width settings in ipython notebook

of pairs, where first element is x axis and the second is the y axis. points : array_like of pairs like integer and integer List of pairs, where first element is x coordinate and the

So, if you clear the current axis (plt.gca().cla()) before you use plt.plot, and put the legend inside the loop, it works without the legend growing each time:

import numpy as np
import time
from IPython import display

x = []
y = []
for i in range(10):
    x = np.append(x, i)
    y = np.append(y, i**2)
    plt.gca().cla() 
    plt.plot(x,y,label='test')
    plt.legend()
    display.clear_output(wait=True)
    display.display(plt.gcf()) 
    time.sleep(0.5) 

EDIT: As @tcaswell pointed out in comments, using the %matplotlib notebook magic command gives you a live figure which can update and redraw.


|
  this answer
edited Apr 13 at 21:53 Jordan Lewis 7,017 2 16 38 answered Aug 17 '15 at 13:24 tom 15.8k 3 27 47      Better, but if I do that, the legend is not visible until the loop has ended –  P. Camilleri Aug 17 '15 at 13:26      really? that works for me. Where did you put the legend? It needs to be before the two display lines –  tom Aug 17 '15 at 13:27 1   One more thing: when I run my snippet (adding your line and changing legend() location), I end up with two images. Any idea why? –  P. Camilleri Aug 17 '15 at 13:32 1   try switching the order of the display.clear_output and display.display lines. That is discussed in more detail in the answers to the question you linked to in the Q –  tom Aug 17 '15 at 13:36 3   You can do better if you use %matplotlib notebook which gives you a live figure that you can update and re-draw (as well as pan/zoom and have mouse+keyboard events) –  tacaswell Aug 18 '15 at 0:39  |  show more comments

Recommend:python - How to grab matplotlib plot as html in ipython notebook

have a function that accepts various scalars and functions as parameters and outputs a stats pack as some tables and a couple of plots. For automation, I want to be able to format this nicely into a "page" and then call the function in a l

------splitte line----------------------------