如何在 python 中将条与功能线对齐

How to align bars to function line in python

我想在 mat plot lib 中创建一个条形图,其中条形图的左上角与功能线接触,如下图所示。我将如何更改下面的代码以实现此目的。为了这个例子,假设 plt.plot() 行是函数行。

import math
import matplotlib.pyplot as plt
xVal = []
yVal = []
for i in range(0, 10):
    xVal.append((i/10))
    yVal.append((2*i*math.pi)/10)

plt.bar(x=xVal, height=yVal, width=0.1, align='edge')
plt.plot(xVal, yVal)
plt.show()

您可以将 x-coordinate 移动 0.1。

例如:

import math
import matplotlib.pyplot as plt
xVal = []
yVal = []
for i in range(0, 10):
    xVal.append((i/10))
    yVal.append((2*i*math.pi)/10)

plt.plot(xVal, yVal)
x = lambda a: a + 0.1
xVal = [x(i) for i in xVal]
plt.bar(x=xVal, height=yVal, width=-0.1, align='edge')
plt.show()