In [1]:
%matplotlib inline
import pylab
import matplotlib.pyplot as plt
import seaborn

In [2]:
x = pylab.linspace(0, 5, 10)
y = x ** 2

以下是 Matlab 风格的画图


In [3]:
pylab.figure()
pylab.plot(x, y, 'r')
pylab.xlabel('X')
pylab.ylabel('Y')
pylab.title('Title')
pylab.show()



In [4]:
pylab.subplot(1, 2, 1)
pylab.plot(x, y, 'r--')
pylab.subplot(1, 2, 2)
pylab.plot(y, x, 'p--')


Out[4]:
[<matplotlib.lines.Line2D at 0x7fbb6ff86390>]

matplotlib 面向对象风格 API


In [5]:
fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])
axes.plot(x, y, 'r+-')
axes.set_xlabel('x')
axes.set_ylabel('y')
axes.set_title('title')


Out[5]:
<matplotlib.text.Text at 0x7fbb6ff692d0>

In [6]:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(20, 10))
ax = fig.add_subplot(111, projection='3d')
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
    xs = np.arange(20)
    ys = np.random.rand(20)

    # You can provide either a single color or an array. To demonstrate this,
    # the first bar of each set will be colored cyan.
    cs = [c] * len(xs)
    cs[0] = 'c'
    ax.bar(xs, ys, zs=z, zdir='y', color=cs, alpha=0.8)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()