gpt4 book ai didi

python - Matplotlib:ValueError:大小不兼容:参数 'height'必须是长度2或标量

转载 作者:太空宇宙 更新时间:2023-11-03 14:45:09 25 4
gpt4 key购买 nike

我正在使用 matplotlib 绘制条形图。之前我在两个位置只有两组酒吧(总共 4 个酒吧)。所以 my_list 是一个 4x2 矩阵,我的脚本运行得很好。

我添加了另一组数据(所以现在我的数组是 6x2),并将这些新条形添加到我的 y1_means 中。但是,我收到以下错误:

ValueError:大小不兼容:参数“高度”必须是长度 2 或标量

<小时/>

* 更新 *

我添加了另一个条形图 (rects3),并将 y_means 减少为两个值。它可以工作,但最后两个栏是堆叠的。

<小时/>

我查看了与此错误相关的其他帖子,但找不到解决方案。有人可以帮忙吗?这是我的脚本:

#!/usr/bin/python

import numpy as np
import matplotlib.pyplot as plt

mylist = [[1061.8065108865587, 242.42977414163315],
[0.17619757021099161, 0.090235256942903783],
[0.12829524580453561, 0.083100278521638746],
[580.78192382619045, 367.56899594357981],
[0.76475391750505795, 0.42257691834032352],
[0.50712851564326855, 0.14217924391430981]]

###### Plot Script ######
N = 2

y1_means = (mylist[1][0], mylist[5][0])
y1_std = (mylist[1][1], mylist[5][1])

ind = np.arange(N) # the x locations for the groups
width = 0.3 # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(ind, y1_means, width, color='r', alpha=0.3, yerr=y1_std)

y2_means = (mylist[1][0], mylist[4][0])
y2_std = (mylist[1][1], mylist[4][1])
rects2 = ax.bar(ind + width, y2_means, width, color='b', alpha=0.3, yerr=y2_std)

y3_means = (mylist[2][0], mylist[5][0])
y3_std = (mylist[2][1], mylist[5][1])
rects3 = ax.bar(ind + width, y3_means, width, color='b', alpha=0.3, yerr=y3_std)

# add some text for labels, title and axes ticks
ax.set_ylabel('RTT')
ax.set_xticks(ind + width / 2)
#ax.grid(True)
ax.yaxis.grid('on')
ax.set_xticklabels(('Label-1', 'Label-2'))
fig.tight_layout(rect=[0, 0.05, 1, 0.95])
plt.figlegend( (rects1[0], rects2[0], rects3[0]), ('Scen-1', 'Scen-2', 'Scen-3'),
loc = 'lower center', ncol=5, labelspacing=0. )
plt.show()

最佳答案

我最终解决了这个问题。我需要添加另一个条形图。因此,每个条 y_means 都采用两个值并更新条的位置!这是我更新的代码:

###### Plot Script ######
N = 2

y1_means = (mylist[0][0], mylist[3][0])
y1_std = (mylist[0][1], mylist[3][1])

ind = np.arange(N) # the x locations for the groups
width = 0.3 # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(ind - width, y1_means, width, color='r', alpha=0.3, yerr=y1_std)

y2_means = (mylist[1][0], mylist[4][0])
y2_std = (mylist[1][1], mylist[4][1])
rects2 = ax.bar(ind, y2_means, width, color='b', alpha=0.3, yerr=y2_std)

y3_means = (mylist[2][0], mylist[5][0])
y3_std = (mylist[2][1], mylist[5][1])
rects3 = ax.bar(ind + width, y2_means, width, color='b', alpha=0.3, yerr=y2_std)

# add some text for labels, title and axes ticks
ax.set_ylabel('RTT')
ax.set_xticks(ind + width / 2)
#ax.grid(True)
ax.yaxis.grid('on')
ax.set_xticklabels(('Label-1', 'Label-2'))
fig.tight_layout(rect=[0, 0.05, 1, 0.95])
plt.figlegend( (rects1[0], rects2[0], rects3[0]), ('Scen-1', 'Scen-2', 'Scen-3'),
loc = 'lower center', ncol=5, labelspacing=0. )
plt.show()

关于python - Matplotlib:ValueError:大小不兼容:参数 'height'必须是长度2或标量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46281514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com