Python × 資料分析

Matplotlib & Seaborn


Kristen Chan

Agenda


  • Basic
  • Plots
  • Supplement -- Seaborn

Review

Note

Note

In [1]:
import matplotlib.pyplot as plt
In [2]:
%matplotlib inline

讓圖畫在 Jupyter Notebook 中

Note

In [3]:
import numpy as np
import pandas as pd

Matplotlib


Basic

Basic


  • plt.xlabel( ) / plt.ylabel( ):X 軸標籤 / Y 軸標籤
  • plt.xlim( ) / plt.ylim( ):X 軸範圍 / Y 軸範圍
  • plt.title( ):圖標題
  • plt.grid( ):格線
  • plt.legend:圖例
  • plt.subplot:子圖
  • plt.show:將畫好的圖顯示出來
  • plt.savefig:另存圖檔

Note

Create Demo Data -- Temperature

In [4]:
Year = ['2014','2014','2014','2014','2014','2014','2014','2014','2014','2014','2014','2014',
        '2015','2015','2015','2015','2015','2015','2015','2015','2015','2015','2015','2015',
        '2016','2016','2016','2016','2016','2016','2016','2016','2016','2016','2016','2016']
Month = ['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC',
         'JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC',
         'JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC']
Season = ['Winter','Winter','Spring','Spring','Spring','Summer','Summer','Summer','Fall','Fall','Fall','Winter',
          'Winter','Winter','Spring','Spring','Spring','Summer','Summer','Summer','Fall','Fall','Fall','Winter',
          'Winter','Winter','Spring','Spring','Spring','Summer','Summer','Summer','Fall','Fall','Fall','Winter']
Taipei = [16.8,16.5,18.9,22.5,25.2,28.0,30.5,30.2,29.7,24.7,22.3,16.5,
          16.7,20.8,18.9,22.7,26.1,30.0,30.0,28.6,27.4,25.2,23.5,18.9,
          16.2,15.5,17.5,24.0,27.1,29.4,30.3,30.1,27.8,27.0,22.6,20.1]
Taichung = [16.4,17.0,19.8,24.1,25.8,28.6,30.1,28.9,29.2,25.6,23.1,17.1,
            17.1,18.1,20.7,24.4,26.9,29.7,29.2,28.0,27.8,26.2,24.3,19.6,
            16.8,16.4,18.3,24.9,27.6,28.8,29.4,28.9,27.9,27.5,23.4,20.4] 
Kaohsiung = [19.5,20.3,22.6,25.9,27.8,29.2,30.3,29.1,29.5,27.2,25.2,20.2,
             19.9,17.3,23.6,26.2,28.3,30.6,29.5,28.8,28.8,27.7,26.2,22.4,
             19.3,19.6,21.6,26.9,28.9,29.7,30.2,29.4,28.4,28.4,25.7,22.9]
Temperature_table = { "Year" : Year,
                      "Month" : Month,
                      "Season" : Season,
                      "Taipei" : Taipei,
                      "Taichung" : Taichung,
                      "Kaohsiung" : Kaohsiung }
Temperature = pd.DataFrame(Temperature_table)
print(Temperature.shape)
Temperature.head()
(36, 6)
Out[4]:
Kaohsiung Month Season Taichung Taipei Year
0 19.5 JAN Winter 16.4 16.8 2014
1 20.3 FEB Winter 17.0 16.5 2014
2 22.6 MAR Spring 19.8 18.9 2014
3 25.9 APR Spring 24.1 22.5 2014
4 27.8 MAY Spring 25.8 25.2 2014

Note

Create Demo Data -- Taipei_Season

In [5]:
Taipei_Season = Temperature['Taipei'].groupby(Temperature['Season']).mean()
Taipei_Season
Out[5]:
Season
Fall      25.577778
Spring    22.544444
Summer    29.677778
Winter    17.555556
Name: Taipei, dtype: float64

Matplotlib


Plots

Plots


  • scatter( ):Scatter plot 散佈圖
  • plot( ):Line plot 線圖
  • hist( ):Histogram 直方圖
  • bar( ) / barh:Bar plot 長條圖

