Is it possible to do something as follow (add blue line) ?
The aim is to add a line at the 50% level. The line should be the same length as the bar.
Code :
JavaScript
x
2
1
MyDataFrame.plot(kind='bar', stacked=True, color=['red', 'skyblue', 'green'],ax=Fig1)
2
Advertisement
Answer
You can use hlines
. I didn’t know how to set percentage on y axis, hope it will still be useful.
JavaScript
1
14
14
1
import pandas as pd
2
import matplotlib.pyplot as plt
3
4
df = pd.DataFrame({"test": [1], "test1": [3], "test2": [3]}, columns=["test", "test1", "test2"])
5
6
axs = plt.gca()
7
axs.set_ylim(0,20)
8
9
df.plot(kind='bar', stacked=True, color=['red', 'skyblue', 'green'], ax=axs)
10
11
axs.hlines(y=15, xmin=-0.25, xmax=0.25, linewidth=2, color='b')
12
13
plt.show()
14
EDIT:
If you want a line above each bar, do something like this:
JavaScript
1
18
18
1
import pandas as pd
2
import matplotlib.pyplot as plt
3
4
df = pd.DataFrame({"test": [1, 2], "test1": [3, 2], "test2": [3, 2]}, columns=["test", "test1", "test2"])
5
6
axs = plt.gca()
7
axs.set_ylim(0,20)
8
9
df.plot(kind='bar', stacked=True, color=['red', 'skyblue', 'green'], ax=axs)
10
11
line_heights = [15, 17]
12
13
for i in range(len(line_heights)):
14
15
axs.hlines(y=line_heights[i], xmin= i - 0.25, xmax= i + 0.25, linewidth=2, color='b')
16
17
plt.show()
18