Python 将水平条形图中的yticklabels居中-matplotlib 有没有办法将YTICK标签放在一个既有负值又有正值的水平条形图中间?我希望存储在一个名为“名称”的列表中的各种Y值的名称出现在以x=0为中心的条形图的中间。这可能吗

Python 将水平条形图中的yticklabels居中-matplotlib 有没有办法将YTICK标签放在一个既有负值又有正值的水平条形图中间?我希望存储在一个名为“名称”的列表中的各种Y值的名称出现在以x=0为中心的条形图的中间。这可能吗,python,matplotlib,Python,Matplotlib,演示代码: names = [1, 2, 3] fig, axs = plt.subplots(3,1, figsize=(12, 24)) ind = np.arange(len(names)) # the x locations for the groups width = 0.35 # the width of the bars axs[0].barh(ind, [3, 4, 5], width, color='red', label='demo') axs[0].set(

演示代码:

names = [1, 2, 3]
fig, axs = plt.subplots(3,1, figsize=(12, 24)) 
ind = np.arange(len(names))  # the x locations for the groups
width = 0.35       # the width of the bars
axs[0].barh(ind, [3, 4, 5], width, color='red', label='demo')
axs[0].set(yticks=ind + width, yticklabels=names)

plt.show()

你很接近。您只需使用align=center,然后按原样重命名记号标签,而无需将记号移动0.5。我将coef_名称替换为name,因为您提供的代码中没有定义前者

names = [1, 2, 3]
fig, axs = plt.subplots(3,1, figsize=(12, 24)) 
ind = np.arange(len(names))  # the x locations for the groups
width = 0.35       # the width of the bars
axs[0].barh(ind, [3, 4, 5], width, color='red', align='center',label='demo')
axs[0].set(yticks=ind , yticklabels=names)