溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

matplot繪圖

發(fā)布時(shí)間:2020-06-24 18:28:53 來源:網(wǎng)絡(luò) 閱讀:390 作者:Tobey_51 欄目:大數(shù)據(jù)

Matplotlib

Matplotlib 是一個 Python 的 2D繪圖庫,通過 Matplotlib,開發(fā)者可以僅需要幾行代碼,便可以生成繪圖,直方圖,功率譜,條形圖,錯誤圖,散點(diǎn)圖等。

http://matplotlib.org

  • 用于創(chuàng)建出版質(zhì)量圖表的繪圖工具庫
  • 目的是為Python構(gòu)建一個Matlab式的繪圖接口
  • import matplotlib.pyplot as plt
  • pyplot模塊包含了常用的matplotlib API函數(shù)

figure

  • Matplotlib的圖像均位于figure對象中
  • 創(chuàng)建figure:fig = plt.figure()

示例代碼:

# 引入matplotlib包
import matplotlib.pyplot as plt
import numpy as np

%matplotlib inline #在jupyter notebook 里需要使用這一句命令

# 創(chuàng)建figure對象
fig = plt.figure()

運(yùn)行結(jié)果:會彈出一個figure窗口,如下圖所示
matplot繪圖

subplot

fig.add_subplot(a, b, c)

  • a,b 表示將fig分割成 a*b 的區(qū)域
  • c 表示當(dāng)前選中要操作的區(qū)域,
  • 注意:從1開始編號(不是從0開始)
  • plot 繪圖的區(qū)域是最后一次指定subplot的位置 (jupyter notebook里不能正確顯示)

示例代碼:

# 指定切分區(qū)域的位置
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax4 = fig.add_subplot(2,2,4)

# 在subplot上作圖
random_arr = np.random.randn(100)
#print random_arr

# 默認(rèn)是在最后一次使用subplot的位置上作圖,但是在jupyter notebook 里可能顯示有誤
plt.plot(random_arr)

# 可以指定在某個或多個subplot位置上作圖
# ax1 = fig.plot(random_arr)
# ax2 = fig.plot(random_arr)
# ax3 = fig.plot(random_arr)

# 顯示繪圖結(jié)果
plt.show()

運(yùn)行結(jié)果:僅右下角有圖
matplot繪圖

直方圖:hist

示例代碼:

import matplotlib.pyplot as plt
import numpy as np

plt.hist(np.random.randn(100), bins=10, color='b', alpha=0.3)
plt.show()

matplot繪圖

散點(diǎn)圖:scatter

示例代碼:

import matplotlib.pyplot as plt
import numpy as np

# 繪制散點(diǎn)圖
x = np.arange(50)
y = x + 5 * np.random.rand(50)
plt.scatter(x, y)
plt.show()

matplot繪圖

柱狀圖:bar

示例代碼:

import matplotlib.pyplot as plt
import numpy as np

# 柱狀圖
x = np.arange(5)
y1, y2 = np.random.randint(1, 25, size=(2, 5))
width = 0.25
ax = plt.subplot(1,1,1)
ax.bar(x, y1, width, color='r')
ax.bar(x+width, y2, width, color='g')
ax.set_xticks(x+width)
ax.set_xticklabels(['a', 'b', 'c', 'd', 'e'])
plt.show()

matplot繪圖
matplot繪圖

矩陣?yán)L圖:plt.imshow()

  • 混淆矩陣,三個維度的關(guān)系

示例代碼:

import matplotlib.pyplot as plt
import numpy as np

# 矩陣?yán)L圖
m = np.random.rand(10,10)
print(m)
plt.imshow(m, interpolation='nearest', cmap=plt.cm.ocean)
plt.colorbar()
plt.show()

plt.subplots()

  • 同時(shí)返回新創(chuàng)建的figure和subplot對象數(shù)組
  • 生成2行2列subplot:fig, subplot_arr = plt.subplots(2,2)
  • 在jupyter里可以正常顯示,推薦使用這種方式創(chuàng)建多個圖表

示例代碼:

import matplotlib.pyplot as plt
import numpy as np

fig, subplot_arr = plt.subplots(2,2)
# bins 為顯示個數(shù),一般小于等于數(shù)值個數(shù)
subplot_arr[1,0].hist(np.random.randn(100), bins=10, color='b', alpha=0.3)
plt.show()

運(yùn)行結(jié)果:左下角繪圖
matplot繪圖

顏色、標(biāo)記、線型

  • ax.plot(x, y, ‘r--’)

    等價(jià)于ax.plot(x, y, linestyle=‘--’, color=‘r’)

示例代碼:

import matplotlib.pyplot as plt
import numpy as np

fig, axes = plt.subplots(2)
axes[0].plot(np.random.randint(0, 100, 50), 'ro--')
# 等價(jià)
axes[1].plot(np.random.randint(0, 100, 50), color='r', linestyle='dashed', marker='o')

matplot繪圖

  • 常用的顏色、標(biāo)記、線型
    顏色
    • b: blue
    • g: grean
    • r: red
    • c: cyan
    • m: magenta
    • y: yellow
    • k: black
    • w: white

標(biāo)記

    • .: point
    • ,: pixel
    • o: circle
    • v: triangle_down
    • ^: triangle_up
    • <: tiiangle_left

線型

    • '-' or 'solid': solid lint
    • '--' or 'dashed': dashed line
    • '-.' or 'dashdot': dash-dotted line
    • ':' or 'dotted': dotted line
    • 'None': draw nothing
    • ' ': draw nothing
    • '': draw nothing

刻度、標(biāo)簽、圖例

  • 設(shè)置刻度范圍

    plt.xlim(), plt.ylim()

    ax.set_xlim(), ax.set_ylim()

  • 設(shè)置顯示的刻度

    plt.xticks(), plt.yticks()

    ax.set_xticks(), ax.set_yticks()

  • 設(shè)置刻度標(biāo)簽

    ax.set_xticklabels(), ax.set_yticklabels()

  • 設(shè)置坐標(biāo)軸標(biāo)簽

    ax.set_xlabel(), ax.set_ylabel()

  • 設(shè)置標(biāo)題

    ax.set_title()

  • 圖例

    ax.plot(label=‘legend’)

    ax.legend(), plt.legend()
    loc=‘best’:自動選擇放置圖例最佳位置

示例代碼:

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(1)
ax.plot(np.random.randn(1000).cumsum(), label='line0')

# 設(shè)置刻度
#plt.xlim([0,500])
ax.set_xlim([0, 800])

# 設(shè)置顯示的刻度
#plt.xticks([0,500])
ax.set_xticks(range(0,500,100))

# 設(shè)置刻度標(biāo)簽
ax.set_yticklabels(['Jan', 'Feb', 'Mar'])

# 設(shè)置坐標(biāo)軸標(biāo)簽
ax.set_xlabel('Number')
ax.set_ylabel('Month')

# 設(shè)置標(biāo)題
ax.set_title('Example')

# 圖例
ax.plot(np.random.randn(1000).cumsum(), label='line1')
ax.plot(np.random.randn(1000).cumsum(), label='line2')
ax.legend()
ax.legend(loc='best')
#plt.legend()

matplot繪圖

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI