1 回答

TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊
訣竅是使用left指定欄應(yīng)該從哪里開始,然后width為您的欄傳遞一個(gè)負(fù)數(shù),使其從右向左延伸。由于窗口的右側(cè)也會(huì)隨著您的數(shù)據(jù)而變化,您可能還想設(shè)置某種參數(shù),即x_max:
import numpy as np
import matplotlib.pyplot as plt
x_max = 30
plt.figure(figsize=(9.5, 2.7))
# Create horizontal bars
plt.barh(0, 18,height=0.2,facecolor='orange',edgecolor='black',linewidth=2)
plt.errorbar(x=[18], y=[0], xerr=[2],color='black',fmt='none',linewidth=5,zorder=4)
# new code: use left to specify the start position, then make its width negative
# to extend right to left
plt.barh(0, -5, height=0.2, left=x_max, facecolor='red',edgecolor='black',linewidth=2)
# place error bars the same as you did for the above.
plt.errorbar(x=[x_max - 5], y=[0], xerr=[2],color='black',fmt='none',linewidth=5,zorder=4)
plt.xticks(np.arange(10, 30+1, 1.0),fontsize=14)
plt.yticks([])
plt.xlim(10, x_max)
plt.ylim(-.13, .13)
plt.show()
添加回答
舉報(bào)