python - 已知 x、y 的条形图

标签 python numpy matplotlib

我想绘制以下内容:

x = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
y = [10, 20, 1, 8, 7, 2, 3, 7, 11]

作为条形图(条形宽度可以是任何数字,只要它们可见即可)

当我这样做时:

plt.figure()
plt.bar(x, y)

我得到:

TypeError: cannot concatenate 'str' and 'float' objects
为什么?我怎样才能看到它的条形图?

最佳答案

该错误意味着您传递了错误的类型。

猜测,假设这是 pyplot,bar 命令有参数:

left, height, width=0.8, bottom=None, hold=None, **kwargs

您正在为“left”传递一个字符串数组,但您需要一个标量数字类型数组。 'left' 不是标签,而是坐标。

关于python - 已知 x、y 的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23689711/

相关文章:

python - "Element Should be Disabled"不工作?

Python Pandas - 多个特定列中变量的独特组合

python - 集合中不相交子集的最大可能数量

Python:计算 3D 网格的梯度

python - 如何在 azureml 中记录绘图?

python - 使用正则表达式提取域

python - 检查矩阵中的值是否与数组中的值匹配,如果不匹配则返回矩阵索引

python - numpy 数组中的匹配元素

date - matplotlib plot_date()在指定日期添加垂直线

python-3.x - 我如何摆脱这些工件(带有 LaTeX 的 matplotlib)