[IPython-User] newbie iPython notebook, how to update matplotlib.pyplot figure/graph?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[IPython-User] newbie iPython notebook, how to update matplotlib.pyplot figure/graph?

Andy Davidson
Hi 
 
I am new to iPthon notebook and matplotlib. I am trying to figure out how to create a loop that does the following

While( not done)
Read new data from network
Update data set
Redraw graph

Bellow is my test code. I figure out how to generated a new graph/fig every time I iterate how ever I can not figure out to pass the figure/plot into my graph function. Clear it and redraw

Any comments or suggestions would be greatly appreciated.

Thanks

Andy



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

dynamicPosData     = [1, 1, 1, 1, 1] #[0, 0, 0, 0, 0] #[2, 12, 3, 7, 9]
dynamicNegData     = [1, 1, 1, 1, 1] #[0, 0, 0, 0, 0] #[1, 10, 2, 4, 8]
dynamicNeutralData = [1, 1, 1, 1, 1] #[0, 0, 0, 0, 0] #[3, 4, 5, 6, 7]
dynamicN = 5

def graph (fig, ax, posDataArg, negDataArg, neutralDataArg, NArg) :
    ind = np.arange(NArg)  # the x locations for the groups
    width = 0.25        # the width of the bars
    fig, ax = plt.subplots() # this will generated a new plot each time, not update existing plot
    ax.clear() # 
    rects1 = ax.bar(ind, negDataArg, width, color='r')
    rects2 = ax.bar(ind+width, neutralDataArg, width, color='y')
    rects3 = ax.bar(ind+width*2, posDataArg, width, color='g')
    ax.set_title('Are People Happy?')
    #ax.set_xticks(ind+width)
    ax.set_xlabel('1 to 5 sec ago')
    ax.legend( (rects1[0], rects2[0], rects3[0]), ('neg', 'unknown', 'pos') )
    fig.canvas.draw() # I thought this would cause the existing graph to update, how ever it does not work?
    
dynamicFig = plt.figure()
dynamicAx = dynamicFig.add_subplot(111)
i = 0
count = 3
while (i < count) :
    graph(dynamicFig, dynamicAx, dynamicPosData, dynamicNegData, dynamicNeutralData, dynamicN)
    plt.show()
    dataIndex = i % dynamicN
    dynamicPosData[dataIndex] = dynamicPosData[dataIndex] + i + 1
    dynamicNeutralData[dataIndex] = dynamicNeutralData[dataIndex] + i + 1
    dynamicNegData[dataIndex] = dynamicNegData[dataIndex] + i + 1
    i += 1
    time.sleep(5) # seconds
    

_______________________________________________
IPython-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-user
Reply | Threaded
Open this post in threaded view
|

Re: newbie iPython notebook, how to update matplotlib.pyplot figure/graph?

Zoltán Vörös-2
Hi Andy,


You should look at the example notebook

/examples/IPython%20Kernel/Animations%20Using%20clear_output.ipynb
<http://localhost:8888/notebooks/ipython/examples/IPython%20Kernel/Animations%20Using%20clear_output.ipynb>


Also, ipython-user is being phased out. If you want to get answers to
your questions, you should post them on ipython-dev.

Cheers,
Zoltán

On 09/21/2014 09:54 PM, Andy Davidson wrote:

> Hi
> I am new to iPthon notebook and matplotlib. I am trying to figure out
> how to create a loop that does the following
>
> While( not done)
> Read new data from network
> Update data set
> Redraw graph
>
> Bellow is my test code. I figure out how to generated a new graph/fig
> every time I iterate how ever I can not figure out to pass the
> figure/plot into my graph function. Clear it and redraw
>
> Any comments or suggestions would be greatly appreciated.
>
> Thanks
>
> Andy
>

_______________________________________________
IPython-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-user
Reply | Threaded
Open this post in threaded view
|

Re: newbie iPython notebook, how to update matplotlib.pyplot figure/graph?

Andy Davidson
Many thanks

Andy


From: Zoltán Vörös <[hidden email]>
Reply-To: "Discussions about using IPython. http://ipython.org" <[hidden email]>
Date: Sunday, September 21, 2014 at 1:04 PM
To: <[hidden email]>
Subject: Re: [IPython-User] newbie iPython notebook, how to update matplotlib.pyplot figure/graph?

Hi Andy,


You should look at the example notebook

/examples/IPython%20Kernel/Animations%20Using%20clear_output.ipynb


Also, ipython-user is being phased out. If you want to get answers to
your questions, you should post them on ipython-dev.

Cheers,
Zoltán

On 09/21/2014 09:54 PM, Andy Davidson wrote:
Hi
I am new to iPthon notebook and matplotlib. I am trying to figure out
how to create a loop that does the following

While( not done)
Read new data from network
Update data set
Redraw graph

Bellow is my test code. I figure out how to generated a new graph/fig
every time I iterate how ever I can not figure out to pass the
figure/plot into my graph function. Clear it and redraw

Any comments or suggestions would be greatly appreciated.

Thanks

Andy


_______________________________________________
IPython-User mailing list


_______________________________________________
IPython-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-user