2014.04.28 15:57

pylab - Plotting with Matplotlib

조회 수 8581 댓글 0


Prev이전 문서

Next다음 문서

+ - Up Down Comment Print


Prev이전 문서

Next다음 문서

+ - Up Down Comment Print

이곳 Doc를 참조할 것

%pylab makes the following imports:

import numpy
import matplotlib
from matplotlib import pylab, mlab, pyplot
np = numpy
plt = pyplot

from IPython.display import display
from IPython.core.pylabtools import figsize, getfigs

from pylab import *
from numpy import *

Plotting with Matplotlib

IPython works with the Matplotlib plotting library, which integrates Matplotlib with IPython's display system and event loop handling.

matplotlib mode

To make plots using Matplotlib, you must first enable IPython's matplotlib mode.

To do this, run the %matplotlib magic command to enable plotting in the current Notebook.

This magic takes an optional argument that specifies which Matplotlib backend should be used. Most of the time, in the Notebook, you will want to use the inline backend, which will embed plots inside the Notebook:

In [1]:
%matplotlib inline

You can also use Matplotlib GUI backends in the Notebook, such as the Qt backend (%matplotlib qt). This will use Matplotlib's interactive Qt UI in a floating window to the side of your browser. Of course, this only works if your browser is running on the same system as the Notebook Server. You can always call the display function to paste figures into the Notebook document.

Making a simple plot

With matplotlib enabled, plotting should just work.

In [2]:
import matplotlib.pyplot as plt
import numpy as np
In [3]:
x = np.linspace(0, 3*np.pi, 500)
plt.plot(x, np.sin(x**2))
plt.title('A simple chirp');

These images can be resized by dragging the handle in the lower right corner. Double clicking will return them to their original size.

One thing to be aware of is that by default, the Figure object is cleared at the end of each cell, so you will need to issue all plotting commands for a single figure in a single cell.

Loading Matplotlib demos with %load

IPython's %load magic can be used to load any Matplotlib demo by its URL:

In [4]:
In [5]:
Plot demonstrating the integral as the area under a curve.

Although this is a simple example, it demonstrates some important tweaks:

    * A simple line plot with custom color and line width.
    * A shaded region created using a Polygon patch.
    * A text label with mathtext rendering.
    * figtext calls to label the x- and y-axes.
    * Use of axis spines to hide the top and right spines.
    * Custom tick placement and labels.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

def func(x):
    return (x - 3) * (x - 5) * (x - 7) + 85

a, b = 2, 9 # integral limits
x = np.linspace(0, 10)
y = func(x)

fig, ax = plt.subplots()
plt.plot(x, y, 'r', linewidth=2)

# Make the shaded region
ix = np.linspace(a, b)
iy = func(ix)
verts = [(a, 0)] + list(zip(ix, iy)) + [(b, 0)]
poly = Polygon(verts, facecolor='0.9', edgecolor='0.5')

plt.text(0.5 * (a + b), 30, r"$\int_a^b f(x)\mathrm{d}x$",
         horizontalalignment='center', fontsize=20)

plt.figtext(0.9, 0.05, '$x$')
plt.figtext(0.1, 0.9, '$y$')


ax.set_xticks((a, b))
ax.set_xticklabels(('$a$', '$b$'))

Dreamy의 코드 스크랩

내가 모으고 내가 보는

List of Articles
번호 분류 제목 날짜 조회 수 추천 수
35 Python py2exe를 이용한 실행파일 만들기 2009.04.10 49213 0
34 Python Python Embedding (MFC프로그램에서 파이썬 실행하기) 2009.11.14 45258 0
33 Python python을 이용한 프로그램 개발 및 배포 방안 2010.02.25 44453 0
32 Python C#에서 Python 파일 실행하고 결과 가져오기 2012.02.02 34561 0
31 Python Visual Studio 2005 + IronPython 연동하기 2012.02.02 15251 0
30 Python BeautifulSoup으로 웹에 있는 데이터 긁어오기 2013.04.08 72007 0
29 Python BeautifulSoup로 HTML 파싱 끝내기 2013.04.08 39070 0
28 Python python 문법요약 2013.04.08 26906 0
27 Python Python 문자열 관련 함수 2013.05.07 21037 0
26 Python paramiko로 SSH 접속하기 2014.04.22 18701 0
25 Python distutils 개요 2014.04.24 8082 0
24 Python wxPython Tutorial 링크 2014.04.24 9143 0
23 Python Lambda 함수, 축약 함수 2014.04.25 11084 0
» Python pylab - Plotting with Matplotlib 2014.04.28 8581 0
21 Python map() 함수 2014.04.30 8670 0
Board Pagination ‹ Prev 1 2 3 Next ›
/ 3

나눔글꼴 설치 안내

이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소

Designed by / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5