欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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軸名稱
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軸名稱
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軸名稱
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é)

本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!   

相關(guān)文章

  • tensorflow實(shí)現(xiàn)在函數(shù)中用tf.Print輸出中間值

    tensorflow實(shí)現(xiàn)在函數(shù)中用tf.Print輸出中間值

    今天小編就為大家分享一篇tensorflow實(shí)現(xiàn)在函數(shù)中用tf.Print輸出中間值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • Python?字符串使用多個(gè)分隔符分割成列表的2種方法

    Python?字符串使用多個(gè)分隔符分割成列表的2種方法

    本文主要介紹了Python?字符串使用多個(gè)分隔符分割成列表,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Python中SQLite數(shù)據(jù)庫(kù)的使用

    Python中SQLite數(shù)據(jù)庫(kù)的使用

    SQLite是一種輕型關(guān)系型數(shù)據(jù)庫(kù),常用于嵌入式設(shè)備和移動(dòng)應(yīng)用中。Python中內(nèi)置了SQLite模塊,可用于連接和操作SQLite數(shù)據(jù)庫(kù)。通過(guò)Python SQLite模塊,可以方便地創(chuàng)建、查詢和修改數(shù)據(jù)庫(kù)中的數(shù)據(jù),支持事務(wù)處理和數(shù)據(jù)庫(kù)操作的原子性保證
    2023-04-04
  • Python實(shí)現(xiàn)將Unicode轉(zhuǎn)換為ASCII

    Python實(shí)現(xiàn)將Unicode轉(zhuǎn)換為ASCII

    這篇文章主要為大家詳細(xì)介紹了系統(tǒng)編碼的不同方法以及如何利用Python實(shí)現(xiàn)將Unicode轉(zhuǎn)換為?ASCII,文中的示例代碼講解詳細(xì),有需要的小伙伴可以學(xué)習(xí)一下
    2023-10-10
  • Pygame鼠標(biāo)進(jìn)行圖片的移動(dòng)與縮放案例詳解

    Pygame鼠標(biāo)進(jìn)行圖片的移動(dòng)與縮放案例詳解

    pygame是Python的第三方庫(kù),里面提供了使用Python開(kāi)發(fā)游戲的基礎(chǔ)包。本文將介紹如何通過(guò)Pygame實(shí)現(xiàn)鼠標(biāo)進(jìn)行圖片的移動(dòng)與縮放,感興趣的可以關(guān)注一下
    2021-12-12
  • Python自動(dòng)化辦公之Word文件內(nèi)容的讀取

    Python自動(dòng)化辦公之Word文件內(nèi)容的讀取

    word、excel、PPT,雖然說(shuō)是特殊文件,其實(shí)也是實(shí)際工作中我們經(jīng)常會(huì)用到的文件類(lèi)型。本文將為大家詳解Python讀取Word文件和文件內(nèi)容的方法,感興趣的可以了解一下
    2022-05-05
  • 基于python實(shí)現(xiàn)計(jì)算且附帶進(jìn)度條代碼實(shí)例

    基于python實(shí)現(xiàn)計(jì)算且附帶進(jìn)度條代碼實(shí)例

    這篇文章主要介紹了基于python實(shí)現(xiàn)計(jì)算且附帶進(jìn)度條代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 在PyTorch中Tensor的查找和篩選例子

    在PyTorch中Tensor的查找和篩選例子

    今天小編就為大家分享一篇在PyTorch中Tensor的查找和篩選例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-08-08
  • python數(shù)據(jù)結(jié)構(gòu)的排序算法

    python數(shù)據(jù)結(jié)構(gòu)的排序算法

    下面是是對(duì)python數(shù)據(jù)結(jié)構(gòu)的排序算法的一些講解及示意圖,感興趣的小伙伴一起來(lái)學(xué)習(xí)吧
    2021-08-08
  • python實(shí)現(xiàn)超級(jí)馬里奧

    python實(shí)現(xiàn)超級(jí)馬里奧

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)超級(jí)馬里奧,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03

最新評(píng)論