Хитрость заключается в том, чтобы использовать 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()