0

Is there a way to plot in an already open window in pandas. Suppose i have a dataframe of 10,000 items and only one column.

what I want to do is to keep plotting only 100 of those points at a time in that open plot window, like a streaming plot maybe.

So the outcome would be a open window that displays line plot of first 100 points then the next 100 points, i.e. 101 to 200 and so on....

i tried looking on the SO but could only find examples like : Real time matplotlib plot is not working while still in a loop I cannot make it play nice with df.plot() method....

Community
  • 1
  • 1
mayank
  • 186
  • 3
  • 12
  • 1
    are you feeding the Axes object to the `plot` method? – Paul H Nov 25 '14 at 20:24
  • For future reference, you could try using `matplotlib`'s animation options for this, e.g. [here](http://matplotlib.org/examples/animation/basic_example.html) – Marius Nov 25 '14 at 21:49
  • I saw that but this method allows me to simply use the plotting functionality in pandas to create various plots that I need. So its easy to just work with dataframes... – mayank Nov 25 '14 at 22:15

2 Answers2

3

put your plot in a loop and call clf() at the end of each loop. That will clear the matplotlib figure.

You may want to pursue treating this as an animation:

http://nbviewer.ipython.org/url/jakevdp.github.io/downloads/notebooks/AnimationEmbedding.ipynb

JD Long
  • 59,675
  • 58
  • 202
  • 294
  • Yes, I firstly tried to make that work but couldn't use the .plot() method of pandas. This method is a little clumsy but allows me to plot using pandas functions. If you can create an example of animation with pandas, I would be grateful. – mayank Nov 25 '14 at 21:21
  • I tried for 10 min & also found it really hard. I'll try again today. I'll probably end up writing an SO question about it ;) – JD Long Nov 26 '14 at 10:58
0

So I figured it out! Also, thanks for the help @JD Long. Here's a sample that works. Instead of using same dataframe values I am creating new columns on the fly and then plotting:

import time
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
pd.options.display.mpl_style = 'default'


df = pd.DataFrame({'a':[3,4,45,6,70,6],'b':[4,4,2,4,2,2]})

plt.axis([0, 50, 0, 100])
plt.ion()
plt.show()

for i in range(1,6):
    df.c = df.a + i
    df.b = df.b - i
    df.c.plot(title = str(i)+'th Iteration')
    df.b.plot(kind='bar', title = str(i)+'th Iteration')
    plt.draw()
    time.sleep(1)
    plt.pause(0.0001)
    plt.clf()
plt.close()
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
mayank
  • 186
  • 3
  • 12
  • so you're using ipython notebook and NOT using inline plots then? – JD Long Nov 25 '14 at 20:43
  • I have pasted this in a single cell of the notebook. and Yeah, I don't want to use inline plots because i feel its better to have a separate window where we can update the plots. – mayank Nov 25 '14 at 20:51
  • hmmm.. what OS are you running? On my Mac this method results in a big stack of plotting windows all stacked on top of each other – JD Long Nov 25 '14 at 20:55
  • i highly recommend creating Figure and Axes instances (`fig, ax = plt.subplots()`) and using those explicitly. – Paul H Nov 25 '14 at 21:00
  • @JD Long, ohh, I am running this on windows 8. I wonder if it doesn't work on mac , it might not run well on linux too. I will try Paul H's idea. I tried it earlier but made some mistake so couldn't make it work. I think that would solve the problem of many windows. – mayank Nov 25 '14 at 21:07
  • If you have a new question, please ask it by clicking the [Ask Question](//stackoverflow.com/questions/ask) button. Don't put it in your other questions or answers. – Remi Guan Apr 20 '16 at 09:51