python數(shù)據(jù)可視化的那些操作你了解嗎
0. 前言
數(shù)據(jù)處理過程中,可視化可以更直觀得感受數(shù)據(jù),因此打算結(jié)合自己的一些實(shí)踐經(jīng)理,以效果為準(zhǔn)寫這篇博客。內(nèi)容應(yīng)該會(huì)不斷擴(kuò)充。
1. matplotlib中figure、subplot和plot等什么關(guān)系
記住這幾個(gè)關(guān)系可以結(jié)合實(shí)際。假設(shè)你去外面寫生要帶哪些工具呢,包括畫板、畫紙還有畫筆,那么就可以一一對應(yīng)了。
函數(shù) | 工具 |
---|---|
figure | 畫板 |
subplot、add_subplot | 畫紙 |
plot、hist、scatter | 畫筆 |
那么再往深處想,畫紙貼在畫板上,畫紙可以裁剪成多塊布局在畫板上,而畫筆只能畫在紙上,可能這樣講有點(diǎn)籠統(tǒng),下面一個(gè)代碼配合注釋就可以清晰明白啦。(感覺需要記住以下代碼)
代碼
import matplotlib.pyplot as plt import numpy as np # 拿起畫板 fig = plt.figure() # 在畫板上貼上畫紙 ax1 = fig.add_subplot(221) ax2 = fig.add_subplot(222) ax3 = fig.add_subplot(223) # 一步完成(直接拿起畫板和畫紙)----------------- # ax1 = plt.subplot(221) # ax2 = plt.subplot(222) # ax3 = plt.subplot(223) # ---------------------------------------- # 在畫紙上作圖 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. 畫圖的細(xì)節(jié)修改
依次完成以下的畫圖效果:
1.一個(gè)正弦函數(shù)和一個(gè)隨機(jī)數(shù)值的曲線,正弦函數(shù)直線,隨機(jī)數(shù)值曲線虛線以及其他樣式修改;
2.圖例、標(biāo)簽等修改;
3.加上標(biāo)注,標(biāo)注范圍內(nèi)用紅色矩形表示。
2.1 plot畫圖形式修改
代碼
import matplotlib.pyplot as plt import numpy as np # 拿起畫板 fig = plt.figure() # 貼上畫紙 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) # 畫圖 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 # 拿起畫板 fig = plt.figure() # 貼上畫紙 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) # 畫圖 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軸名稱 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 在圖上畫注解和矩形
代碼
import matplotlib.pyplot as plt import numpy as np # 拿起畫板 fig = plt.figure() # 貼上畫紙 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) # 畫圖 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軸名稱 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 # 拿起畫板 fig = plt.figure() # 貼上畫紙 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) # 畫圖 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軸名稱 ax2.set_xlabel('x') # 設(shè)置x軸坐標(biāo)范圍 ax2.set_xlim(xmin=0, xmax=6) # 添加圖例 ax2.legend(loc='best') ax3.set_title('Title') # 添加x軸名稱 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é)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
tensorflow實(shí)現(xiàn)在函數(shù)中用tf.Print輸出中間值
今天小編就為大家分享一篇tensorflow實(shí)現(xiàn)在函數(shù)中用tf.Print輸出中間值,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01Python?字符串使用多個(gè)分隔符分割成列表的2種方法
本文主要介紹了Python?字符串使用多個(gè)分隔符分割成列表,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04Python實(shí)現(xiàn)將Unicode轉(zhuǎn)換為ASCII
這篇文章主要為大家詳細(xì)介紹了系統(tǒng)編碼的不同方法以及如何利用Python實(shí)現(xiàn)將Unicode轉(zhuǎn)換為?ASCII,文中的示例代碼講解詳細(xì),有需要的小伙伴可以學(xué)習(xí)一下2023-10-10Pygame鼠標(biāo)進(jìn)行圖片的移動(dòng)與縮放案例詳解
pygame是Python的第三方庫,里面提供了使用Python開發(fā)游戲的基礎(chǔ)包。本文將介紹如何通過Pygame實(shí)現(xiàn)鼠標(biāo)進(jìn)行圖片的移動(dòng)與縮放,感興趣的可以關(guān)注一下2021-12-12Python自動(dòng)化辦公之Word文件內(nèi)容的讀取
word、excel、PPT,雖然說是特殊文件,其實(shí)也是實(shí)際工作中我們經(jīng)常會(huì)用到的文件類型。本文將為大家詳解Python讀取Word文件和文件內(nèi)容的方法,感興趣的可以了解一下2022-05-05基于python實(shí)現(xiàn)計(jì)算且附帶進(jìn)度條代碼實(shí)例
這篇文章主要介紹了基于python實(shí)現(xiàn)計(jì)算且附帶進(jìn)度條代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03python數(shù)據(jù)結(jié)構(gòu)的排序算法
下面是是對python數(shù)據(jù)結(jié)構(gòu)的排序算法的一些講解及示意圖,感興趣的小伙伴一起來學(xué)習(xí)吧2021-08-08