~kiorky/ipython/ipython-readline

« back to all changes in this revision

Viewing changes to docs/examples/lib/gui-mpl.py

  • Committer: Brian Granger
  • Date: 2009-08-31 22:21:10 UTC
  • mfrom: (1152.2.31 inputhook)
  • Revision ID: ellisonbg@gmail.com-20090831222110-5d6rcwijplf3y97r
Merging the inputhook branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Test the new %gui command.  Run this in ipython as
 
2
 
 
3
In [1]: %gui [backend]
 
4
 
 
5
In [2]: %run switchgui [backend]
 
6
 
 
7
where the optional backend can be one of:  qt4, gtk, tk, wx.
 
8
 
 
9
Because of subtle difference in how Matplotlib handles the different GUI 
 
10
toolkits (in things like draw and show), minor modifications to this script
 
11
have to be made for Tk to get it to work with the 0.99 and below releases
 
12
of Matplotlib.  However, in the future, Matplotlib should be able to have 
 
13
similar logic for all the toolkits, as they are all now using PyOS_InputHook.
 
14
"""
 
15
 
 
16
import sys
 
17
import time
 
18
 
 
19
from  IPython.lib import inputhook
 
20
 
 
21
gui = inputhook.current_gui()
 
22
if gui is None:
 
23
    gui = 'qt4'
 
24
    inputhook.enable_qt4(app=True)
 
25
 
 
26
backends = dict(wx='wxagg', qt4='qt4agg', gtk='gtkagg', tk='tkagg')
 
27
 
 
28
import matplotlib
 
29
matplotlib.use(backends[gui])
 
30
matplotlib.interactive(True)
 
31
 
 
32
import matplotlib
 
33
from matplotlib import pyplot as plt, mlab, pylab
 
34
import numpy as np
 
35
 
 
36
from numpy import *
 
37
from matplotlib.pyplot import *
 
38
 
 
39
x = np.linspace(0,pi,500)
 
40
 
 
41
print "A plot has been created"
 
42
line, = plot(x,sin(2*x))
 
43
inputhook.spin() # This has to be removed for Tk
 
44
 
 
45
 
 
46
print "Now, we will update the plot..."
 
47
print
 
48
for i in range(1,51):
 
49
    print i, 
 
50
    sys.stdout.flush()
 
51
    line.set_data(x,sin(x*i))
 
52
    plt.title('i=%d' % i)
 
53
    plt.draw()
 
54
    inputhook.spin() # This has to be removed for Tk