matplotlib 繪圖

大體步驟是:dom

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 5, 10)
y = x**2

# 首先,須要一個figure對象 fig
= plt.figure()
# 而後,axes對象,在axes上面繪圖 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')

若是須要在一張圖中繪多個軸:spa

fig, axes = plt.subplots(nrows=1, ncols=2)

for ax in axes:
    ax.plot(x, y, 'r')
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_title('title')

一些例子:3d

n = np.array([0,1,2,3,4,5])
x = np.linspace(-0.75, 1., 100)

fig, axes = plt.subplots(1, 4, figsize=(12,3))

axes[0].scatter(x, x + 0.25*np.random.randn(len(x)))

axes[1].step(n, n**2, lw=2)

axes[2].bar(n, n**2, align="center", width=0.5, alpha=0.5)

axes[3].fill_between(x, x**2, x**3, color="green", alpha=0.5);

散點圖,改變顏色,大小:code

 
 

import numpy as np
import matplotlib.pyplot as plt對象

 
 

N = 50
x = np.random.rand(N)
y = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2 # 0 to 15 point radiuses
color = 2 * np.pi * np.random.rand(N)
plt.scatter(x, y, s=area, c=color, alpha=0.5, cmap=plt.cm.hsv)
plt.show()blog

 直方圖:it

x = np.random.randn(1000)
num_bins = 40
plt.hist(x, num_bins, facecolor='green', alpha=0.5)

極座標:class

fig = plt.figure()
ax = fig.add_axes([0.0, 0.0, .6, .6], polar=True)
t = linspace(0, 2 * pi, 100)
ax.plot(t, t, color='blue', lw=3);

相關文章
相關標籤/搜索