Matplotlib

折線圖

In [11]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

plt.plot([1, 4, 3, 1, 5])
plt.show()
In [15]:
x = np.linspace(0, 10, 101)
y = x**2-6*x-3
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
Out[15]:
Text(0,0.5,'y')
In [18]:
x = np.linspace(0, 10, 21)
plt.plot(x, x, 'r^')
plt.plot(x, x**2, '--')
plt.plot(x, x**3, 'go')
Out[18]:
[<matplotlib.lines.Line2D at 0x10d692e48>]
In [20]:
plt.plot?

長條圖

In [22]:
x = np.arange(10)
y = np.array([5, 9, 4, 8, 7, 4, 3, 1, 2, 9])
plt.bar(x, y)
Out[22]:
<BarContainer object of 10 artists>

散佈圖

In [25]:
x = np.random.rand(30)
y = np.random.rand(30)
plt.scatter(x, y)
Out[25]:
<matplotlib.collections.PathCollection at 0x10dc35518>

多張圖

In [30]:
x = np.linspace(0, 5, 21)
plt.figure()

plt.subplot(231)
plt.plot(x, x)

plt.subplot(232)
plt.plot(x, x**2)

plt.subplot(233)
plt.plot(x, x**3)

plt.subplot(234)
plt.plot(x, x**0.5)

plt.subplot(235)
plt.plot(x, x**0.25)

plt.subplot(236)
plt.plot(x, x*2)
Out[30]:
[<matplotlib.lines.Line2D at 0x11096f320>]
In [ ]: