Python 减去熊猫中的两个数据帧
我有一个数据帧:Python 减去熊猫中的两个数据帧,python,pandas,matplotlib,Python,Pandas,Matplotlib,我有一个数据帧: hourly=pd.DataFrame((np.random.randn(24,2)),列=['mean','std']) hourly.index.name='Hour' 我想画出每小时的平均值,加上两边的标准偏差,在标准偏差带之间画出阴影。我有以下代码来执行此操作: plt.figure() plt.plot(hourly["mean"].index, hourly["mean"] , 'k') upp_bd = hourly["mean"].add(2*hourly['s
hourly=pd.DataFrame((np.random.randn(24,2)),列=['mean','std'])
hourly.index.name='Hour'
我想画出每小时的平均值,加上两边的标准偏差,在标准偏差带之间画出阴影。我有以下代码来执行此操作:
plt.figure()
plt.plot(hourly["mean"].index, hourly["mean"] , 'k')
upp_bd = hourly["mean"].add(2*hourly['std'])
low_bd = hourly['mean'].subtract(2*hourly['std'])
plt.fill_between(hourly.index, upp_bd,low_bd,color='b', alpha=0.2)
不幸的是,我一直收到以下错误:
ValueError: Argument dimensions are incompatible
hourly[“mean”]
变量具有以下数据结构:
SomeNum
Hour
0 50.003195
1 50.003687
2 50.005464
3 50.002127
4 49.998756
5 50.000194
6 49.994266
7 49.989631
8 49.997383
9 49.998948
10 50.005860
11 49.999882
12 50.000132
13 49.997024
14 49.995282
15 49.994489
16 49.995658
17 50.006386
18 50.004037
19 49.999172
20 49.999787
21 50.000317
22 50.000174
23 50.002752
SomeNum
Hour
0 50.112504
1 50.108413
2 50.107596
3 50.104473
4 50.099938
5 50.108084
6 50.106991
7 50.102655
8 50.107372
9 50.106870
10 50.111932
11 50.105725
12 50.104598
13 50.101926
14 50.100037
15 50.103230
16 50.107482
17 50.115969
18 50.114712
19 50.112294
20 50.116430
21 50.116907
22 50.109491
23 50.110875
Int64Index([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
dtype='int64', name=u'Hour')
upp\u bd
和low\u bd
变量具有以下结构:
SomeNum
Hour
0 50.003195
1 50.003687
2 50.005464
3 50.002127
4 49.998756
5 50.000194
6 49.994266
7 49.989631
8 49.997383
9 49.998948
10 50.005860
11 49.999882
12 50.000132
13 49.997024
14 49.995282
15 49.994489
16 49.995658
17 50.006386
18 50.004037
19 49.999172
20 49.999787
21 50.000317
22 50.000174
23 50.002752
SomeNum
Hour
0 50.112504
1 50.108413
2 50.107596
3 50.104473
4 50.099938
5 50.108084
6 50.106991
7 50.102655
8 50.107372
9 50.106870
10 50.111932
11 50.105725
12 50.104598
13 50.101926
14 50.100037
15 50.103230
16 50.107482
17 50.115969
18 50.114712
19 50.112294
20 50.116430
21 50.116907
22 50.109491
23 50.110875
Int64Index([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
dtype='int64', name=u'Hour')
而hourly.index
变量具有以下结构:
SomeNum
Hour
0 50.003195
1 50.003687
2 50.005464
3 50.002127
4 49.998756
5 50.000194
6 49.994266
7 49.989631
8 49.997383
9 49.998948
10 50.005860
11 49.999882
12 50.000132
13 49.997024
14 49.995282
15 49.994489
16 49.995658
17 50.006386
18 50.004037
19 49.999172
20 49.999787
21 50.000317
22 50.000174
23 50.002752
SomeNum
Hour
0 50.112504
1 50.108413
2 50.107596
3 50.104473
4 50.099938
5 50.108084
6 50.106991
7 50.102655
8 50.107372
9 50.106870
10 50.111932
11 50.105725
12 50.104598
13 50.101926
14 50.100037
15 50.103230
16 50.107482
17 50.115969
18 50.114712
19 50.112294
20 50.116430
21 50.116907
22 50.109491
23 50.110875
Int64Index([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
dtype='int64', name=u'Hour')
注意,第一个绘图
plt.plot(hourly[“mean”].index,hourly[“mean”],'k')
工作正常。您正在尝试每小时和每天绘图吗?那些框架的形状是一样的吗?我猜其中一个维度与另一个维度不同,因此绘图库无法处理它。daily[“mean”]
和hourly[“mean”]
具有相同的维度?抱歉-输入错误。每小时一次。我现在已经纠正了这个问题。它们都应该有相同的尺寸。对于哪一行你会得到这个例外?还可以显示正在使用的每个变量的维度(或者更好,提供一个可复制的示例),堆栈跟踪引用不同的代码版本。在这里,您每小时和每天都在使用。它们有相同的尺寸吗?另外,您正在根据upp\u bd
绘制upp\u bd.index
,第四个参数转换为where=upp\u bd
。这段代码对我来说适用于相同的lib版本。