值错误:标签和X的尺寸必须兼容

2024-05-18 08:45:10 发布

您现在位置:Python中文网/ 问答频道 /正文

运行代码后,会发生以下情况:

ValueError: Dimensions of labels and X must be compatible

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

老实说,python是个新手,引用了一个代码,然后按照它来生成一个boxplot图,但是遇到了一个错误,下面是我的代码:

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

非常感谢任何帮助^^ 谢谢


Tags: ofnumberfordatalabelspltdictcolor
1条回答
网友
1楼 · 发布于 2024-05-18 08:45:10

您的错误在labels变量中。具体地说,其中有额外的值,比如15-Nov。另外,在使用set()函数时,会丢失标签的顺序,因此它们以随机顺序出现。{{1}你不需要调用cdm>来修复它。然后你可以找出有效的标签。在

错误是试图说“数据的维度和标签的维度不匹配”。在

祝你好运!在

相关问题 更多 >