我想演示用分段常数阶跃函数逼近连续函数的积分.

生成的绘图应如下所示:

enter image description here

我有一个工件常数函数,我的问题是我不知道如何绘制它,因为典型的候选者似乎不起作用:

它看起来类似于直方图,但生成方式非常不同.

从我所看到的情况来看,条形图与数字线不一致.

The plt.step() method on the other hand does not include the bins/bars; using it I got this so far: enter image description here

使用此代码

kwargs = dict(drawstyle = 'steps-mid')

plt.plot(times, f(times),  '-')
plt.plot(times, fitted_values, **kwargs)

是否有一个专门的函数,或我忽略的一些kwargs参数,可以在这里绘制出我需要的内容?

Edit: Thank you for the answer @Stef ! I just tried this solution and recognized an issue with a bar-plot here. plt generates a bar for every value in the times array. Now I get this result: enter image description here

推荐答案

您可以将baralign参数一起使用:

import numpy as np

x = np.linspace(0, 1, 11)
y = x**2 + 1
plt.plot(x, y, 'r-')
plt.bar(x, y, width=0.1, align='edge', fc='lightgreen', ec='k')
plt.xlim(0, 1)

enter image description here

Python相关问答推荐

从不规则形状区域返回使用openCV跟踪的对象的着陆位置

将数组操作转化为纯numpy方法

如何分割我的收件箱,以便连续的数字各自位于自己的收件箱中?

Django关于UniqueBindition的更新

在Docker中运行HAProxy时无法获得503服务

如何从格式为note:{neighbor:weight}的字典中构建networkx图?

从 struct 类型创建MultiPolygon对象,并使用Polars列出[list[f64]列

Python中的函数中是否有充分的理由接受float而不接受int?

通过交换 node 对链接列表进行 Select 排序

scikit-learn导入无法导入名称METRIC_MAPPING64'

' osmnx.shortest_track '返回有效源 node 和目标 node 的'无'

通过pandas向每个非空单元格添加子字符串

如何在Python中找到线性依赖mod 2

转换为浮点,pandas字符串列,混合千和十进制分隔符

Python中的变量每次增加超过1

为什么numpy. vectorize调用vectorized函数的次数比vector中的元素要多?

以逻辑方式获取自己的pyproject.toml依赖项

Pandas:计算中间时间条目的总时间增量

Python—转换日期:价目表到新行

如何检测鼠标/键盘的空闲时间,而不是其他输入设备?