用python画大白_[Python][可视化]matplotlib基础入门
Python包matplotlib畫(huà)圖入門(mén),以折線圖為例。
在使用之前,導(dǎo)入matplotlib包,設(shè)置中文字體
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['font.family'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
PS:關(guān)于matplotlib畫(huà)圖中文無(wú)法顯示的問(wèn)題請(qǐng)參見(jiàn)我的另一篇博文:ubuntu16.04中解決matplotlib畫(huà)圖中文無(wú)法顯示問(wèn)題
以折線圖為例入門(mén)matplotlib
1 簡(jiǎn)單畫(huà)圖
下圖可用于展現(xiàn)一周的天氣情況
# 創(chuàng)建畫(huà)布
plt.figure()
# 繪制圖像
plt.plot([1, 2, 3, 4, 5, 6, 7], [17, 17, 18, 15, 11, 11, 13])
# 保存圖像
plt.savefig("1.png") # 用于導(dǎo)出圖片
# 顯示圖像
plt.show()
2 改變畫(huà)布大小與清晰度
通過(guò)figsize和dpi 調(diào)整畫(huà)布
# 創(chuàng)建并調(diào)整畫(huà)布
plt.figure(figsize=(20, 8), dpi=80) # 設(shè)置畫(huà)布大小與清晰度
plt.plot([1, 2, 3, 4, 5, 6, 7], [17, 17, 18, 15, 11, 11, 13])
plt.show()
3 使用隨機(jī)數(shù)據(jù)
例如畫(huà)出某上海11點(diǎn)到12點(diǎn)1小時(shí)內(nèi)每分鐘的溫度變化折線圖,溫度范圍在15度~18度
# 準(zhǔn)備數(shù)據(jù) x和y,隨機(jī)生成
x = range(60) # 60個(gè)點(diǎn)
y = [random.uniform(15, 18) for i in x] #用random包生成60個(gè)均勻分布uniform的隨機(jī)數(shù)
plt.figure(figsize=(20, 8), dpi=80)
plt.plot(x, y)
plt.show()
4 設(shè)置XY軸
跟上圖相比,增加X(jué)Y軸的格式
x = range(60) # 60個(gè)點(diǎn)
y = [random.uniform(15, 18) for i in x]
plt.figure(figsize=(20, 8), dpi=80)
plt.plot(x, y)
# 設(shè)置X和Y軸
plt.xticks(x) # x軸每分鐘顯示一次
plt.yticks(range(0, 40, 5))#增加Y軸,數(shù)值從0-40,每隔5顯示一次
plt.show()
5 美化X軸設(shè)置
X軸以中文,間隔時(shí)間顯示
x = range(60)
y = [random.uniform(15, 18) for i in x]
plt.figure(figsize=(20, 8), dpi=80)
plt.plot(x, y)
# 美化X軸設(shè)置
x_label = ["11點(diǎn){}分".format(i) for i in x] #設(shè)置x軸格式
plt.xticks(x[::5], x_label[::5]) #每隔5顯示一次
plt.yticks(range(0, 40, 5))
plt.show()
6 顯示網(wǎng)格
在圖背景中顯示網(wǎng)格
x = range(60) # 60個(gè)點(diǎn)
y = [random.uniform(15, 18) for i in x]
plt.figure(figsize=(20, 8), dpi=80)
plt.plot(x, y)
x_label = ["11點(diǎn){}分".format(i) for i in x]
plt.xticks(x[::5], x_label[::5])
plt.yticks(range(0, 40, 5))
# 添加網(wǎng)格顯示
plt.grid(True, linestyle="--", alpha=0.5) #true讓它顯示網(wǎng)格(true也可以刪除),網(wǎng)格類型linestyle設(shè)為--的虛線,透明度alpha設(shè)為0.5
plt.show()
7 設(shè)置標(biāo)簽與標(biāo)題信息
添加x和y軸以及標(biāo)題信息
x = range(60)
y = [random.uniform(15, 18) for i in x]
plt.figure(figsize=(20, 8), dpi=80)
plt.plot(x, y)
x_label = ["11點(diǎn){}分".format(i) for i in x]
plt.xticks(x[::5], x_label[::5])
plt.yticks(range(0, 40, 5))
plt.grid(True, linestyle="--", alpha=0.5)
# 添加描述信息
plt.xlabel("時(shí)間變化")
plt.ylabel("溫度變化")
plt.title("上海11點(diǎn)到12點(diǎn)每分鐘的溫度變化狀況")
plt.show()
8 增加另一條折線
再添加一個(gè)城市北京的溫度變化,北京的溫度在1度到3度
x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]
# 增加數(shù)據(jù)
y_beijing = [random.uniform(1, 3) for i in x] # 增加了B城市數(shù)據(jù),1-3之間
plt.figure(figsize=(20, 8), dpi=80)
# 繪制兩條折線
plt.plot(x, y_shanghai, color="r", linestyle="-.", label="上海")# 上海折線設(shè)為紅色虛線
plt.plot(x, y_beijing, color="b", label="北京") # 北京折線設(shè)為藍(lán)色實(shí)線
# 顯示圖例
plt.legend()
x_label = ["11點(diǎn){}分".format(i) for i in x]
plt.xticks(x[::5], x_label[::5])
plt.yticks(range(0, 40, 5))
plt.grid(linestyle="--", alpha=0.5)
plt.xlabel("時(shí)間變化")
plt.ylabel("溫度變化")
plt.title("上海、北京兩城市11點(diǎn)到12點(diǎn)每分鐘的溫度變化狀況")
plt.show()
9 兩條折線分兩個(gè)子圖顯示
調(diào)用plt.subplots函數(shù)將兩條折線分兩個(gè)子圖顯示
x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]
y_beijing = [random.uniform(1, 3) for i in x]
# 創(chuàng)建多個(gè)子圖,nrows=1, ncols=2代表1行兩列
figure, axes = plt.subplots(nrows=1, ncols=2, figsize=(20, 8), dpi=80)
# 繪制圖像,axes[0]代表第一個(gè)繪圖區(qū),axes[1]代表第二個(gè)繪圖區(qū),
axes[0].plot(x, y_shanghai, color="r", linestyle="-.", label="上海") #
axes[1].plot(x, y_beijing, color="b", label="北京")
# 顯示圖例
axes[0].legend()
axes[1].legend()
# 修改x、y刻度,分別設(shè)置兩個(gè)子圖的坐標(biāo)
x_label = ["11點(diǎn){}分".format(i) for i in x]
axes[0].set_xticks(x[::5])
axes[0].set_xticklabels(x_label)
axes[0].set_yticks(range(0, 40, 5))
axes[1].set_xticks(x[::5])
axes[1].set_xticklabels(x_label)
axes[1].set_yticks(range(0, 40, 5))
# 添加網(wǎng)格顯示
axes[0].grid(linestyle="--", alpha=0.5)
axes[1].grid(linestyle="--", alpha=0.5)
# 添加描述信息
axes[0].set_xlabel("時(shí)間變化")
axes[0].set_ylabel("溫度變化")
axes[0].set_title("上海11點(diǎn)到12點(diǎn)每分鐘的溫度變化狀況")
axes[1].set_xlabel("時(shí)間變化")
axes[1].set_ylabel("溫度變化")
axes[1].set_title("北京11點(diǎn)到12點(diǎn)每分鐘的溫度變化狀況")
plt.show()
繪制散點(diǎn)圖
# 準(zhǔn)備數(shù)據(jù)
x = [225.98, 247.07, 253.14, 457.85, 241.58, 301.01, 20.67, 288.64,
163.56, 120.06, 207.83, 342.75, 147.9 , 53.06, 224.72, 29.51,
21.61, 483.21, 245.25, 399.25, 343.35]
y = [196.63, 203.88, 210.75, 372.74, 202.41, 247.61, 24.9 , 239.34,
140.32, 104.15, 176.84, 288.23, 128.79, 49.64, 191.74, 33.1 ,
30.74, 400.02, 205.35, 330.64, 283.45]
# 創(chuàng)建畫(huà)布
plt.figure(figsize=(20, 8), dpi=80)
# 繪制散點(diǎn)圖
plt.scatter(x, y)
# 顯示圖像
plt.show()
繪制柱狀圖
# 準(zhǔn)備數(shù)據(jù)
names = ['阿花','阿草','阿樹(shù)','小月','小星', '大白','大黃','大黑','嘻嘻','哈哈','嘿嘿']
tickets = [73853,57767,22354,15969,14839,8725,8716,8318,7916,6764,52222]
# 創(chuàng)建畫(huà)布
plt.figure(figsize=(20, 8), dpi=80)
# 繪制柱狀圖
x_ticks = range(len(names))
plt.bar(x_ticks, tickets, color=['b','r','g','y','c','m','y','k','c','g','b'])
# 修改x刻度
plt.xticks(x_ticks, names)
# 添加標(biāo)題
plt.title("個(gè)人收入對(duì)比")
# 網(wǎng)格顯示
plt.grid(linestyle="--", alpha=0.5)
# 顯示圖像
plt.show()
繪制直方圖
# 準(zhǔn)備數(shù)據(jù)
time = [131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115, 99, 136, 126, 134, 95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117, 86, 95, 144, 105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123, 86, 101, 99, 136,123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140, 83, 110, 102,123,107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144, 83, 123, 111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137, 92,121, 112, 146, 97, 137, 105, 98, 117, 112, 81, 97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110,105, 129, 137, 112, 120, 113, 133, 112, 83, 94, 146, 133, 101,131, 116, 111, 84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150]
# 創(chuàng)建畫(huà)布
plt.figure(figsize=(20, 8), dpi=80)
# 繪制直方圖
distance = 2
group_num = int((max(time) - min(time)) / distance)
plt.hist(time, bins=group_num, density=True)
# 修改x軸刻度
plt.xticks(range(min(time), max(time) + 2, distance))
# 添加網(wǎng)格
plt.grid(linestyle="--", alpha=0.5)
# 顯示圖像
plt.show()
繪制餅圖
# 準(zhǔn)備數(shù)據(jù)
movie_name = ['雷神3:諸神黃昏','正義聯(lián)盟','東方快車謀殺案','尋夢(mèng)環(huán)游記','全球風(fēng)暴','降魔傳','追捕','七十七天','密戰(zhàn)','狂獸','其它']
place_count = [60605,54546,45819,28243,13270,9945,7679,6799,6101,4621,20105]
# 創(chuàng)建畫(huà)布
plt.figure(figsize=(20, 8), dpi=80)
# 繪制餅圖
plt.pie(place_count, labels=movie_name, colors=['b','r','g','y','c','m','y','k','c','g','y'], autopct="%1.2f%%")
# 顯示圖例
plt.legend()
plt.axis('equal')
# 顯示圖像
plt.show()
后記
matplotlib是一個(gè)python廣泛熟悉的包,在可視化上發(fā)揮著重要的作用,網(wǎng)上關(guān)于它的資料已經(jīng)多如繁星了,matplotlib官方網(wǎng)站也提供了非常多的信息,它的gallery有非常多的模板可供使用。本文的作用在于入門(mén)。以上就是用matplotlib畫(huà)圖的基礎(chǔ)。本次學(xué)習(xí)參考了Python教程4天快速入手Python數(shù)據(jù)挖掘的視頻教程,可以下載到課程的code,便于學(xué)習(xí)。
總結(jié)
以上是生活随笔為你收集整理的用python画大白_[Python][可视化]matplotlib基础入门的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 股票减持多少比例要出公告?
- 下一篇: python简单语法_python的基本