matplotlib阶梯图的实现(step())
作者:mighty13 时间:2023-03-18 23:29:54
step函数概述
step
函数用于绘制阶梯图。
根据源码可知,step
函数是对plot
函数的轻量级封装,很多概念和用法与plot
函数非常相似。
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
函数签名:
matplotlib.pyplot.step(x, y, *args, where='pre', data=None, **kwargs)
step
函数调用签名:
step(x, y, [fmt], *, data=None, where='pre', **kwargs)
step(x, y, [fmt], x2, y2, [fmt2], ..., *, where='pre', **kwargs)
其中:
x:类数组结构,一维x轴坐标序列。一般假设x轴坐标均匀递增。必备参数。
y:类数组结构,一维y轴坐标序列。必备参数。
fmt:格式字符串,与plot函数的fmt参数类似。可选参数。官方建议只设置颜色格式。
data:可索引数据,类似于plot函数。可选参数。
**kwargs:类似于plot函数。
where :设置阶梯所在位置,取值范围为{'pre', 'post', 'mid'},默认值为'pre'。
案例:使用step函数和plot函数演示不同where参数的效果
通过案例可知,step
函数可以认为是plot
函数绘制阶梯图的一个特例。
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()
来源:https://blog.csdn.net/mighty13/article/details/114273271
标签:matplotlib,阶梯图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Django 5种类型Session使用方法解析
2022-08-05 06:46:10
asp数字或者字符排序函数代码
2011-02-24 11:00:00
详解Python中的相对导入和绝对导入
2023-02-05 01:10:36
Laravel框架实现定时发布任务的方法
2023-11-22 23:54:57
看看那些名牌LOGO的成长史
2009-03-24 20:37:00
![](https://img.aspxhome.com/file/UploadPic/20093/24/1406370-34s.jpg)
Python IDLE清空窗口的实例
2023-11-22 17:59:23
详解TensorFlow2实现线性回归
2022-11-10 15:35:13
![](https://img.aspxhome.com/file/2023/2/92512_0s.jpg)
python字符串运算符详情
2023-11-19 20:51:31
![](https://img.aspxhome.com/file/2023/9/64609_0s.png)
Golang中interface的基本用法详解
2023-07-14 17:00:33
![](https://img.aspxhome.com/file/2023/7/99637_0s.png)
python turtle库画一个方格和圆实例
2021-09-25 14:06:10
![](https://img.aspxhome.com/file/2023/9/93799_0s.png)
空行不空格式排版组织原理
2010-05-03 14:46:00
![](https://img.aspxhome.com/file/UploadPic/20105/3/20105314576317s.jpg)
超级连接的title提示中如何换行实现多行显示
2008-03-07 15:57:00
python 生成器协程运算实例
2021-11-22 05:27:51
python使用建议技巧分享(三)
2021-07-07 06:58:16
pandas取dataframe特定行列的实现方法
2022-03-05 15:51:29
![](https://img.aspxhome.com/file/2023/7/93687_0s.png)
Python matplotlib超详细教程实现图形绘制
2023-04-20 06:37:53
![](https://img.aspxhome.com/file/2023/7/81657_0s.jpg)
Python3使用xlrd、xlwt处理Excel方法数据
2023-04-03 03:25:29
![](https://img.aspxhome.com/file/2023/6/82766_0s.png)
一篇文章带你入门Python正则表达式
2021-11-29 03:00:56
![](https://img.aspxhome.com/file/2023/6/70126_0s.jpg)
发现一个不错的11px字体:PMingLiu
2008-09-06 12:49:00
快速升级MySQL系统表
2009-01-23 12:35:00