Plots


Scatter plot

想知道 Temperature 資料中 台北市 及 台中市 溫度的關係

Note

In [6]:
Temperature
Out[6]:
Kaohsiung Month Season Taichung Taipei Year
0 19.5 JAN Winter 16.4 16.8 2014
1 20.3 FEB Winter 17.0 16.5 2014
2 22.6 MAR Spring 19.8 18.9 2014
3 25.9 APR Spring 24.1 22.5 2014
4 27.8 MAY Spring 25.8 25.2 2014
5 29.2 JUN Summer 28.6 28.0 2014
6 30.3 JUL Summer 30.1 30.5 2014
7 29.1 AUG Summer 28.9 30.2 2014
8 29.5 SEP Fall 29.2 29.7 2014
9 27.2 OCT Fall 25.6 24.7 2014
10 25.2 NOV Fall 23.1 22.3 2014
11 20.2 DEC Winter 17.1 16.5 2014
12 19.9 JAN Winter 17.1 16.7 2015
13 17.3 FEB Winter 18.1 20.8 2015
14 23.6 MAR Spring 20.7 18.9 2015
15 26.2 APR Spring 24.4 22.7 2015
16 28.3 MAY Spring 26.9 26.1 2015
17 30.6 JUN Summer 29.7 30.0 2015
18 29.5 JUL Summer 29.2 30.0 2015
19 28.8 AUG Summer 28.0 28.6 2015
20 28.8 SEP Fall 27.8 27.4 2015
21 27.7 OCT Fall 26.2 25.2 2015
22 26.2 NOV Fall 24.3 23.5 2015
23 22.4 DEC Winter 19.6 18.9 2015
24 19.3 JAN Winter 16.8 16.2 2016
25 19.6 FEB Winter 16.4 15.5 2016
26 21.6 MAR Spring 18.3 17.5 2016
27 26.9 APR Spring 24.9 24.0 2016
28 28.9 MAY Spring 27.6 27.1 2016
29 29.7 JUN Summer 28.8 29.4 2016
30 30.2 JUL Summer 29.4 30.3 2016
31 29.4 AUG Summer 28.9 30.1 2016
32 28.4 SEP Fall 27.9 27.8 2016
33 28.4 OCT Fall 27.5 27.0 2016
34 25.7 NOV Fall 23.4 22.6 2016
35 22.9 DEC Winter 20.4 20.1 2016

Plots


Scatter plot

想知道 Temperature 資料中 台北市 及 台中市 溫度的關係

In [7]:
plt.scatter(Temperature['Taipei'], Temperature['Taichung'])
plt.show()

Plots


Scatter plot

想知道 Temperature 資料中 台北市 及 台中市 溫度的關係,加入標籤及標題

In [8]:
x = Temperature['Taipei']
y = Temperature['Taichung']
plt.scatter(x, y)
plt.xlabel("Taipei")
plt.ylabel("Taichung")
plt.title("Taipei VS Taichung")
plt.show()

Plots


Scatter plot

想知道 Temperature 資料中 台北市 及 台中市 溫度的關係,加入標籤及標題,將標題改為中文

In [9]:
x = Temperature['Taipei']
y = Temperature['Taichung']
plt.scatter(x, y)
plt.xlabel("Taipei")
plt.ylabel("Taichung")
plt.title("台北市及台中市溫度的散佈圖")
plt.show()

Plots


Scatter plot

想知道 Temperature 資料中 台北市 及 台中市 溫度的關係,加入標籤及標題,將標題改為中文

Note

Mac 找一個中文字型,並複製他的路徑

Mac Font

Plots


Scatter plot

想知道 Temperature 資料中 台北市 及 台中市 溫度的關係,加入標籤及標題,將標題改為中文

Note

Windows 找一個中文字型,並複製他的路徑

Windows Font

Plots


Scatter plot

想知道 Temperature 資料中 台北市 及 台中市 溫度的關係,加入標籤及標題,將標題改為中文

Note

設定畫圖用字型

In [10]:
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"/System/Library/Fonts/STHeiti Light.ttc",size=12)

