类型错误:barh() 获得参数“宽度”的多个值

Pat*_*pes 1 python matplotlib

我正在尝试制作一个图表,该图表以前是垂直的......我正在尝试使其水平。发生的情况是我收到错误:

类型错误: barh () 获得参数“宽度”的多个值。

有人可以帮忙吗?

barWidth = 1
plt.barh(r, bars1, color='#7f6d5f', edgecolor='white', width=barWidth, label="Lasso")
plt.barh(r, bars2, bottom=bars1, color='#557f2d', edgecolor='white', width=barWidth, label="Random Forest")
plt.barh(r, bars3, bottom=bars, color='#2d7f5e', edgecolor='white', width=barWidth, label="Decision Tree")
Run Code Online (Sandbox Code Playgroud)

Joh*_*anC 7

有几点是错误的:

  • 您不能指定水平条的宽度,您已经使用第二个参数设置了宽度barh;你需要设置高度
  • 同样,没有底部,需要设置左侧
  • 最后,为了让所有东西都定位良好,对于第三个条形,您需要前两个条形的总和:(left=bars1+bars2或使用单独的变量作为累积宽度)

一些示例代码展示了它如何工作:

from matplotlib import pyplot as plt
import numpy as np

N = 5
r = range(N)
bars1 = np.random.binomial(20, .7, N)
bars2 = np.random.binomial(20, .5, N)
bars3 = np.random.binomial(20, .4, N)

colors = ['#7f6d5f', '#557f2d', '#2d7f5e']
labels = ["Lasso", "Random Forest", "Decision Tree"]

barWidth = 1
lefts = 0
for bars, col, label in zip([bars1, bars2, bars3], colors, labels):
    plt.barh(r, bars, left=lefts, color=col, edgecolor='white', height=barWidth, label=label)
    lefts += bars
plt.legend()
plt.ylim(-0.5, len(bars) - 0.5)
plt.show()
Run Code Online (Sandbox Code Playgroud)

样本