標籤:orm nbsp line span 符號 net and ext 預設
測試環境:
Jupyter QtConsole 4.2.1
Python 3.6.1
1. 基本畫線:
以下得出紅藍綠三色的點
import numpy as np
import matplotlib.pyplot as plt
# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)
# red dashes, blue squares and green triangles
plt.plot(t, t, ‘r--‘, t, t**2, ‘bs‘, t, t**3, ‘g^‘)
plt.show()
以下設定線寬,得到比較粗一點兒的線,如果 plot中只給了一維資訊,
預設圖形是把數值匹配成縱座標的
x = np.arange(0., 5., 0.1)
plt.plot(x, 4*x, linewidth=8.0)
plt.show()
以下得到同一個圖中兩幅分圖:
import numpy as np
import matplotlib.pyplot as plt
def f(t):
return np.exp(-t) * np.cos(2*np.pi*t)
t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)
plt.figure(1)
plt.subplot(211)#表示兩幅圖豎著排列
plt.plot(t1, f(t1), ‘bo‘, t2, f(t2), ‘k‘)
plt.subplot(212)#如果為(221)和(222)表示橫排列
plt.plot(t2, np.cos(2*np.pi*t2), ‘r--‘)
plt.show()
2. 畫長條圖
以下為常態分佈
np.random.seed(19680801)
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
# the histogram of the data
n, bins, patches = plt.hist(x, 50, normed=1, facecolor=‘g‘, alpha=0.1)
plt.xlabel(‘Smarts‘)
plt.ylabel(‘Probability‘)
plt.title(‘Histogram of IQ‘)
plt.text(60, .025, r‘$\mu=100,\ \sigma=15$‘)
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()
其中
n, bins, patches = plt.hist(arr, bins=10, normed=0, facecolor=‘black‘, edgecolor=‘black‘,alpha=1,histtype=‘bar‘)
hist的參數非常多,但常用的就這六個,只有第一個是必須的,後面四個可選
arr: 需要計算長條圖的一維數組
bins: 長條圖的柱數,可選項,預設為10
normed: 是否將得到的長條圖向量歸一化。預設為0
facecolor: 長條圖顏色
edgecolor: 長條圖邊框顏色
alpha: 透明度
histtype: 長條圖類型,‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’
傳回值 :
n: 長條圖向量,是否歸一化由參數normed設定
bins: 返回各個bin的區間範圍
patches: 返回每個bin裡麵包含的資料,是一個list
3. 特殊符號和標註
用以下方式可以寫出特殊的數學公式符號:
plt.title(r‘$\sigma_i=15$‘)
以下代碼錶示文字顯示地區是(3, 1.5)指向座標位置是(2,1)
import numpy as npimport matplotlib.pyplot as pltax = plt.subplot(111)t = np.arange(0.0, 5.0, 0.01)s = np.cos(2*np.pi*t)line, = plt.plot(t, s, lw=2)plt.annotate(‘local max‘, xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor=‘black‘, shrink=0.05), )#shrink表示箭頭縮放情況,值越小顯示越大
plt.ylim(-2,2)#表示y座標軸的上界和下界 plt.show()
更多資訊:
http://blog.csdn.net/panda1234lee/article/details/52311593
Python matplotlib繪圖學習筆記