在許多實際問題中,常常要對給出的數據進行可視化,便於觀察。html
import matplotlib.pyplot as plt
import numpy as np
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()
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()
使用style.use()函數python
df_iris = pd.read_csv('../input/iris.csv') plt.style.use('ggplot') #'fivethirtyeight','ggplot','dark_background','bmh' df_iris.hist('sepal length') 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$') # 線型 能夠直接'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()
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()
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')。dom
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()
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()