Python 使用matplotlib plt.barh时,invert_xaxis会出错

Python 使用matplotlib plt.barh时,invert_xaxis会出错,python,matplotlib,bar-chart,Python,Matplotlib,Bar Chart,我正在尝试绘制双向条形图。我想反转 x1 < /代码>的x轴,使0位于两者的中间。我不断得到错误: AttributeError:“BarContainer”对象没有属性“invert_xaxis” 这是我的密码: import matplotlib.pyplot as plt y = ['F','M','H'] x1 = [8, 4, 3] x2 = [2, 4, 7] fig, axes = plt.subplots(ncols=2, sharey=True) axes[0] = plt.b

我正在尝试绘制双向条形图。我想反转<代码> x1 < /代码>的x轴,使0位于两者的中间。我不断得到错误:

AttributeError:“BarContainer”对象没有属性“invert_xaxis”

这是我的密码:

import matplotlib.pyplot as plt
y = ['F','M','H']
x1 = [8, 4, 3]
x2 = [2, 4, 7]
fig, axes = plt.subplots(ncols=2, sharey=True)
axes[0] = plt.barh(y, x1, align='center', color='b')
axes[1] = plt.barh(y, x2, align='center', color='r')
axes[0].invert_xaxis()
plt.show()

问题是将打印指定给两个轴对象,而不是使用它们进行打印。正确的方法是直接使用axis对象来绘制
barh
。然后事情就会按预期的那样进行

import matplotlib.pyplot as plt

y = ['F','M','H']
x1 = [8, 4, 3]
x2 = [2, 4, 7]
fig, axes = plt.subplots(ncols=2, sharey=True)
axes[0].barh(y, x1, align='center', color='b') # <---- Changed here
axes[1].barh(y, x2, align='center', color='r') # <---- Changed here
axes[0].invert_xaxis()
plt.show()
导入matplotlib.pyplot作为plt
y=['F','M','H']
x1=[8,4,3]
x2=[2,4,7]
图,轴=plt子批次(ncols=2,sharey=True)

轴[0]。barh(y,x1,align='center',color='b')#问题是您将打印指定给两个轴对象,而不是使用它们进行打印。正确的方法是直接使用axis对象来绘制
barh
。然后事情就会按预期的那样进行

import matplotlib.pyplot as plt

y = ['F','M','H']
x1 = [8, 4, 3]
x2 = [2, 4, 7]
fig, axes = plt.subplots(ncols=2, sharey=True)
axes[0].barh(y, x1, align='center', color='b') # <---- Changed here
axes[1].barh(y, x2, align='center', color='r') # <---- Changed here
axes[0].invert_xaxis()
plt.show()
导入matplotlib.pyplot作为plt
y=['F','M','H']
x1=[8,4,3]
x2=[2,4,7]
图,轴=plt子批次(ncols=2,sharey=True)

轴[0]。barh(y,x1,align='center',color='b')#@AlexandraSilva:不客气。感谢您提供完整的example@AlexandraSilva:不客气。感谢您提供完整的示例