溫馨提示×

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

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

使用matplotlib怎么繪制一個(gè)階梯圖

發(fā)布時(shí)間:2021-03-02 16:35:53 來(lái)源:億速云 閱讀:318 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)使用matplotlib怎么繪制一個(gè)階梯圖,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

step函數(shù)概述

step函數(shù)用于繪制階梯圖。

根據(jù)源碼可知,step函數(shù)是對(duì)plot函數(shù)的輕量級(jí)封裝,很多概念和用法與plot函數(shù)非常相似。

def step(self, x, y, *args, where='pre', data=None, **kwargs):
 cbook._check_in_list(('pre', 'post', 'mid'), where=where)
 kwargs['drawstyle'] = 'steps-' + where
 return self.plot(x, y, *args, data=data, **kwargs)

step函數(shù)簽名:

matplotlib.pyplot.step(x, y, *args, where='pre', data=None, **kwargs)

step函數(shù)調(diào)用簽名:

step(x, y, [fmt], *, data=None, where='pre', **kwargs)
step(x, y, [fmt], x2, y2, [fmt2], ..., *, where='pre', **kwargs)

其中:

  • x:類數(shù)組結(jié)構(gòu),一維x軸坐標(biāo)序列。一般假設(shè)x軸坐標(biāo)均勻遞增。必備參數(shù)。

  • y:類數(shù)組結(jié)構(gòu),一維y軸坐標(biāo)序列。必備參數(shù)。

  • fmt:格式字符串,與plot函數(shù)的fmt參數(shù)類似??蛇x參數(shù)。官方建議只設(shè)置顏色格式。

  • data:可索引數(shù)據(jù),類似于plot函數(shù)。可選參數(shù)。

  • **kwargs:類似于plot函數(shù)。

  • where :設(shè)置階梯所在位置,取值范圍為{'pre', 'post', 'mid'},默認(rèn)值為'pre'。

 案例:使用step函數(shù)和plot函數(shù)演示不同where參數(shù)的效果

通過(guò)案例可知,step函數(shù)可以認(rèn)為是plot函數(shù)繪制階梯圖的一個(gè)特例。

使用matplotlib怎么繪制一個(gè)階梯圖

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(14)
y = np.sin(x / 2)

plt.figure(figsize=(12,5))
plt.subplot(121)
plt.step(x, y + 2, label='pre (default)')
plt.plot(x, y + 2, 'o--', color='grey', alpha=0.3)

plt.step(x, y + 1, where='mid', label='mid')
plt.plot(x, y + 1, 'o--', color='grey', alpha=0.3)

plt.step(x, y, where='post', label='post')
plt.plot(x, y, 'o--', color='grey', alpha=0.3)

plt.grid(axis='x', color='0.95')
plt.legend(title='Parameter where:')
plt.title('plt.step(where=...)')

plt.subplot(122)
plt.plot(x, y + 2, drawstyle='steps', label='steps (=steps-pre)')
plt.plot(x, y + 2, 'o--', color='grey', alpha=0.3)

plt.plot(x, y + 1, drawstyle='steps-mid', label='steps-mid')
plt.plot(x, y + 1, 'o--', color='grey', alpha=0.3)

plt.plot(x, y, drawstyle='steps-post', label='steps-post')
plt.plot(x, y, 'o--', color='grey', alpha=0.3)

plt.grid(axis='x', color='0.95')
plt.legend(title='Parameter drawstyle:')
plt.title('plt.plot(drawstyle=...)')
plt.show()

關(guān)于使用matplotlib怎么繪制一個(gè)階梯圖就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

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

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

AI