Plots


Scatter plot

想知道 Temperature 資料中 台北市 及 台中市 溫度的關係,加入標籤及標題,將標題改為中文

In [11]:
x = Temperature['Taipei']
y = Temperature['Taichung']
plt.scatter(x, y)
plt.xlabel("Taipei")
plt.ylabel("Taichung")
plt.title(u"台北市及台中市溫度的散佈圖",fontproperties=font)
plt.show()

Plots


Line plot

Sine Function $$ f(x) = sin(x) ; -2 \le x \le 2 $$

In [12]:
x = np.arange(-2.0, 2.0, 0.01)
y = np.sin(2 * np.pi * x)

plt.plot(x, y)
plt.xlabel("x")
plt.ylabel("y")
plt.title("f(x) = sin(x)")
plt.show()

Plots


Line plot

Sine Function,加入格線 $$ f(x) = sin(x) ; -2 \le x \le 2 $$

In [13]:
x = np.arange(-2.0, 2.0, 0.01)
y = np.sin(2 * np.pi * x)

plt.plot(x, y)
plt.xlabel("x")
plt.ylabel("y")
plt.title("f(x) = sin(x)")
plt.grid(True)
plt.show()

Exercise

Q. Line Plot

$$ f(x) = x^2 ; -2 \le x \le 2 $$

Answers

In [14]:
x = np.arange(-2.0, 2.0, 0.01)
y = x**2

plt.plot(x, y)
plt.xlabel("x")
plt.ylabel("y")
plt.title("$f(x) = x^2$")
plt.grid(True)
plt.show()

Exercise

Q. Line Plot

$$ f(x) = cos(x) ; -2 \le x \le 2 $$

Answers

In [15]:
x = np.arange(-2.0, 2.0, 0.01)
y = np.cos(2 * np.pi * x)

plt.plot(x, y)
plt.xlabel("x")
plt.ylabel("y")
plt.title("f(x) = cos(x)")
plt.grid(True)
plt.show()

Plots


Line plot

疊圖
Sine Function & Cosine Function

In [16]:
x = np.arange(-2.0, 2.0, 0.01)
y_sin = np.sin(2 * np.pi * x)
y_cos = np.cos(2 * np.pi * x)

plt.plot(x, y_sin)
plt.plot(x, y_cos)
plt.xlabel("x")
plt.ylabel("y")
plt.title("f(x) = sin(x) & f(x) = cos(x)")
plt.grid(True)
plt.show()

Plots


Line plot

疊圖,改顏色
Sine Function(青色) & Cosine Function(紅色)

In [17]:
x = np.arange(-2.0, 2.0, 0.01)
y_sin = np.sin(2 * np.pi * x)
y_cos = np.cos(2 * np.pi * x)

plt.plot(x, y_sin, "cyan")
plt.plot(x, y_cos, "red")
plt.xlabel("x")
plt.ylabel("y")
plt.title("f(x) = sin(x) & f(x) = cos(x)")
plt.grid(True)
plt.show()

Plots


Line plot

疊圖,改顏色、線條型態
Sine Function(dashed) & Cosine Function(dotted)

In [18]:
x = np.arange(-2.0, 2.0, 0.01)
y_sin = np.sin(2 * np.pi * x)
y_cos = np.cos(2 * np.pi * x)

plt.plot(x, y_sin, "c--")
plt.plot(x, y_cos, "r:")
plt.xlabel("x")
plt.ylabel("y")
plt.title("f(x) = sin(x) & f(x) = cos(x)")
plt.grid(True)
plt.show()

Plots


Note

Color 顏色

  • b : blue
  • g : green
  • r : red
  • c : cyan
  • m : magenta
  • y : yellow
  • k : black
  • w : white

Plots


Note

Linestyle 線條樣式

  • '-' or 'solid':solid line
  • '--' or 'dashed':dashed line
  • '-. ' or 'dashdot':dash-dotted line
  • ':' or 'dotted':dotted line
  • 'None' or ' ' or '':draw nothing

Plots


Line plot

疊圖,改顏色、線條型態,加入圖例
Sine Function(dashed) & Cosine Function(dotted)

