1 回答

TA贡献1779条经验 获得超6个赞
诀窍是使用left指定栏应该从哪里开始,然后width为您的栏传递一个负数,使其从右向左延伸。由于窗口的右侧也会随着您的数据而变化,您可能还想设置某种参数,即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()
添加回答
举报