运行代码之后,就会发生这样的情况:
ValueError:标签和X的尺寸必须兼容
我不太明白上面的错误是什么
老实说,python非常新,它指的是一段代码,然后跟随它生成一个方框图,但是遇到了一个错误,下面是我的代码:
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
任何帮助都是非常感谢的。
发布于 2019-11-23 08:57:24
您的错误在您的labels变量中。具体来说,它中有额外的值,如15-Nov。此外,当您使用set()函数时,您会丢失标签的顺序,因此它们以随机顺序出现。我不太清楚您今晚需要做些什么来修复它,但是您可以从调用labels中删除plt.boxplot()参数以使其正常工作。然后你就可以找出有效的标签。
错误是试图说“数据的尺寸和标签的尺寸不匹配”。
祝好运!
发布于 2022-01-08 07:13:22
标签应该是feature_names (即列维度,或axis=1),这样才能用不同的列划分在一个图中绘制。但是您的labels_var只是一个列(容量)值的列表--这是不正确的。你要么需要pivot_table你的数据..。或者plt.boxplot (不是ax.boxplot -我没有调查为什么)给了一个使用grouping_param的机会,例如“按‘容量’”(可能适合你的情况).或者你可以尝试使用海航库--也许它提供了更多的机会。
发布于 2022-06-18 21:55:08
尝试使用下面的方法来绘制旧版本。
bp_dict = plt.boxplot(values_total.transpose(),labels=labels,patch_artist=True)https://stackoverflow.com/questions/59005560
复制相似问题