首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ValueError:标签和X的尺寸必须兼容

ValueError:标签和X的尺寸必须兼容
EN

Stack Overflow用户
提问于 2019-11-23 07:42:29
回答 3查看 7.4K关注 0票数 2

运行代码之后,就会发生这样的情况:

ValueError:标签和X的尺寸必须兼容

我不太明白上面的错误是什么

老实说,python非常新,它指的是一段代码,然后跟随它生成一个方框图,但是遇到了一个错误,下面是我的代码:

代码语言:javascript
运行
复制
import numpy as np
import matplotlib.pyplot as plt


title = "Annual Bus Population"
titlelen = len(title)
print("{:*^{titlelen}}".format(title, titlelen=titlelen+6))
print()

filename = 'annual-bus-population-by-passenger-capacity.csv'
data = np.genfromtxt(filename, dtype=["i4", "U50", "i8"], delimiter=",", names=True)

#print("Original data: " + str(data.shape))

null_rows = np.isnan(data['number'])
nonnull_values = data[null_rows==False]
#print("Filtered data: " + str(nonnull_values.shape))

labels = list(set(data['capacity']))
capacities = np.arange(0,len(labels))
capacity_number = data[['capacity','number']]

numbers = capacity_number['number']

values_nine = numbers[capacity_number ['capacity'] == '<10']
values_fifteen = numbers[capacity_number['capacity'] == '10-15']
values_twenty = numbers[capacity_number['capacity'] == '16-20']
values_twentyfive = numbers[capacity_number['capacity'] == '21-25']
values_thirty= numbers[capacity_number ['capacity'] == '21-30']
values_thirtyfive = numbers[capacity_number ['capacity'] == '31-35']
values_fourty = numbers[capacity_number ['capacity'] == '36-40']
values_fourtyfive = numbers[capacity_number ['capacity'] == '40-45']
values_fifty = numbers[capacity_number ['capacity'] == '45-50']
values_fiftyfive = numbers[capacity_number ['capacity'] == '51-55']
values_sixty = numbers[capacity_number ['capacity'] == '56-60']
values_sixtyfive = numbers[capacity_number ['capacity'] == '61-65']
values_seventy = numbers[capacity_number ['capacity'] == '66-70']
values_moreseventy = numbers[capacity_number ['capacity'] == '>70']


values_total = [values_nine,values_fifteen,values_twenty,values_twentyfive,values_thirty,values_thirtyfive,values_fourty,values_fourtyfive,values_fifty,values_fiftyfive,values_sixty,values_sixtyfive,values_seventy,values_moreseventy]

#print(values_total.shape)
#print()

plt.figure(2, figsize=(30,30))
plt.title(title,fontsize=50)
plt.ylabel('Number of passengers',fontsize=40)
plt.yticks(fontsize=30)
plt.xticks(fontsize=30,rotation='vertical')
bp_dict = plt.boxplot(values_total,labels=labels,patch_artist=True)


## change outline color, fill color and linewidth of the boxes
for box in bp_dict['boxes']:
    # change outline color
    box.set( color='#7570b3', linewidth=2)
    # change fill color
    box.set( facecolor = '#1b9e77' )

## change color and linewidth of the whiskers
for whisker in bp_dict['whiskers']:
    whisker.set(color='#7570b3', linewidth=2)

## change color and linewidth of the caps
for cap in bp_dict['caps']:
    cap.set(color='#7570b3', linewidth=2)

## change color and linewidth of the medians
for median in bp_dict['medians']:
    median.set(color='#b2df8a', linewidth=2)

## change the style of fliers and their fill
for flier in bp_dict['fliers']:
    flier.set(marker='D', color='#e7298a', alpha=0.5)

print(bp_dict.keys())

for line in bp_dict['medians']:
    # get position data for median line
    x, y = line.get_xydata()[1] # top of median line
    # overlay median value
    plt.text(x, y, '%.1f' % y,
         horizontalalignment='center',fontsize=30) # draw above, centered

fliers = []
for line in bp_dict['fliers']:
    ndarray = line.get_xydata()
    if (len(ndarray)>0):
       max_flier = ndarray[:,1].max()
       max_flier_index = ndarray[:,1].argmax()
       x = ndarray[max_flier_index,0]
       print("Flier: " + str(x) + "," + str(max_flier))

       plt.text(x,max_flier,'%.1f' % max_flier,horizontalalignment='center',fontsize=30,color='green') 

plt.show()

错误出现在这一行:

bp_dict = plt.boxplot(values_total,labels=labels,patch_artist=True)

数据集来自:

https://data.gov.sg/dataset/annual-age-bus-population-by-passenger-capacity

任何帮助都是非常感谢的。

EN

回答 3

Stack Overflow用户

发布于 2019-11-23 08:57:24

您的错误在您的labels变量中。具体来说,它中有额外的值,如15-Nov。此外,当您使用set()函数时,您会丢失标签的顺序,因此它们以随机顺序出现。我不太清楚您今晚需要做些什么来修复它,但是您可以从调用labels中删除plt.boxplot()参数以使其正常工作。然后你就可以找出有效的标签。

错误是试图说“数据的尺寸和标签的尺寸不匹配”。

祝好运!

票数 1
EN

Stack Overflow用户

发布于 2022-01-08 07:13:22

标签应该是feature_names (即列维度,或axis=1),这样才能用不同的列划分在一个图中绘制。但是您的labels_var只是一个列(容量)值的列表--这是不正确的。你要么需要pivot_table你的数据..。或者plt.boxplot (不是ax.boxplot -我没有调查为什么)给了一个使用grouping_param的机会,例如“按‘容量’”(可能适合你的情况).或者你可以尝试使用海航库--也许它提供了更多的机会。

票数 0
EN

Stack Overflow用户

发布于 2022-06-18 21:55:08

尝试使用下面的方法来绘制旧版本。

代码语言:javascript
运行
复制
bp_dict = plt.boxplot(values_total.transpose(),labels=labels,patch_artist=True)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59005560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档