一般来说,如果您显示的两个变量位于不同的单位或具有不同的范围,那么您将需要使用两个具有共享y轴的子图.这类似于@regdoug的答案,但最好明确地共享y轴以确保数据保持对齐(例如,尝试使用此示例进行缩放/平移).
例如:
import matplotlib.pyplot as plt
y = range(20)
x1 = range(20)
x2 = range(0,200,10)
fig,axes = plt.subplots(ncols=2,sharey=True)
axes[0].barh(y,x1,align='center',color='gray')
axes[1].barh(y,x2,color='gray')
axes[0].invert_xaxis()
plt.show()
如果你想更精确地重现你链接到的问题中显示的例子(我将离开灰色背景和白色网格,但如果你喜欢它们,那么很容易添加):
import numpy as np
import matplotlib.pyplot as plt
# Data
states = ["AK","OK"]
staff = np.array([20,32])
sales = staff * (20 + 10 * np.random.random(staff.size))
# Sort by number of sales staff
idx = staff.argsort()
states,staff,sales = [np.take(x,idx) for x in [states,sales]]
y = np.arange(sales.s