python matplotlib数据可视化_Python - matplotlib 数据可视化
2 ?圖和子圖的建立
2.1? 導入matplotlib
import matplotlib.pyplot as plt
import numpy as np
2.2??建立圖和子圖方式一
plt.plot( )會在最近的一個圖上進行繪制
from numpy.random import randn
fig = plt.figure(figsize = (8,4)) #設置圖的大小
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,1,2)
ax3.plot(randn(50).cumsum(),'k--') # plt.plot(randn(50).cumsum(),'k--')等效
ax1.hist(randn(100),bins = 10, color = 'b', alpha = 0.3) #bins 分成多少間隔 alpha 透明度
ax2.scatter(np.arange(30),np.arange(30) + 3*randn(30))
plt.show()
2.3?建立子圖方式二
from numpy.random import randn
fig, axes = plt.subplots(2,2) #以數組方式訪問
t = np.arange(0., 5., 0.2)
axes[0,0].plot(t, t, 'r-o', t, t**2, 'bs', t, t**3, 'g^') #同時繪制多條曲線
axes[1,1].plot(randn(40).cumsum(),'b--')
plt.show()
2.4?主題設置
使用style.use()函數
df_iris = pd.read_csv('../input/iris.csv')
plt.style.use('ggplot') #'fivethirtyeight','ggplot','dark_background','bmh'
df_iris.hist('sepal length')
plt.show()
3 ?顏色、標記、線型、刻度、標簽和圖例
from numpy.random import randn
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
ax1.plot(randn(30).cumsum(),color = 'b',linestyle = '--',marker = 'o',label = '$cumsum$') # 線型 可以直接'k--o'
ax1.set_xlim(10,25)
ax1.set_title('My first plot')
ax1.set_xlabel('Stages')
plt.legend(loc = 'best') #把圖放在不礙事的地方 xticks([])設置刻度
plt.show()
等價于下面的代碼:
from numpy.random import randn
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
ax1.plot(randn(30).cumsum(),color = 'b',linestyle = '--',marker = 'o',label = '$cumsum$') #圖標可以使用latex內嵌公式
plt.xlim(10,25) #plt.axis([10,25,0,10])對x,y軸范圍同時進行設置
plt.title('My first plot')
plt.xlabel('Stages')
plt.legend(loc = 'best')
plt.show()
4 ?pandas中的繪圖函數
在pandas中,我們具有行標簽,列標簽以及分組信息。這也就是說,要制作一張完整的圖表,原本需要一大堆的matplotlib代碼,現在只需一兩條簡潔的語句就可以了。
pandas有很多能夠利用DataFrame對象數據組織特點來創建標準圖表的高級繪圖方法。
4.1 ?線型圖
from numpy.random import randn
fig, axes = plt.subplots(1,2)
s = pd.Series(randn(10).cumsum(),index = np.arange(0,100,10))
s.plot(ax = axes[0]) # ax參數選擇子圖
df = pd.DataFrame(randn(10,3).cumsum(0),columns = ['A','B','C'],index = np.arange(0,100,10))
df.plot(ax = axes[1])
plt.show()
4.2 柱狀圖
from numpy.random import rand
fig, axes = plt.subplots(1,2)
data = pd.Series(rand(16),index = list('abcdefghijklmnop'))
data.plot(kind = 'bar', ax = axes[0], color = 'b', alpha = 0.7) #kind選擇圖表類型 'bar' 垂直柱狀圖
data.plot(kind = 'barh', ax = axes[1], color = 'b', alpha = 0.7) # 'barh' 水平柱狀圖
plt.show()
from numpy.random import rand
fig, axes = plt.subplots(1,2)
data = pd.DataFrame(rand(6,4),
index = ['one','two','three','four','five','six'],
columns = pd.Index(['A','B','C','D'], name = 'Genus'))
data.plot(kind = 'bar', ax = axes[0], alpha = 0.5)
data.plot(kind = 'bar', ax = axes[1], stacked = True, alpha = 0.5)
plt.show()
此外,柱狀圖有一個非常不錯的用法,利用value_counts( )圖形化顯示Series中各值的出現概率,比如s.value_counts( ).plot(kind = 'bar')。
4.3 直方圖和密度圖
from numpy.random import randn
fig, axes = plt.subplots(1,2)
data = pd.Series(randn(100))
data.hist(ax = axes[0], bins = 50) #直方圖
data.plot(kind = 'kde', ax = axes[1]) #密度圖
plt.show()
其實可以一次性制作多個直方圖,layout參數的意思是將兩個圖分成兩行一列,如果沒有這個參數,默認會將全部的圖放在同一行。
df_iris = pd.read_csv('../input/iris.csv')
columns = ['sepal length','sepal width','petal length','petal width']
df_iris.hist(column=columns, layout=(2,2))
plt.show()
4.4 箱型圖
箱型圖是基于五數概括法(最小值,第一個四分位數,第一個四分位數(中位數),第三個四分位數,最大值)的數據的一個圖形匯總,還需要用到四分位數間距IQR = 第三個四分位數 - 第一個四分位數。
df_iris = pd.read_csv('../input/iris.csv') #['sepal length','sepal width','petal length','petal width','class']
sample_size = df_iris[['petal width','class']]
sample_size.boxplot(by='class')
plt.xticks(rotation=90) #將X軸的坐標文字旋轉90度,垂直顯示
plt.show()
5 ?參考資料鏈接
總結
以上是生活随笔為你收集整理的python matplotlib数据可视化_Python - matplotlib 数据可视化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue 找回密码_vue实现个人信息查看
- 下一篇: 生产环境可以用吗_柑橘可以生产果醋吗?柑