'How to align bars to function line in python
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()
Solution 1:[1]
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mkrieger1 |

