类型错误:barh() 为参数“宽度"获得了多个值 [英] TypeError: barh() got multiple values for argument 'width'

查看:187
本文介绍了类型错误:barh() 为参数“宽度"获得了多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个以前是垂直的图形...我正在尝试使其水平.发生什么情况是我得到一个错误:

I'm trying to make a graph, which was previously vertical... I'm trying to make it horizontal. What happens is that I get an error:

TypeError:barh()为参数"width"获得了多个值.

TypeError: barh () got multiple values for argument 'width'.

有人可以帮忙吗?

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")

推荐答案

有一些错误:

  • 你不能给横条设置宽度,你已经用barh的第二个参数设置了宽度;你需要设置高度
  • 类似地,没有底部,您需要设置左侧
  • 最后,为了让所有东西都定位良好,对于第三个条形,您需要前两个条形的总和:left=bars1+bars2(或对累积宽度使用单独的变量)
  • you can not give the width to the horizontal bars, you already set the width with the second parameter of barh; you need to set the height instead
  • similarly, there is no bottom, you need to set the left
  • finally, to get everything well positioned, for the third bars you need the sum of the previous two: left=bars1+bars2 (or use a separate variable for the cumulated widths)

一些示例代码来展示它是如何工作的:

Some example code to show how it could work:

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()

这篇关于类型错误:barh() 为参数“宽度"获得了多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