- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我需要像这样想出不同列表的条形图
import math
import numpy as np
import matplotlib.pyplot as plt
month=["dec-09","jan","feb"]
n=len(month)
kitchen=[57.801,53.887,49.268]
laundry=[53.490,56.568,53.590]
air=[383.909,395.913,411.714]
other=[519.883,483.293,409.956]
ind=np.arange(n)
width=0.35
p1=plt.bar(ind,kitchen,width,color="cyan")
p2=plt.bar(ind,laundry,width,color="red",bottom=kitchen)
p3=plt.bar(ind,air,width,color="green",bottom=kitchen+laundry)
p4=plt.bar(ind,other,width,color="blue",bottom=kitchen+laundry+air)
plt.ylabel("KWH")
plt.title("winter")
plt.xticks(ind+width/2,("dec-09","jan","feb"))
plt.show()
这只是一个简单的代码,我想把它们堆叠起来,但我遇到了一个错误,我不知道该怎么办
p3=plt.bar(ind,air,width,color="green",bottom=kitchen+laundry)
File "C:\Python33\lib\site-packages\matplotlib\pyplot.py", line 2515, in bar
ret = ax.bar(left, height, width=width, bottom=bottom, **kwargs)
File "C:\Python33\lib\site-packages\matplotlib\axes.py", line 5007, in bar
nbars)
AssertionError: incompatible sizes: argument 'bottom' must be length 3 or scalar
最佳答案
创建kitchen
、laundry
、air
和other
NumPy 数组:
import math
import numpy as np
import matplotlib.pyplot as plt
month=["dec-09","jan","feb"]
n=len(month)
kitchen=np.array([57.801,53.887,49.268])
laundry=np.array([53.490,56.568,53.590])
air=np.array([383.909,395.913,411.714])
other=np.array([519.883,483.293,409.956])
ind=np.arange(n)
width=0.35
p1=plt.bar(ind,kitchen,width,color="cyan")
p2=plt.bar(ind,laundry,width,color="red",bottom=kitchen)
p3=plt.bar(ind,air,width,color="green",bottom=kitchen+laundry)
p4=plt.bar(ind,other,width,color="blue",bottom=kitchen+laundry+air)
plt.ylabel("KWH")
plt.title("winter")
plt.xticks(ind+width/2,("dec-09","jan","feb"))
plt.show()
您得到的错误是由于添加 lists
将它们连接起来:
In [162]: [1,2,3] + [4,5,6]
Out[162]: [1, 2, 3, 4, 5, 6]
然而,添加 NumPy 数组会按元素添加数组:
In [163]: np.array([1,2,3]) + np.array([4,5,6])
Out[163]: array([5, 7, 9])
在线上出现错误:
p3=plt.bar(ind,air,width,color="green",bottom=kitchen+laundry)
因为 kitchen+laundry
有 6 个元素(因为串联),而您只需要 3 个元素(在元素加法之后)。
关于python - matplotlib 堆叠条形图 AssertionError : incompatible sizes: argument 'bottom' must be length 3 or scalar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23189815/
我知道关于这个错误已经有几个问题了。但在这种特殊情况下,我不确定是否已经有解决我的问题的方法。 我有这部分代码,我想打印 Dataframe df 的“y”列。 出现以下错误: 类型错误:只有整数标量
我为这个问题做了一个更简单的两个数组的例子: labelArray 是一个一维数组,在索引处有标签,对应于 nD 数组 someValuesArray。我得到了标签 2 出现的所有索引,并希望检索 s
我正在尝试 github link 中的 tensorflow 的简单演示代码. 我目前使用的是python 3.5.2版 Z:\downloads\tensorflow_demo-master\te
我在一个图像数组中有一些真实图像,而在另一个图像数组中有一些虚假图像。我想附加两个numpy图像阵列来训练我的CNN模型。但是np.concatenate产生错误only integer scalar
我有一组属性,单个属性的每个值要么是标量(字符串、整数……),要么是标量集合(集合、集合……)。以下是一个 XML 文档作为示例: 1 1 2
我首先要说的是,我对 python 和 Stack Overflow 非常陌生。我正在尝试对一堆不同的数组进行操作。这些数组之一的示例是 s2 = np.array( ['e','e','e','k'
i=np.arange(1,4,dtype=np.int) a=np.arange(9).reshape(3,3) 和 a >>>array([[0, 1, 2], [3, 4,
重命名DataFrame的列后,合并新列时出现错误: import pandas as pd df1 = pd.DataFrame({'a': [1, 2]}) df2 = pd.DataFrame(
我正在我的代码中寻找一个奇怪的运行时错误,该错误是一致的。我在一行中得到了我认为未定义的行为:Eigen::Vector3d m = d1 * cos(theta) + d2 * sin(theta)
我正在尝试这样做: for i in cols: print('rmse value for', i, 'is : ', np.sqrt(mean_squared_error(pred[i],
我试图找到数据集最大值处的 x 值以及每个最大值所在的峰值宽度。我已经厌倦了下面的代码,第一部分正确返回峰值 x 位置,但是一旦添加第二部分,它就会失败并显示错误消息: TypeError: only
我想创建一个程序来使用 SVM 对文本数据进行分类。但在此之前,我必须使用 StratifiedKFold() 将数据拆分为训练数据和测试数据。 但它以这个错误结束: 'Traceback (most
我正在尝试为我的神经网络实现自定义数据集。但是在运行转发功能时出现此错误。代码如下。 import torch import torch.nn as nn import torch.nn.functi
我确信我错过了一些简单的事情,但在格式为“对象”的日期列的“to_datetime”转换过程中,我遇到了此类型错误。我尝试了此代码的不同变体,但仍然遇到相同的错误。我正在使用 PyCharm IDE。
尝试对包含 279 个文件的数据集执行 Kfold cv,执行 k 均值后,文件的形状为 ( 279 , 5 , 90) 。我重新调整了它的形状,以便适合 svm。现在的形状是(279, 5*90)。
我在这里使用 Arjun Comar 版本的 OpenCV 3.0 https://github.com/arjuncomar/opencv ...Arjun Comar 更新了此版本的 OpenCV
以下代码: x = list(range(0,10)) random.shuffle(x) ind = np.argsort(x) x[ind] 产生错误:TypeError: only intege
这个问题在这里已经有了答案: Concatenating two one-dimensional NumPy arrays (6 个答案) 关闭 5 年前。 我想将 numpy 数组存储到另一个 n
我有一个 PyTorch LSTM 模型,我的forward 函数如下所示: def forward(self, x, hidden): print('in forward',
我正在尝试通过 PyTorch 训练分类器。但是,当我向模型提供训练数据时,我遇到了训练问题。我在 y_pred = model(X_trainTensor) 上收到此错误: RuntimeError
我是一名优秀的程序员,十分优秀!