슬라이드 1 - LINK@KoreaTech

Download Report

Transcript 슬라이드 1 - LINK@KoreaTech

Python & NetworkX
2013. 3
Youn-Hee Han
LINK@KOREATECH
http://link.koreatech.ac.kr
Why Python?
 Python has been considered as a “godsend” to all developers.
 It miraculously combines all the good things we always wanted
– It is incredibly fast
– It makes it easy to develop complex yet robust enterprise-level
systems
– It is designed to maintain clarity as development expands.
 It enables “exploratory programming”
LINK@KOREATECH
2
Installing Python 2.7 & NetworkX 1.7
LINK@KOREATECH
3
VirtualBox and Python 2.7.2
 Environment Recommendation  Ubuntu in VirtualBox
– In this material…
• Ubuntu 11.10
• Python 2.7.2
LINK@KOREATECH
4
iPython
 iPython (http://ipython.org)
– It is an enhanced Python shell and an architecture for interactive
parallel computing.
– It has everything to make your Python life much easier, from tab
completion to inline code inspection and interactive debugging.
– In addition, IPython has a special pylab mode for matplotlib.
– Terminal
– Qt Console
– HTML Notebook
 Just run:
– easy_install ipython
LINK@KOREATECH
5
NetworkX
 NetworkX (http://networkx.lanl.gov/)
– It is a Python language software package for the creation,
manipulation, and study of the structure, dynamics, and functions of
complex networks.
 Just run:
– easy_install networkx
LINK@KOREATECH
6
numpy and matplotlib
 numpy (http://networkx.lanl.gov/)
– It is the fundamental Python package for scientific computing with
Python.
– It contains among other things:
•
•
•
•
a powerful N-dimensional array object
sophisticated (broadcasting) functions
tools for integrating C/C++ and Fortran code
useful linear algebra, Fourier transform, and random number capabilities
 matplotlib (http://matplotlib.org/)
– It is a python 2D plotting library which produces publication quality
figures in a variety of hardcopy formats and interactive environments
across platforms.
 Just run:
– easy_install numpy
– easy_install matplotlib
LINK@KOREATECH
7
numpy and matplotlib
 Graphic Library  backend control
– http://stackoverflow.com/questions/7534453/matplotlib-does-notshow-my-drawings-although-i-call-pyplot-show
– Location of “matplotlibrc” file
• /usr/local/lib/python2.7/dist-packages/matplotlib-1.2.0-py2.7-linuxi686.egg/matplotlib/mpl-data/matplotlibrc
#backend
backend
: Agg
: GTK3Agg
 Just run
– easy_install PyGTK
 [Note] In MAC OS, you use X11
LINK@KOREATECH
8
Test Code
 Simple Test Code
simple.py
import networkx as net
import matplotlib.pyplot as plt
g=net.Graph()
g.add_edge('a','b')
net.draw(g)
plt.show()
– ipython simple.py
LINK@KOREATECH
9
Test Code
 Basic Animation
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def update_line(num, data, line):
line.set_data(data[...,:num])
return line,
basic_animation.py
fig1 = plt.figure()
data = np.random.rand(2, 25)
l, = plt.plot([], [], 'r-')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.xlabel('x')
plt.title('test')
line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),
interval=50, blit=True)
#line_ani.save('lines.mp4')
fig2 = plt.figure()
x = np.arange(-9, 10)
y = np.arange(-9, 10).reshape(-1, 1)
base = np.hypot(x, y)
ims = []
for add in np.arange(15):
ims.append((plt.pcolor(x, y, base + add, norm=plt.Normalize(0, 30)),))
im_ani = animation.ArtistAnimation(fig2, ims, interval=50, repeat_delay=3000, blit=True)
#im_ani.save('im.mp4', metadata={'artist':'Guido'})
LINK@KOREATECH
plt.show()
10