最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python數(shù)據(jù)可視化的那些操作你了解嗎

 更新時(shí)間:2022年01月26日 16:09:18   作者:橙橙小貍貓  
這篇文章主要為大家詳細(xì)介紹了python數(shù)據(jù)可視化操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助

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使用urllib2實(shí)現(xiàn)發(fā)送帶cookie的請(qǐng)求,涉及Python操作cookie的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • Sphinx生成python文檔示例圖文解析

    Sphinx生成python文檔示例圖文解析

    這篇文章主要介為大家紹了Sphinx生成python文檔示例圖文解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-04-04
  • python怎么使用xlwt操作excel你知道嗎

    python怎么使用xlwt操作excel你知道嗎

    這篇文章主要為大家介紹了python使用xlwt操作excel的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01
  • Pytorch 如何查看、釋放已關(guān)閉程序占用的GPU資源

    Pytorch 如何查看、釋放已關(guān)閉程序占用的GPU資源

    這篇文章主要介紹了Pytorch 查看、釋放已關(guān)閉程序占用的GPU資源的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。
    2021-05-05
  • python中remove函數(shù)的踩坑記錄

    python中remove函數(shù)的踩坑記錄

    這篇文章主要給大家介紹了關(guān)于python中remove函數(shù)的踩坑記錄,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Python求平面內(nèi)點(diǎn)到直線距離的實(shí)現(xiàn)

    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實(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)

    這篇文章主要介紹了pandas刪除行刪除列增加行增加列的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • python判斷集合的超集方法及實(shí)例

    python判斷集合的超集方法及實(shí)例

    在本篇內(nèi)容里小編給大家分享的是一篇關(guān)于python判斷集合的超集方法及實(shí)例內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2021-05-05
  • python查找指定具有相同內(nèi)容文件的方法

    python查找指定具有相同內(nèi)容文件的方法

    這篇文章主要介紹了python查找指定具有相同內(nèi)容文件的方法,涉及Python針對(duì)文件操作的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06

最新評(píng)論

安塞县| 卓资县| 鄂温| 蓬莱市| 镇坪县| 剑河县| 镇安县| 徐州市| 彭州市| 葵青区| 平原县| 紫阳县| 兰西县| 梓潼县| 永清县| 黄冈市| 台江县| 房山区| 会泽县| 呼伦贝尔市| 巨鹿县| 大厂| 长岭县| 平和县| 红桥区| 瓦房店市| 林西县| 保德县| 西乌珠穆沁旗| 杭州市| 嵩明县| 吉安县| 哈尔滨市| 崇礼县| 玛多县| 黑水县| 龙游县| 富顺县| 公主岭市| 永清县| 平山县|