python數(shù)據(jù)可視化的那些操作你了解嗎
0. 前言
數(shù)據(jù)處理過(guò)程中,可視化可以更直觀得感受數(shù)據(jù),因此打算結(jié)合自己的一些實(shí)踐經(jīng)理,以效果為準(zhǔn)寫(xiě)這篇博客。內(nèi)容應(yīng)該會(huì)不斷擴(kuò)充。
1. matplotlib中figure、subplot和plot等什么關(guān)系
記住這幾個(gè)關(guān)系可以結(jié)合實(shí)際。假設(shè)你去外面寫(xiě)生要帶哪些工具呢,包括畫(huà)板、畫(huà)紙還有畫(huà)筆,那么就可以一一對(duì)應(yīng)了。
| 函數(shù) | 工具 |
|---|---|
| figure | 畫(huà)板 |
| subplot、add_subplot | 畫(huà)紙 |
| plot、hist、scatter | 畫(huà)筆 |
那么再往深處想,畫(huà)紙貼在畫(huà)板上,畫(huà)紙可以裁剪成多塊布局在畫(huà)板上,而畫(huà)筆只能畫(huà)在紙上,可能這樣講有點(diǎn)籠統(tǒng),下面一個(gè)代碼配合注釋就可以清晰明白啦。(感覺(jué)需要記住以下代碼)
代碼
import matplotlib.pyplot as plt import numpy as np # 拿起畫(huà)板 fig = plt.figure() # 在畫(huà)板上貼上畫(huà)紙 ax1 = fig.add_subplot(221) ax2 = fig.add_subplot(222) ax3 = fig.add_subplot(223) # 一步完成(直接拿起畫(huà)板和畫(huà)紙)----------------- # ax1 = plt.subplot(221) # ax2 = plt.subplot(222) # ax3 = plt.subplot(223) # ---------------------------------------- # 在畫(huà)紙上作圖 ax1.hist(np.random.randn(100), bins=20, color='k', alpha=0.3) ax2.scatter(np.arange(30), np.arange(30) + 3 * np.random.randn(30)) ax3.plot(np.random.randn(50).cumsum(), 'k--') plt.show()
運(yùn)行結(jié)果

函數(shù)解析
| 代碼行 | 作用 | 參考鏈接 |
|---|---|---|
| ax1.hist(np.random.randn(100), bins=20, color=‘k’, alpha=0.3) | 繪制直方圖 | python用hist參數(shù)解讀 |
2. 畫(huà)圖的細(xì)節(jié)修改
依次完成以下的畫(huà)圖效果:

1.一個(gè)正弦函數(shù)和一個(gè)隨機(jī)數(shù)值的曲線,正弦函數(shù)直線,隨機(jī)數(shù)值曲線虛線以及其他樣式修改;
2.圖例、標(biāo)簽等修改;
3.加上標(biāo)注,標(biāo)注范圍內(nèi)用紅色矩形表示。
2.1 plot畫(huà)圖形式修改
代碼
import matplotlib.pyplot as plt
import numpy as np
# 拿起畫(huà)板
fig = plt.figure()
# 貼上畫(huà)紙
ax1 = fig.add_subplot(111)
# 數(shù)據(jù)準(zhǔn)備
x_sin = np.arange(0, 6, 0.001) # [0, 6]
y_sin = np.sin(x_sin)
data_random = np.zeros(7) # 生成[-1,1]的7個(gè)隨機(jī)數(shù)
for i in range(0, 6):
data_random[i] = np.random.uniform(-1, 1)
# 畫(huà)圖
ax1.plot(x_sin, y_sin, linestyle='-', color='g', linewidth=3)
ax1.plot(data_random, linestyle='dashed', color='b', marker='o')
plt.show()
運(yùn)行結(jié)果

2.2 添加圖例、標(biāo)簽等
代碼
import matplotlib.pyplot as plt
import numpy as np
# 拿起畫(huà)板
fig = plt.figure()
# 貼上畫(huà)紙
ax1 = fig.add_subplot(111)
# 數(shù)據(jù)準(zhǔn)備
x_sin = np.arange(0, 6, 0.001) # [0, 6]
y_sin = np.sin(x_sin)
data_random = np.zeros(7) # 生成[-1,1]的7個(gè)隨機(jī)數(shù)
for i in range(0, 6):
data_random[i] = np.random.uniform(-1, 1)
# 畫(huà)圖
ax1.plot(x_sin, y_sin, linestyle='-', color='g', linewidth=3, label='sin')
ax1.plot(data_random, linestyle='dashed', color='b', marker='o', label='random')
#-----------------添加部分------------------
# 添加標(biāo)題
ax1.set_title('Title')
# 添加x軸名稱(chēng)
ax1.set_xlabel('x')
# 設(shè)置x軸坐標(biāo)范圍
ax1.set_xlim(xmin=0, xmax=6)
# 添加圖例,在plot處加上label
ax1.legend(loc='best')
#----------------------------------------
plt.show()
運(yùn)行結(jié)果

