
Reference : https://www.wikiwand.com/en/Data_analysis
import matplotlib.pyplot as plt
%matplotlib inline
讓圖畫在 Jupyter Notebook 中
import numpy as np
import pandas as pd
plt.xlabel( ) / plt.ylabel( ):X 軸標籤 / Y 軸標籤plt.xlim( ) / plt.ylim( ):X 軸範圍 / Y 軸範圍plt.title( ):圖標題plt.grid( ):格線plt.legend:圖例plt.subplot:子圖plt.show:將畫好的圖顯示出來plt.savefig:另存圖檔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)
| 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 |
Taipei_Season = Temperature['Taipei'].groupby(Temperature['Season']).mean()
Taipei_Season
Season Fall 25.577778 Spring 22.544444 Summer 29.677778 Winter 17.555556 Name: Taipei, dtype: float64
scatter( ):Scatter plot 散佈圖plot( ):Line plot 線圖hist( ):Histogram 直方圖bar( ) / barh:Bar plot 長條圖Temperature
| 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 |
plt.scatter(Temperature['Taipei'], Temperature['Taichung'])
plt.show()
x = Temperature['Taipei']
y = Temperature['Taichung']
plt.scatter(x, y)
plt.xlabel("Taipei")
plt.ylabel("Taichung")
plt.title("Taipei VS Taichung")
plt.show()
x = Temperature['Taipei']
y = Temperature['Taichung']
plt.scatter(x, y)
plt.xlabel("Taipei")
plt.ylabel("Taichung")
plt.title("台北市及台中市溫度的散佈圖")
plt.show()
Mac 找一個中文字型,並複製他的路徑

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

設定畫圖用字型
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"/System/Library/Fonts/STHeiti Light.ttc",size=12)
x = Temperature['Taipei']
y = Temperature['Taichung']
plt.scatter(x, y)
plt.xlabel("Taipei")
plt.ylabel("Taichung")
plt.title(u"台北市及台中市溫度的散佈圖",fontproperties=font)
plt.show()
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()
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()
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()
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()
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()
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()
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()
'-' or 'solid':solid line '--' or 'dashed':dashed line '-. ' or 'dashdot':dash-dotted line ':' or 'dotted':dotted line'None' or ' ' or '':draw nothingx = 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()
plt.legend(loc='位置')
best:最適合位置upper right:右上角upper left:左上角lower left:左下角lower right:右下角right:右邊center left:左邊中間center right:右邊中間lower center:下方中間upper center:上方中間center:正中間隨機產生 n 個服從 Normal( $\mu$ , $\sigma$ )
np.random.normal( mu , sigma , n )
normal_data = np.random.normal(size = 10000)
plt.hist(normal_data)
plt.title("Normal distribution")
plt.show()
隨機產生 n 個介在 low~high 的值並服從 unifom
np.random.uniform( low , high , n )
uniform_data = np.random.uniform(size = 10000)
plt.hist(uniform_data)
plt.title("Uniform distribution")
plt.show()
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()
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) |
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()
台北市在各季節的溫度狀況
Taipei_Season
Season Fall 25.577778 Spring 22.544444 Summer 29.677778 Winter 17.555556 Name: Taipei, dtype: float64
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()
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()
plt.annotate( s='文字',
xy=(標註圖_x座標, 標註圖_y座標),
xytext=(標註字_x座標, 標註字_y座標),
arrowprops=dict(標註圖形態) )
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()
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")
台北市在各季節的溫度狀況
Taipei_Season
Season Fall 25.577778 Spring 22.544444 Summer 29.677778 Winter 17.555556 Name: Taipei, dtype: float64
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()
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()
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()
Anaconda Windows Open Anaconda Prompt
conda install seaborn

Anaconda Mac Open Terminal
conda install seaborn

import seaborn as sns
想知道 Temperature 資料中 台北市 及 台中市 溫度的關係
x = Temperature['Taipei']
y = Temperature['Taichung']
sns.jointplot(x, y)
<seaborn.axisgrid.JointGrid at 0x109fd2550>
Normal distribution ( size = 10000 )
normal_data = np.random.normal(size = 100000)
sns.distplot(normal_data)
<matplotlib.axes._subplots.AxesSubplot at 0x10a5f9f98>
利用 Taipei_Season 畫一個,台北在不同季節下的溫度變化
sns.barplot(Taipei_Season.index, Taipei_Season)
<matplotlib.axes._subplots.AxesSubplot at 0x10a214d68>