Python數據處理從零開始----第四章(可視化)(4)目錄正文

正文

繪製連續誤差圖

有時候需要展示連續變量的誤差,matplotlib通過plt.plot和plt.fill_between來實現。下面通過Scikit-Learn程序庫的API裏面的高斯過程迴歸方法來演示。這是用一種非常靈活的非參數方程對帶有不確定性的連續測量變量進行擬合的方法。

# -*- coding: utf-8 -*-
"""
Created on Tue Oct 30 18:46:30 2018

@author: Administrator
"""
%reset -f
%clear
# In[*]
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
import numpy as np



from sklearn.gaussian_process import GaussianProcess
# In[*]
# define the model and draw some data
model = lambda x: x * np.sin(x)
xdata = np.array([1, 3, 5, 6, 8])
ydata = model(xdata)

# Compute the Gaussian process fit
gp = GaussianProcess(corr='cubic', theta0=1e-2, thetaL=1e-4, thetaU=1E-1,
                     random_start=100)
gp.fit(xdata[:, np.newaxis], ydata)

xfit = np.linspace(0, 10, 1000)
yfit, MSE = gp.predict(xfit[:, np.newaxis], eval_MSE=True)
dyfit = 2 * np.sqrt(MSE)  # 2*sigma ~ 95% confidence region

我們現在有xfit,yfit和dyfit,它們可以對我們的數據進行連續擬合。 我們可以將這些傳遞給plt.errorbar函數,如上所述,但我們真的不想用1000個錯誤條繪製1,000個點。 相反,我們可以使用淺色的plt.fill_between函數來顯示這個連續錯誤:

# In[*]


# Visualize the result
plt.plot(xdata, ydata, 'or')
plt.plot(xfit, yfit, '-', color='gray')

plt.fill_between(xfit, yfit - dyfit, yfit + dyfit,
                 color='gray', alpha=0.2)
plt.xlim(0, 10);

注意我們在這裏用fill_between函數完成的工作:傳遞一個x值,然後是Y軸下邊界,然後是Y軸上邊界,結果是這些區域之間的區域被填充。

直方圖,數據區間和密度

一般直方圖來用於顯示數據的分佈

# -*- coding: utf-8 -*-
"""
Created on Tue Oct 30 18:46:30 2018

@author: Administrator
"""
%reset -f
%clear
# In[*]
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')

data = np.random.randn(1000)


plt.hist(data);
# In[*]

plt.hist(data, bins=30, normed=True, alpha=0.5,
         histtype='stepfilled', color='#E41A1C',
         edgecolor='none');

可以將histtype='stepfilled'與透明設置參數alpha搭配使用

 # In[*]        
         
x1 = np.random.normal(0, 0.8, 1000)
x2 = np.random.normal(-2, 1, 1000)
x3 = np.random.normal(3, 2, 1000)

kwargs = dict(histtype='stepfilled', alpha=0.8, normed=True, bins=40)

plt.hist(x1, **kwargs)
plt.hist(x2, **kwargs)
plt.hist(x3, **kwargs);

配置圖例

圖例賦予可視化意義,爲各種元素指定意義。 我們已經知道如何創建一個簡單的圖例; 在這裏,我們將介紹如何在Matplotlib中自定義圖例的位置和其他。可以使用plt.legend()命令創建最簡單的圖例,該命令會自動爲任何標記的繪圖元素創建圖例:

# -*- coding: utf-8 -*-
"""
Created on Tue Oct 30 18:46:30 2018

@author: Administrator
"""
%reset -f
%clear
# In[*]
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
%matplotlib inline
import numpy as np
x = np.linspace(0, 10, 1000)
fig, ax = plt.subplots()
ax.plot(x,np.sin(x), "#377EB8", label='Sine')
ax.plot(x,np.cos(x), "#E41A1C", label='Cosine')
ax.axis('equal')
leg = ax.legend();
# In[*]
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
%matplotlib inline
import numpy as np
x = np.linspace(0, 10, 1000)
fig, ax = plt.subplots()
ax.plot(x,np.sin(x), "#377EB8", label='Sine')
ax.plot(x,np.cos(x), "#E41A1C", label='Cosine')
ax.axis('equal')
leg = ax.legend(loc='upper left',frameon=False);

還可以定義圓角邊框(fancybox),增加陰影,改變外邊框透明度(framealpha值),或者改變文字間距

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章