In [19]:
x = np.arange(-2.0, 2.0, 0.01)
y_sin = np.sin(2 * np.pi * x)
y_cos = np.cos(2 * np.pi * x)

plt.plot(x, y_sin, "c--", label="sin")
plt.plot(x, y_cos, "r:", label="cos")
plt.xlabel("x")
plt.ylabel("y")
plt.title("f(x) = sin(x) & f(x) = cos(x)")
plt.grid(True)
plt.legend()
plt.show()

Plots


Note

Legend 位置

plt.legend(loc='位置')
  • best:最適合位置
  • upper right:右上角
  • upper left:左上角
  • lower left:左下角
  • lower right:右下角
  • right:右邊
  • center left:左邊中間
  • center right:右邊中間
  • lower center:下方中間
  • upper center:上方中間
  • center:正中間

Plots


Histogram plot

Normal distribution ( size = 10000 )

Review

隨機產生 n 個服從 Normal( $\mu$ , $\sigma$ )

np.random.normal( mu , sigma , n )

Plots


Histogram plot

Normal distribution ( size = 10000 )

In [20]:
normal_data = np.random.normal(size = 10000)
plt.hist(normal_data)
plt.title("Normal distribution")
plt.show()

Plots


Histogram plot

Uniform distribution ( size = 10000 )

Review

隨機產生 n 個介在 low~high 的值並服從 unifom

np.random.uniform( low , high , n  )

Plots


Histogram plot

Uniform distribution ( size = 10000 )

In [21]:
uniform_data = np.random.uniform(size = 10000)
plt.hist(uniform_data)
plt.title("Uniform distribution")
plt.show()

Plots


Histogram plot

左右圖
Normal distribution ( size = 10000) ← 左
Uniform distribution ( size = 10000 ) ← 右

In [22]:
normal_data = np.random.normal(size = 10000)
uniform_data = np.random.uniform(size = 10000)
plt.subplot(1, 2, 1)
plt.hist(normal_data)
plt.title("Normal distribution")
plt.subplot(1, 2, 2)
plt.hist(uniform_data)
plt.title("Uniform distribution")
plt.show()

Plots


Note

Subplot 位置擺放

plt.subplot(m, n, loc)
  • m:產生 $m \times n$ 的 subplot
  • n : 產生 $m \times n$ 的 subplot
  • loc : 表示為第幾個 subplot ,計算順序依序為:從左到右,從上到下

Example:畫一個 $2 \times 3$

(2, 3, 1) (2, 3, 2) (2, 3, 3)
(2, 3, 4) (2, 3, 5) (2, 3, 6)

Exercise

Q. 使用 Temperature 這組資料,劃出一個 上下排列 台北、台中、高雄溫度的直方圖

Answers

In [23]:
plt.subplot(3, 1, 1)
plt.hist(Temperature['Taipei'])
plt.title("Taipei Temperature")
plt.subplot(3, 1, 2)
plt.hist(Temperature['Taichung'])
plt.title("Taichung Temperature")
plt.subplot(3, 1, 3)
plt.hist(Temperature['Kaohsiung'])
plt.title("Kaohsiung Temperature")
plt.tight_layout()
plt.show()

Plots


Bar plot

Note

台北市在各季節的溫度狀況

In [24]:
Taipei_Season
Out[24]:
Season
Fall      25.577778
Spring    22.544444
Summer    29.677778
Winter    17.555556
Name: Taipei, dtype: float64

Plots


Bar plot

利用 Taipei_Season 畫一個,台北在不同季節下的溫度變化

In [25]:
season_cut = range(len(Taipei_Season))
plt.bar(season_cut , Taipei_Season, color="blue", align = "center")
plt.xticks(season_cut , Taipei_Season.index)
plt.show()

Plots


Bar plot

利用 Taipei_Season 畫一個,台北在不同季節下的溫度變化,在 Summer 的上方加入一個 Max 的標註

