matplotlib階梯圖的實(shí)現(xiàn)(step())
step函數(shù)概述
step函數(shù)用于繪制階梯圖。
根據(jù)源碼可知,step函數(shù)是對plot函數(shù)的輕量級封裝,很多概念和用法與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ù)類似。可選參數(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ù)的效果
通過案例可知,step函數(shù)可以認(rèn)為是plot函數(shù)繪制階梯圖的一個特例。

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階梯圖的實(shí)現(xiàn)(step())的文章就介紹到這了,更多相關(guān)matplotlib 階梯圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python學(xué)習(xí)之使用Matplotlib畫實(shí)時的動態(tài)折線圖的示例代碼
- Matplotlib animation模塊實(shí)現(xiàn)動態(tài)圖
- matplotlib bar()實(shí)現(xiàn)多組數(shù)據(jù)并列柱狀圖通用簡便創(chuàng)建方法
- matplotlib源碼解析標(biāo)題實(shí)現(xiàn)(窗口標(biāo)題,標(biāo)題,子圖標(biāo)題不同之間的差異)
- matplotlib繪制正余弦曲線圖的實(shí)現(xiàn)
- 詳解matplotlib繪圖樣式(style)初探
- matplotlib更改窗口圖標(biāo)的方法示例
- matplotlib繪制多子圖共享鼠標(biāo)光標(biāo)的方法示例
- python使用matplotlib的savefig保存時圖片保存不完整的問題
- matplotlib 畫動態(tài)圖以及plt.ion()和plt.ioff()的使用詳解
- matplotlib制作雷達(dá)圖報(bào)錯ValueError的實(shí)現(xiàn)
相關(guān)文章
python使用fork實(shí)現(xiàn)守護(hù)進(jìn)程的方法
守護(hù)進(jìn)程(Daemon)也稱為精靈進(jìn)程是一種生存期較長的一種進(jìn)程。它們獨(dú)立于控制終端并且周期性的執(zhí)行某種任務(wù)或等待處理某些發(fā)生的事件。他們常常在系統(tǒng)引導(dǎo)裝入時啟動,在系統(tǒng)關(guān)閉時終止。2017-11-11
Python上下文管理器類和上下文管理器裝飾器contextmanager用法實(shí)例分析
這篇文章主要介紹了Python上下文管理器類和上下文管理器裝飾器contextmanager用法,結(jié)合實(shí)例形式分析了上下文管理器類定義、使用、sqlalchemy實(shí)現(xiàn)數(shù)據(jù)庫的自動提交和回滾相關(guān)操作技巧,需要的朋友可以參考下2019-11-11
使用SQLAlchemy操作數(shù)據(jù)庫表過程解析
這篇文章主要介紹了使用SQLAlchemy操作數(shù)據(jù)庫表過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
Python數(shù)據(jù)處理利器Slice函數(shù)用法詳解
這篇文章主要給大家介紹了關(guān)于Python數(shù)據(jù)處理利器Slice函數(shù)用法的相關(guān)資料,slice函數(shù)是Python中的一個內(nèi)置函數(shù),用于對序列進(jìn)行切片操作,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03
python numpy生成等差數(shù)列、等比數(shù)列的實(shí)例
今天小編就為大家分享一篇python numpy生成等差數(shù)列、等比數(shù)列的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
python ceiling divide 除法向上取整(或小數(shù)向上取整)的實(shí)例
今天小編就為大家分享一篇python ceiling divide 除法向上取整 (或小數(shù)向上取整)的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12