2.3 在圖上畫(huà)注解和矩形
代碼
import matplotlib.pyplot as plt
import numpy as np
# 拿起畫(huà)板
fig = plt.figure()
# 貼上畫(huà)紙
ax1 = fig.add_subplot(111)
# 數(shù)據(jù)準(zhǔn)備
x_sin = np.arange(0, 6, 0.001) # [0, 6]
y_sin = np.sin(x_sin)
data_random = np.zeros(7) # 生成[-1,1]的7個(gè)隨機(jī)數(shù)
for i in range(0, 6):
data_random[i] = np.random.uniform(-1, 1)
# 畫(huà)圖
ax1.plot(x_sin, y_sin, linestyle='-', color='g', linewidth=3, label='sin')
ax1.plot(data_random, linestyle='dashed', color='b', marker='o', label='random')
# 添加標(biāo)題
ax1.set_title('Title')
# 添加x軸名稱(chēng)
ax1.set_xlabel('x')
# 設(shè)置x軸坐標(biāo)范圍
ax1.set_xlim(xmin=0, xmax=6)
# 添加圖例
ax1.legend(loc='best')
#-----------------添加部分------------------
# 注解
ax1.annotate('max', xy=((np.pi) / 2, np.sin(np.pi/2)),
xytext=((np.pi) / 2, np.sin(np.pi/2)-0.2),
arrowprops=dict(facecolor='black', headwidth=4, width=2,headlength=4),
horizontalalignment='left', verticalalignment='top')
ax1.annotate('min', xy=((np.pi) * 3 / 2, np.sin(np.pi * 3 / 2)),
xytext=((np.pi) * 3 / 2, np.sin(np.pi * 3 / 2)+0.2),
arrowprops=dict(facecolor='black', headwidth=4, width=2,headlength=4),
horizontalalignment='left', verticalalignment='top')
# 矩形
print(ax1.axis())
rect = plt.Rectangle((np.pi / 2, ax1.axis()[2]), np.pi, ax1.axis()[3] - ax1.axis()[2], color='r', alpha=0.3) # 起始坐標(biāo)點(diǎn),width, height
ax1.add_patch(rect)
#-----------------------------------------
plt.show()
運(yùn)行結(jié)果

3. 圖形保存
plt.savefig('figpath.png', dpi=400)
注意要放在show前面。
完整代碼:
import matplotlib.pyplot as plt
import numpy as np
# 拿起畫(huà)板
fig = plt.figure()
# 貼上畫(huà)紙
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
# 數(shù)據(jù)準(zhǔn)備
x_sin = np.arange(0, 6, 0.001) # [0, 6]
y_sin = np.sin(x_sin)
data_random = np.zeros(7) # 生成[-1,1]的7個(gè)隨機(jī)數(shù)
for i in range(0, 6):
data_random[i] = np.random.uniform(-1, 1)
# 畫(huà)圖
ax1.plot(x_sin, y_sin, linestyle='-', color='g', linewidth=3, label='sin')
ax1.plot(data_random, linestyle='dashed', color='b', marker='o', label='random')
ax2.plot(x_sin, y_sin, linestyle='-', color='g', linewidth=3, label='sin')
ax2.plot(data_random, linestyle='dashed', color='b', marker='o', label='random')
ax3.plot(x_sin, y_sin, linestyle='-', color='g', linewidth=3, label='sin')
ax3.plot(data_random, linestyle='dashed', color='b', marker='o', label='random')
# # 添加標(biāo)題
ax2.set_title('Title')
# 添加x軸名稱(chēng)
ax2.set_xlabel('x')
# 設(shè)置x軸坐標(biāo)范圍
ax2.set_xlim(xmin=0, xmax=6)
# 添加圖例
ax2.legend(loc='best')
ax3.set_title('Title')
# 添加x軸名稱(chēng)
ax3.set_xlabel('x')
# 設(shè)置x軸坐標(biāo)范圍
ax3.set_xlim(xmin=0, xmax=6)
# 添加圖例
ax3.legend(loc='best')
# 注解
ax3.annotate('max', xy=((np.pi) / 2, np.sin(np.pi/2)),
xytext=((np.pi) / 2, np.sin(np.pi/2)-0.2),
arrowprops=dict(facecolor='black', headwidth=4, width=2,headlength=4),
horizontalalignment='left', verticalalignment='top')
ax3.annotate('min', xy=((np.pi) * 3 / 2, np.sin(np.pi * 3 / 2)),
xytext=((np.pi) * 3 / 2, np.sin(np.pi * 3 / 2)+0.2),
arrowprops=dict(facecolor='black', headwidth=4, width=2,headlength=4),
horizontalalignment='left', verticalalignment='top')
# 矩形
# print(ax1.axis())
rect = plt.Rectangle((np.pi / 2, ax3.axis()[2]), np.pi, ax3.axis()[3] - ax3.axis()[2], color='r', alpha=0.3) # 起始坐標(biāo)點(diǎn),width, height
ax3.add_patch(rect)
#-----------------添加部分------------------
plt.savefig('figpath.png', dpi=400)
#------------------------------------------
plt.show()
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
python使用urllib2實(shí)現(xiàn)發(fā)送帶cookie的請(qǐng)求
這篇文章主要介紹了python使用urllib2實(shí)現(xiàn)發(fā)送帶cookie的請(qǐng)求,涉及Python操作cookie的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
Pytorch 如何查看、釋放已關(guān)閉程序占用的GPU資源
這篇文章主要介紹了Pytorch 查看、釋放已關(guān)閉程序占用的GPU資源的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2021-05-05
Python求平面內(nèi)點(diǎn)到直線距離的實(shí)現(xiàn)
今天小編就為大家分享一篇Python求平面內(nèi)點(diǎn)到直線距離的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
python實(shí)現(xiàn)循環(huán)語(yǔ)句1到100累和
這篇文章主要介紹了python循環(huán)語(yǔ)句1到100累和方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
pandas刪除行刪除列增加行增加列的實(shí)現(xiàn)
這篇文章主要介紹了pandas刪除行刪除列增加行增加列的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