In [26]:
season_cut = range(len(Taipei_Season))
plt.bar(season_cut , Taipei_Season, color="blue", align = "center")
plt.xticks(season_cut , Taipei_Season.index)
plt.annotate( s='Max', xy=( 2 ,max(Taipei_Season) ), xytext=(2+1 ,max(Taipei_Season)+1),
              arrowprops=dict(facecolor='black'))
plt.show()

Plots


Note

Annotating 標註

plt.annotate( s='文字',
              xy=(標註圖_x座標, 標註圖_y座標), 
              xytext=(標註字_x座標, 標註字_y座標),
              arrowprops=dict(標註圖形態) )

Plots


Bar plot ( barh ),轉換圖的方向

利用 Taipei_Season 畫一個,台北在不同季節下的溫度變化

In [27]:
season_cut = range(len(Taipei_Season))
plt.barh(season_cut, Taipei_Season, color="blue", align = "center")
plt.yticks(season_cut,Taipei_Season.index )
plt.show()

Plots


Bar plot ( barh ),轉換圖的方向

利用 Taipei_Season 畫一個,台北在不同季節下的溫度變化,並存成 Taipei_Season.png

In [28]:
season_cut = range(len(Taipei_Season))
plt.barh(season_cut, Taipei_Season, color="blue", align = "center")
plt.yticks(season_cut,Taipei_Season.index )
plt.savefig(filename = "/Users/hsinyu/Desktop/Python資料分析/Taipei_Season.png", format = "png")

Plots


Pie plot

Note

台北市在各季節的溫度狀況

In [29]:
Taipei_Season
Out[29]:
Season
Fall      25.577778
Spring    22.544444
Summer    29.677778
Winter    17.555556
Name: Taipei, dtype: float64

Plots


Pie plot

利用 Taipei_Season 畫一個,台北溫度在不同季節的分佈狀況

In [30]:
plt.pie( Taipei_Season, explode=(0, 0, 0, 0),
         labels=['Fall', 'Spring', 'Summer', 'Winter'],
         shadow=True, startangle=90 )
plt.axis('equal')
plt.legend(loc='best')
plt.show()

Plots


Pie plot

加入 百分比
利用 Taipei_Season 畫一個,台北溫度在不同季節的分佈狀況

In [31]:
plt.pie( Taipei_Season, explode=(0, 0, 0, 0),
         labels=['Fall', 'Spring', 'Summer', 'Winter'],
         autopct='%.1f%%', shadow=True, startangle=90 )
plt.axis('equal')
plt.legend(loc='best')
plt.show()

Plots


Pie plot

加入百分比,把 Summer 分離開表示
利用 Taipei_Season 畫一個,台北溫度在不同季節的分佈狀況

In [32]:
plt.pie( Taipei_Season, explode=(0, 0, 0.1, 0),
         labels=['Fall', 'Spring', 'Summer', 'Winter'],
         autopct='%.1f%%', shadow=True, startangle=90 )
plt.axis('equal')
plt.legend(loc='best')
plt.show()

Matplotlib


Supplement -- Seaborn

Seaborn

安裝 seaborn package

Anaconda Windows Open Anaconda Prompt

conda install seaborn

install seaborn

Seaborn

安裝 seaborn package

Anaconda Mac Open Terminal

conda install seaborn

install seaborn

Seaborn

In [33]:
import seaborn as sns

Seaborn

Scatter plot

想知道 Temperature 資料中 台北市 及 台中市 溫度的關係

In [34]:
x = Temperature['Taipei']
y = Temperature['Taichung']
sns.jointplot(x, y)
Out[34]:
<seaborn.axisgrid.JointGrid at 0x109fd2550>

Seaborn

Histogram plot

Normal distribution ( size = 10000 )

In [35]:
normal_data = np.random.normal(size = 100000)
sns.distplot(normal_data)
Out[35]:
<matplotlib.axes._subplots.AxesSubplot at 0x10a5f9f98>

Seaborn

Bar plot

利用 Taipei_Season 畫一個,台北在不同季節下的溫度變化

In [36]:
sns.barplot(Taipei_Season.index, Taipei_Season)
Out[36]:
<matplotlib.axes._subplots.AxesSubplot at 0x10a214d68>