Home > Blockchain >  How to align bars to function line in python
How to align bars to function line in python

Time:04-19

I'm looking to create a bar graph in mat plot lib where the top left corner of the bar touches the function line as seen in the below image. How would I change the code below to achieve this. For sake of this example lets say that the plt.plot() line is the function line.

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()

enter image description here

CodePudding user response:

You could move your x-coordinate by 0.1.

For example:

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()
  • Related