點贊、關注再看,養成良好習慣
Life is short, U need Python
初學Python,快來點我吧
案例:蘋果公司股票價格時間序列的可視化分析
作者:PyQuant
博客:https://blog.csdn.net/qq_33499889
慕課:https://mooc1-2.chaoxing.com/course/207443619.html
聲明:案例參考博雅大數據學院案例集
本案例適合作爲大數據技術基礎課程中數據可視化部分的配套教學案例。通過本案例,能夠達到以下教學效果:
-
培養學生對真實數據進行可視化分析的能力。
- 案例中數據來源於蘋果公司2015-2019年的股票數據,進行股票數據漲跌的可視化分析。
-
幫助學生進一步掌握常用圖表的繪製方法。
- 案例中涉及到折線圖、散點圖、柱狀圖、直方圖、核密度圖和小提琴圖等。
-
提高學生動手實踐能力。
- 案例中使用Python中的兩個常用可視化工具Matplotlib和Seaborn,提高學生繪製常用圖表的實踐能力。
可視化分析在大數據分析中扮演着相當重要的角色。可視化可以使數據更爲清晰地傳遞信息,使數據分析更爲生動具體。Matplotlib是一個Python庫,用於2D繪圖,而Seaborn是Python基於Matplotlib的數據可視化的庫。Seaborn在Matplotlib的基礎上進行了更高級的封裝,從而使得作圖更加簡單方便,可以作出信息豐富且美觀的圖。本案例旨在幫助大家快速熟悉Matplotlib和Seaborn這兩個可視化工具的操作。
目錄
- 數據載入及預處理
- Matplotlib繪圖
- 面向對象API繪圖
- Seaborn繪圖
1. 數據載入及預處理
本案例選擇蘋果公司的股票作爲分析對象,來進行股票漲跌的分析;數據集來源:Python代碼調取雅虎金融數據端口;其中數據集中每一列數據對應的含義如下表所示:
列名 | 類型 | 說明 |
---|---|---|
Date | Object | 觀測日期 |
Open | Float | 開盤價格 |
High | Float | 最高成交價格 |
Low | Float | 最低成交價格 |
Close | Float | 收盤價格 |
Volume | Int | 交易量 |
Adj Close | Float | 調整後的收盤價格 |
- 導入數據集
import pandas as pd
import numpy as np
import datetime
import pandas_datareader.data as pdr # 事先安裝:pip install pandas-datareader
start = datetime.date(2015,1,2)
end = datetime.date(2019,12,31)
apple = pdr.get_data_yahoo('AAPL',start,end)
apple.head()
High | Low | Open | Close | Volume | Adj Close | |
---|---|---|---|---|---|---|
Date | ||||||
2015-01-02 | 111.440002 | 107.349998 | 111.389999 | 109.330002 | 53204600.0 | 100.216454 |
2015-01-05 | 108.650002 | 105.410004 | 108.290001 | 106.250000 | 64285500.0 | 97.393181 |
2015-01-06 | 107.430000 | 104.629997 | 106.540001 | 106.260002 | 65797100.0 | 97.402374 |
2015-01-07 | 108.199997 | 106.699997 | 107.199997 | 107.750000 | 40105900.0 | 98.768150 |
2015-01-08 | 112.150002 | 108.699997 | 109.230003 | 111.889999 | 59364500.0 | 102.563072 |
apple.tail()
High | Low | Open | Close | Volume | Adj Close | |
---|---|---|---|---|---|---|
Date | ||||||
2019-12-24 | 284.890015 | 282.920013 | 284.690002 | 284.269989 | 12119700.0 | 283.596924 |
2019-12-26 | 289.980011 | 284.700012 | 284.820007 | 289.910004 | 23280300.0 | 289.223602 |
2019-12-27 | 293.970001 | 288.119995 | 291.119995 | 289.799988 | 36566500.0 | 289.113831 |
2019-12-30 | 292.690002 | 285.220001 | 289.459991 | 291.519989 | 36028600.0 | 290.829773 |
2019-12-31 | 293.679993 | 289.519989 | 289.929993 | 293.649994 | 25201400.0 | 292.954712 |
apple_index = apple.index
mon = []
year = []
for item in apple_index:
item = str(item)
mon.append(int(item[5:7]))
year.append(int(item[0:4]))
apple['month'] = mon
apple['year'] = year
apple.head()
High | Low | Open | Close | Volume | Adj Close | month | year | |
---|---|---|---|---|---|---|---|---|
Date | ||||||||
2015-01-02 | 111.440002 | 107.349998 | 111.389999 | 109.330002 | 53204600.0 | 100.216454 | 1 | 2015 |
2015-01-05 | 108.650002 | 105.410004 | 108.290001 | 106.250000 | 64285500.0 | 97.393181 | 1 | 2015 |
2015-01-06 | 107.430000 | 104.629997 | 106.540001 | 106.260002 | 65797100.0 | 97.402374 | 1 | 2015 |
2015-01-07 | 108.199997 | 106.699997 | 107.199997 | 107.750000 | 40105900.0 | 98.768150 | 1 | 2015 |
2015-01-08 | 112.150002 | 108.699997 | 109.230003 | 111.889999 | 59364500.0 | 102.563072 | 1 | 2015 |
2. Matplotlib繪圖
2.1 折線圖
折線圖 用於繪製連續型數據,展示數據變化的趨勢。
- 將數據以年爲單位進行分組,以每年收盤價格的平均值繪圖
%matplotlib inline
import matplotlib.pyplot as plt
x = apple.groupby('year').Close.mean().index
y = apple.groupby('year').Close.mean().values
plt.plot(x,y)
plt.xticks(x)
plt.show()
分析:從圖中我們可以看出,蘋果公司2015-2019年的股票收盤價格變化趨勢,總體趨勢走高,但是我們注意到2015年,出現下降趨勢,但2016股票行情回暖並大幅度走高,且一直持續到2019年底。
備註:plot()
函數—x
爲橫座標數據,y
爲縱座標數據。除此之外,Matplotlib還提供了豐富的線形及其顏色,還可以調節線的寬度(linewidth
),數據點的大小(markersize
)及設置座標軸的名字等。同時,可以利用grid()
函數繪製網格線;默認爲False
,利用legend()
函數設置圖例,使用其loc
參數設置圖例的位置等。另外,Matplotlib允許多條折線繪製在一張圖中。
- 美化 每年收盤價格的平均值曲線圖
plt.plot(x,y,label='Close Price')
plt.grid(True)
plt.xticks(x)
plt.legend(loc = 'upper left')
plt.title('Average Close Price in 2015-2019')
plt.xlabel('Year')
plt.ylabel('Average Close Price')
plt.show()
- 繪製2015-2019年期間年成交量平均值的折線圖
x1 = apple.groupby('year').Volume.mean().index
y1 = apple.groupby('year').Volume.mean().values
plt.plot(x1,y1,color='red',marker='o',linestyle='dashed',label='Close Price',linewidth=2, markersize=12)
# plt.plot(x1, y1, 'r--',label='Close Price', linewidth=2, markersize=12)
plt.xticks(x)
plt.legend(loc = 'upper right')
plt.title('Average volume in 2015-2019')
plt.xlabel('Year')
plt.ylabel('Average volume')
plt.grid(True)
plt.show()
分析:此圖反映了2015-2019年間,蘋果公司股票交易量的變化趨勢,我們可以看出其交易量整體呈下降趨勢,雖2017年開始有所轉變,但2018年開始再次呈現下降趨勢。結合上一個圖,我們看出,雖然股票的價格升高,但是股票的成交量降低,所以由這兩個圖不能很明確的體現股票真正價值的變化,其股票價格的升高,可能是由於貨幣價值的變化,通貨膨脹等,具體原因需要結合更多的數據集進行分析。
- 將數據以年爲單位進行分組,把股票最低價格的平均值和最高價格的平均值繪製於同一個畫布上
x1 = apple.groupby('year').Low.mean().index
y1 = apple.groupby('year').Low.mean().values
y2 = apple.groupby('year').High.mean().values
plt.plot(x1,y1, 'go-', label='High', linewidth=2)
plt.plot(x1,y2, 'rs--', label='Low',linewidth=2)
plt.xticks(x)
plt.title('Average Price in 2015-2019')
plt.xlabel('Year')
plt.ylabel('Average Price')
plt.legend(('High', 'Low'),loc='upper left')
plt.grid(True)
plt.show()
分析:這裏我們注意到,最高價格與最低價格之間的差距,2015年、2017年和2018年較大,這裏也顯示了股票市場的不確定性。
2.2 散點圖
散點圖是數據點在直角座標系平面上的分佈圖,散點圖表示因變量隨自變量而變化的大致趨勢,常用於迴歸分析中。
- 繪製2015-2019年隨股票收盤價格的變化其成交量的散點圖
x = apple.Close
y = apple.Volume
plt.scatter(x,y)
#or plt.plot(x,y,'o')
plt.title('Close Price and Volume in 2015-2019')
plt.xlabel('Close Price')
plt.ylabel('Volume')
plt.show()
分析:此圖展示了2015-2019年隨着股票收盤價格的變化,股票成交量的分佈和聚合情況。
- 通過每個點的顏色來展示不同月份的收盤價格與成交量的分佈,使用更多的參數對此散點圖進行美化
例如,可以設定參數alpha
改變數據點的透明度,設定參數marker
改變數據點的形狀,設定參數c
爲month
設置不同分類的顏色,並使用colorbar()
繪製色階欄。
plt.scatter(x,y,marker = "^",c = apple['month'],alpha = 0.8)
plt.grid(True)
plt.colorbar()
plt.title('Close Price and Volume in 2015-2019')
plt.xlabel('Close Price')
plt.ylabel('Volume')
plt.show()
2.3 柱狀圖
柱狀圖是一種用矩形條表示分類數據的圖表,這些矩形條的高度或長度與它們所表示的值成比例。柱狀圖可以垂直繪製,也可以水平繪製。
- 選擇2015年的數據,進行繪製每月股票收盤價格平均值的柱狀圖
a_2015 = apple.loc[apple['year'] == 2015]
x = a_2015.groupby('month').Close.mean().index
y = a_2015.groupby('month').Close.mean().values
plt.bar(x,y,color = 'SkyBlue',hatch='o')
plt.grid(True)
plt.xlabel('Month')
plt.ylabel('Close Price')
plt.title('Bar Chart of Close Price in 2015')
plt.show()
分析:此柱狀圖表示2015年12個月收盤價格平均值的情況,從8月起,股票收盤價格開始較大幅下降。
備註:bar()
函數繪製柱狀圖,主要的參數x
爲x軸的位置序列;y
爲柱形圖的高度。參數hatch
可以設置填充圖形。
- 繪製2015年每月股票收盤價格和開盤價格平均值的橫向柱狀圖
x = a_2015.groupby('month').Close.mean().index
y1 = a_2015.groupby('month').Close.mean().values
y2 = a_2015.groupby('month').Open.mean().values
width = 0.8
plt.barh(x-width/2, y1,width/2,label='Close Price')
plt.barh(x, y2,width/2,label='Open Price')
plt.xlabel('Price')
plt.ylabel('Month')
plt.legend(loc='upper center', bbox_to_anchor=(1.2,1))
plt.title('Close Price and Open Price in 2015')
plt.show()
備註:利用legend()
函數,設置其參數使圖例置於表格外以免遮擋圖形。
2.4 直方圖
直方圖又稱質量分佈圖,由一系列高度不等的縱向條紋或線段表示數據分佈的情況。
- 使用2015年股票成交量的數據繪製直方圖
x = a_2015.Volume
plt.hist(x,bins = 20,density=0,facecolor='green', edgecolor='black',alpha=1, histtype='bar')
plt.xlabel('Volume')
plt.ylabel('Probability')
plt.title('Histogram of Volume in 2015',fontsize = 16)
plt.show()
分析:此圖展示了2015年股票交易量的分佈直方圖。直方圖是用面積表示各組頻數的多少,矩形的高度表示每一組的頻數或頻率,寬度則表示各組的組距,因此其高度與寬度均有意義。我們看到柱狀圖與直方圖的形狀很相似,但是實際上的含義及用處並不一樣,直方圖主要表示頻率分佈,其x軸爲定量數據,而柱狀圖展示的是大小的比較,其x軸變量是分類數據。
備註:hist()
有很多參數,其中6個比較常用。x
爲輸入數據是必填,其他可選,包括bins
:表示直方圖的柱數,默認爲10;density
:表示是否將得到的直方圖向量歸一化,默認爲0;facecolor
: 表示直方圖顏色;edgecolor
: 表示直方圖邊框顏色;alpha
: 表示透明度;histtype
: 表示直方圖類型(bar
, barstacked
,step
,stepfilled
)。
2.5 箱線圖
箱線圖又稱爲盒須圖、盒式圖或箱式圖,是一種用於顯示一組數據分散情況資料的統計圖,它能顯示出一組數據的最大非異常值、最小非異常值、中位數、及上下四分位數。
- 繪製2015年蘋果股票開盤,收盤,最高及最低價格的分佈
data = a_2015.groupby('month')['Open','Close','High','Low'].mean()
plt.grid(True)
plt.boxplot(x = data.values, labels = data.columns)
plt.title('Box Plot of Volume in 2015',fontsize = 16)
plt.show()
分析:根據“箱子”的含義,我們可以輕鬆看到每類價格的中位數及分佈情況。箱形圖不受異常值的影響,常用於識別異常值。箱式圖經常用於可視化分析中,包含很多信息。
3. 面向對象API繪圖
以上我們介紹的方法是函數方法做圖,即使用基本的Matplotlib命令,接下來我們介紹另一種方法,即面向對象API的方法,此法爲創建圖的最佳方式。
3.1 創建子圖
Matplotlib可以通過add_subplot()
新增子圖,指定figsize
和dpi
參數來指定圖形大小,縱橫比和DPI來創建圖。
- 在同一個畫布上均分成2*1的畫布進行繪圖。
x = a_2015.groupby('month').Close.mean().index
y1 = a_2015.groupby('month').Close.mean().values
y2 = a_2015.groupby('month').Volume.mean().values
fig = plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax1.plot(x, y1)
ax2 = fig.add_subplot(2,1,2)
ax2.plot(x, y2)
ax2.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)
plt.show()
備註:figure()
:創建一個空白畫布,此函數有3個常用參數,依次爲num
:表示此圖形的id
,可爲整型或者字符型;figsize
:表示以英寸爲單位的寬高; dpi
:表示圖形分辨率,相同的figsize
,dpi
越大則圖形越大;facecolor
:表示背景色;edgecolor
:邊框顏色;frameon
:默認值True
爲繪製邊框,如果爲False
則不繪製邊框。其中,tight_layout()
函數的目的是避免圖表之間的重疊。
- 對上圖進行添加座標軸名稱等信息
fig = plt.figure(num = "fig1",figsize=(8,6),dpi=60,facecolor="pink",edgecolor='green',frameon=True)
ax1 = fig.add_subplot(2,1,1)
ax1.plot(x, y1)
ax1.set_xlabel('Month')
ax1.set_ylabel('Close Price')
ax1.set_title('Average Close Price in 2015')
ax2 = fig.add_subplot(2,1,2)
ax2.plot(x, y2)
ax2.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)
ax2.set_xlabel('Month')
ax2.set_ylabel('Volume')
ax2.set_title('Average Volume in 2015')
plt.tight_layout()
備註:通過使用set_xlabel()
,set_ylabel()
及set_title()
設置圖表的信息。
3.2 創建子區域
add_axes()
函數可以爲圖表新增子區域,該區域可以座落在figure
內任意位置,且該區域可任意設置大小
x = a_2015.groupby('month').Close.mean().index
y1 = a_2015.groupby('month').Close.mean().values
y2 = a_2015.groupby('month').Volume.mean().values
fig = plt.figure()
left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
ax1 = fig.add_axes([left, bottom, width, height])
ax1.plot(x, y2, 'r')
ax1.set_title('Average Volume in 2015',fontsize = 15)
ax1.set_xlabel('Month')
left, bottom, width, height = 0.25, 0.6, 0.25, 0.25
ax2 = fig.add_axes([left, bottom, width, height])
ax2.plot(x,y1, 'b')
ax2.set_title('Close Price')
plt.show()
備註:Axes表示一個圖表,通過add_axes()
函數設置子區域的相對位置。
- 利用面向對象API的方法,繪製交易量和收盤價格的線圖
import numpy as np
x = apple.groupby('year').Volume.mean().index
y1 = apple.groupby('year').Close.mean().values
y2 = apple.groupby('year').Volume.mean().values
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(x, y1,'r',label="Close Price")
ax1.legend(loc=1)
ax1.set_xlabel('Year')
ax1.set_ylabel('Y values for Close Price')
ax2 = ax1.twinx()
ax2.plot(x, y2, 'g',label = "Volume")
ax2.legend(loc=2)
ax2.set_ylabel('Y values for Volume')
fig.suptitle('Close Price and Volume', fontsize=16)
plt.xticks(x)
plt.grid(True)
plt.show()
備註:此圖爲共享y軸的例子,共享表示的就是x軸使用同一刻度線,twinx()
函數表示共享x軸,twiny()
表示共享y軸。
4. Seaborn繪圖
Seaborn提供了很多高層封裝的函數,幫助數據分析人員快速繪製美觀的數據圖形。
- 繪製2015年成交量的核密度估計
import seaborn as sns
sns.set()
sns.kdeplot(a_2015['Volume'],color="g",shade=True).set_title('KED of Volume in 2015',fontsize = 12)
plt.show()
分析:核密度圖可以理解爲直方圖的平滑曲線,是對真實的概率分佈曲線進行的模擬。用於觀察數據的分佈。
備註1:導入Seaborn包後,繪圖風格自動變爲Seaborn風格。相較於Matplotlib代碼簡潔很多。
備註2:set()
函數表示使用Seaborn的默認設置參數。shade
設置爲True
時,kde曲線下面的區域會進行陰影處理,默認爲False
;color
控制曲線及陰影的顏色
- 繪製多維數據的核密度圖
例如:開盤價格,收盤價格與成交量的關係。
plt.figure(figsize=(6,6),dpi=100)
plt.subplot(2, 1, 1)
sns.kdeplot(a_2015["Close"], a_2015["Volume"],color = 'r',shade=True,cbar=True)
plt.title('KED of Close Price and Volume in 2015',fontsize = 12)
plt.subplot(2, 2, 3)
sns.kdeplot(a_2015["Open"], a_2015["Volume"],color = 'r')
plt.title("Volume", fontsize = 10)
plt.subplot(2, 2, 4)
sns.kdeplot(a_2015["Close"], a_2015["Volume"],color = 'r')
plt.title("Close Price",fontsize = 10)
plt.tight_layout()
plt.show()
備註:shade
設置陰影,cbar
設置色階欄。
4.2 直方圖
Seaborn可以用distplot()
函數直接把直方圖和核密度估計(KDE)一次性畫出來,並且KDE可以通過kde
參數進行設置,默認爲True
;通過bins
參數設置分段數量。
- 導入Seaborn包後,繪製2015年股票成交量的直方圖和核密度圖
x = a_2015.Volume
kde_kws = {"color": "g", "lw": 3, "label": "KDE"}
sns.distplot(x, kde = True,bins = 30,color = "r",kde_kws = kde_kws )
plt.title('Histogram for Volume', fontsize = 16)
plt.show()
備註:kde_kws
是一個字典,可以設置KDE相關的參數。
- 使用面向對象API的方法,繪製兩個子圖,分別表示2015年開盤價格和收盤價格的直方圖
x = a_2015.Open
y = a_2015.Close
fig, (ax0, ax1) = plt.subplots(2, 1)
fig.subplots_adjust(hspace=0.5) # 表格之間空間
ax0 = sns.distplot(x,ax=ax0,rug=True,color = 'green')
ax1 = sns.distplot(y,ax=ax1,rug=True,color = 'red')
fig.suptitle('Volume and Close Price in 2015', fontsize = 16)
plt.show()
分析:此圖展示了開盤與收盤價格的密度分佈,總體來說,其分佈基本類似。
備註:rug
設置是否生成觀測數值的小細條。
4.3 散點圖
Seaborn中可以使用scatterplot()
函數繪製散點圖。
- 爲了方便分析,我們新建一列
tend
,把2015年股票收盤價格小於2015年收盤價格平均值的日子設爲0,大於及等於2015年平均值的日子設爲1
ls = []
for item in a_2015.Close:
if float(item) < a_2015.Close.mean():
ls.append(0)
else :ls.append(1)
a_2015 = a_2015.assign(tend=ls)
a_2015.tail()
High | Low | Open | Close | Volume | Adj Close | month | year | tend | |
---|---|---|---|---|---|---|---|---|---|
Date | |||||||||
2015-12-24 | 109.000000 | 107.949997 | 109.000000 | 108.029999 | 13570400.0 | 100.712036 | 12 | 2015 | 0 |
2015-12-28 | 107.690002 | 106.180000 | 107.589996 | 106.820000 | 26704200.0 | 99.584007 | 12 | 2015 | 0 |
2015-12-29 | 109.430000 | 106.860001 | 106.959999 | 108.739998 | 30931200.0 | 101.373940 | 12 | 2015 | 0 |
2015-12-30 | 108.699997 | 107.180000 | 108.580002 | 107.320000 | 25213800.0 | 100.050133 | 12 | 2015 | 0 |
2015-12-31 | 107.029999 | 104.820000 | 107.010002 | 105.260002 | 40912300.0 | 98.129669 | 12 | 2015 | 0 |
- 繪製2015年收盤價格與成交量的散點圖
sns.scatterplot(x = 'Close', y = 'Volume', hue='month',data=a_2015)
plt.title('Close VS. Volume in 2015')
plt.show()
分析:此圖展示了2015年收盤價格與成交量之間的關係,由顏色我們可以看出,前半年的收盤股票價格普遍較高,此結果與我們之前的分析一致。
備註:參數hue
用於指定額外的分組特徵,其值爲分組數據
- 使用
lmplot()
函數可以得到兩個特徵之間的線性迴歸直線
lm = sns.lmplot(x = 'Close', y = 'Volume', data=a_2015)
lm.fig.suptitle("Close VS. Volume in 2015", fontsize=12)
plt.show()
4.4 聯合繪圖
Seaborn可以可視化兩個變量的雙變量分佈,joinplot()
函數可以顯示兩個變量之間的雙變量關係以及每個變量的單變量分佈。
- 繪製2015年收盤價格與開盤價格的聯合分佈圖
sns.jointplot(x = 'Close', y = 'Open', data=a_2015)
plt.show()
分析:此圖展示了2015年收盤價格與開盤價格之間的關係,我們可以知道呈明顯的正相關關係。jointplot()
函數將直方圖和散點圖同時繪製出來。
備註:通過參數kind
設置圖形的類型:scatter
,reg
,resid
,kde
,hex
。
- 爲圖像添加回歸擬合直線
sns.jointplot(x = 'Close', y = 'Open', data=a_2015,kind="reg")
plt.show()
- 繪製2015年收盤價格和開盤價格聯合核密度圖
sns.jointplot(x = 'Close', y = 'Open', data=a_2015,color = 'g',kind="kde")
plt.show()
- 繪製2015年收盤價格和開盤價格散點圖,然後添加聯合密度分佈圖
sns.jointplot(x = 'Close', y = 'Open', data=a_2015, color="k").plot_joint(sns.kdeplot, zorder=0, n_levels=6)
plt.show()
備註:plot_joint
函數可以將很多繪圖形式放在同一張圖表中。
4.5 點對圖
pairplot()
可以完成點對圖的繪製,多用於展示變量之間的相關性;對角線上的直方圖允許我們看到單個變量的分佈,而上下三角形上的散點圖顯示了兩個變量之間的關係。
- 繪製
open
,close
,high
,low
與volume
的點對圖
sns.pairplot(a_2015[['Open','Close','High','Low','Volume']],kind="reg", markers='+')
plt.show()
分析:由圖可知,open
,close
,high
和low
之間均呈正相關關係,與volume
相關性較不明顯。
備註:參數kind
設置爲reg
時,每個散點圖將繪製線性迴歸擬合模型圖像。
結論:由此可見,Seaborn提供的操作更爲簡潔,提供的圖像更加多樣及美觀。
4.6 小提琴圖
小提琴圖是箱線圖和核密度圖的結合,在Seaborn中,使用violinplot()
函數繪製。
- 繪製小提琴圖
fig,ax=plt.subplots(nrows=2,ncols=2,figsize=(15,15))
ax[0,0] = sns.violinplot(x='month', y = 'Close',data = a_2015,ax=ax[0,0])
ax[0,0].set_title("Violin chart")
ax[0,1] = sns.violinplot(x='month', y = 'Close',hue ='tend',data = a_2015,ax=ax[0,1])
ax[0,1].set_title("Violin Chart with hue")
ax[1,0] = sns.violinplot(x='month', y = 'Close',hue ='tend',split = True,data = a_2015,ax=ax[1,0])
ax[1,0].set_title("Violin Chart with hue side")
ax[1,0] = sns.violinplot(x='year', y = 'Close',data = apple,ax=ax[1,1])
ax[1,1].set_title("Violin Chart for Close Price in 2015-2018")
plt.show()
分析:此圖含有的信息量也有很多,圖一展示了2015年中12月,每個月的收盤價格的分佈情況;圖二展示了,當有特徵變量加入之後,收盤價格的分佈情況,圖三爲圖二的升級版,將特徵變量分佈在小提琴圖的兩側;圖四顯示了2013-2015年收盤價格的分佈情況。
備註:參數hue
用於指定額外的分組特徵,其值爲分組數據;如若想要將圖的分組分別繪製在箱式圖的兩邊, 使用split
參數設置。
補充:
Matplotlib和Seaborn可視化中不同參數可以得到不同的效果,更加美觀的繪圖方法參見 官網。
- Matplotlib官網:https://matplotlib.org/
- Seaborn官網:https://seaborn.pydata.org/
- Pandas官網:https://pandas.pydata.org/
- Plotly官網:https://plot.ly/
- 寫作不易,切勿白剽
- 博友們的點贊和關注就是對博主堅持寫作的最大鼓勵
- 持續更新,未完待續…