iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 18
2
Big Data

R 語言使用者的 Python 學習筆記系列 第 18

[第 18 天] 資料視覺化 matplotlib

  • 分享至 

  • twitterImage
  •  

在我們昨天的文章 [第 17 天] 資料角力有提到,進行資料角力(Data wrangling)的目的多半是為了後續的資料視覺化或者建立機器學習的模型。R 語言使用者的資料視覺化工具有靜態的 Base plotting system(R 語言內建的繪圖功能)跟 ggplot2 套件,與動態的 plotly 套件。而 Python 的視覺化套件有靜態的 matplotlibseaborn 套件,與動態的 bokeh 套件。

我們今天試著使用看看 matplotlib 並且也使用 R 語言的 Base plotting system 來畫一些基本的圖形,包括:

  • 直方圖(Histogram)
  • 散佈圖(Scatter plot)
  • 線圖(Line plot)
  • 長條圖(Bar plot)
  • 盒鬚圖(Box plot)

我們的開發環境是 Jupyter Notebook,這個指令可以讓圖形不會在新視窗呈現。

%matplotlib inline

直方圖(Histogram)

Python

使用 matplotlib.pyplothist() 方法。

%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

normal_samples = np.random.normal(size = 100000) # 生成 100000 組標準常態分配(平均值為 0,標準差為 1 的常態分配)隨機變數
uniform_samples = np.random.uniform(size = 100000) # 生成 100000 組介於 0 與 1 之間均勻分配隨機變數

plt.hist(normal_samples)
plt.show()
plt.hist(uniform_samples)
plt.show()

day1801

day1802

如果你對於 numpy 套件的 random() 方法覺得陌生,我推薦你參考 [第 13 天] 常用屬性或方法(2)ndarray

R 語言

使用 hist() 函數。

normal_samples <- runif(100000) # 生成 100000 組標準常態分配(平均值為 0,標準差為 1 的常態分配)隨機變數
uniform_samples <- rnorm(100000) # 生成 100000 組介於 0 與 1 之間均勻分配隨機變數

hist(normal_samples)
hist(uniform_samples)

day1803

day1804

散佈圖(Scatter plot)

Python

使用 matplotlib.pyplotscatter() 方法。

%matplotlib inline

import matplotlib.pyplot as plt

speed = [4, 4, 7, 7, 8, 9, 10, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 16, 16, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 20, 20, 20, 20, 20, 22, 23, 24, 24, 24, 24, 25]
dist = [2, 10, 4, 22, 16, 10, 18, 26, 34, 17, 28, 14, 20, 24, 28, 26, 34, 34, 46, 26, 36, 60, 80, 20, 26, 54, 32, 40, 32, 40, 50, 42, 56, 76, 84, 36, 46, 68, 32, 48, 52, 56, 64, 66, 54, 70, 92, 93, 120, 85]

plt.scatter(speed, dist)
plt.show()

day1805

R 語言

使用 plot() 函數。

plot(cars$speed, cars$dist)

day1806

線圖(Line plot)

Python

使用 matplotlib.pyplotplot() 方法。

%matplotlib inline

import matplotlib.pyplot as plt

speed = [4, 4, 7, 7, 8, 9, 10, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 16, 16, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 20, 20, 20, 20, 20, 22, 23, 24, 24, 24, 24, 25]
dist = [2, 10, 4, 22, 16, 10, 18, 26, 34, 17, 28, 14, 20, 24, 28, 26, 34, 34, 46, 26, 36, 60, 80, 20, 26, 54, 32, 40, 32, 40, 50, 42, 56, 76, 84, 36, 46, 68, 32, 48, 52, 56, 64, 66, 54, 70, 92, 93, 120, 85]

plt.plot(speed, dist)
plt.show()

day1807

R 語言

使用 plot() 函數,指定參數 type = "l"

plot(cars$speed, cars$dist, type = "l")

day1808

長條圖(Bar plot)

Python

使用 matplotlib.pyplotbar() 方法。

%matplotlib inline

from collections import Counter
import matplotlib.pyplot as plt
import numpy as np

cyl = [6 ,6 ,4 ,6 ,8 ,6 ,8 ,4 ,4 ,6 ,6 ,8 ,8 ,8 ,8 ,8 ,8 ,4 ,4 ,4 ,4 ,8 ,8 ,8 ,8 ,4 ,4 ,4 ,8 ,6 ,8 ,4]

labels, values = zip(*Counter(cyl).items())
width = 1

plt.bar(indexes, values)
plt.xticks(indexes + width * 0.5, labels)
plt.show()

day1809

R 語言

使用 barplot() 函數。

barplot(table(mtcars$cyl))

day1810

盒鬚圖(Box plot)

python

使用 matplotlib.pyplotboxplot() 方法。

%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

normal_samples = np.random.normal(size = 100000) # 生成 100000 組標準常態分配(平均值為 0,標準差為 1 的常態分配)隨機變數

plt.boxplot(normal_samples)
plt.show()

day1811

R 語言

使用 boxplot() 函數。

normal_samples <- runif(100000) # 生成 100000 組標準常態分配(平均值為 0,標準差為 1 的常態分配)隨機變數
boxplot(normal_samples)

day1812

輸出圖形

python

使用圖形物件的 savefig() 方法。

import numpy as np
import matplotlib.pyplot as plt

normal_samples = np.random.normal(size = 100000) # 生成 100000 組標準常態分配(平均值為 0,標準差為 1 的常態分配)隨機變數

plt.hist(normal_samples)
plt.savefig(filename = "my_hist.png", format = "png")

day1813

R 語言

先使用 png() 函數建立一個空的 .png 圖檔,繪圖後再輸入 dev.off()

normal_samples <- runif(100000) # 生成 100000 組標準常態分配(平均值為 0,標準差為 1 的常態分配)隨機變數
png("my_hist.png")
hist(normal_samples)
dev.off()

day1814

小結

第十八天我們練習使用 Python 的視覺化套件 matplotlib 繪製基本的圖形,並且與 R 語言的 Base plotting system 相互對照。

參考連結

同步刊登於 Github:https://github.com/yaojenkuo/learn_python_for_a_r_user


上一篇
[第 17 天] 資料角力
下一篇
[第 19 天] 資料視覺化(2)Seaborn
系列文
R 語言使用者的 Python 學習筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
flowblue
iT邦新手 5 級 ‧ 2017-10-08 16:27:51

Python 長條圖(Bar plot)那段無法執行。NameError: name 'indexes' is not defined

hyl iT邦新手 5 級 ‧ 2019-02-13 15:46:17 檢舉

%matplotlib inline

from collections import Counter
import matplotlib.pyplot as plt
import numpy as np

cyl = [6 ,6 ,4 ,6 ,8 ,6 ,8 ,4 ,4 ,6 ,6 ,8 ,8 ,8 ,8 ,8 ,8 ,4 ,4 ,4 ,4 ,8 ,8 ,8 ,8 ,4 ,4 ,4 ,8 ,6 ,8 ,4]

labels, values = zip(*Counter(cyl).items())
indexes = np.arange(len(values))

plt.bar(indexes, values, width = 0.5)
plt.xticks(indexes, labels)
plt.show()

另外python輸出圖形
savefig()
#filename改成fname就能執行了

import numpy as np
import matplotlib.pyplot as plt
normal_samples=np.random.normal(size=100000)
plt.hist(normal_samples)
plt.savefig(fname="my_hist.png",format="png")

https://ithelp.ithome.com.tw/upload/images/20220403/20125900SZlGvgXQ4s.png``

我要留言

立即登入留言