【问题标题】:ValueError: Dimensions of labels and X must be compatibleValueError:标签和X的尺寸必须兼容
【发布时间】:2022-01-09 13:41:10
【问题描述】:

运行代码后,会出现这种情况:

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

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

【问题讨论】:

  • 你好,达米安。这是一个长代码示例。为了有人提供帮助,您需要说明错误发生在哪一行。最好只粘贴您收到的带有错误的整个回溯文本。谢谢。
  • bp_dict = plt.boxplot(values_total,labels=labels,patch_artist=True) 这是错误所在,抱歉!
  • 还有礼貌地提到数据集在data.gov.sg/dataset/…提供
  • 对此非常抱歉,现在将包括在内。谢谢

标签: python matplotlib pycharm


【解决方案1】:

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

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

祝你好运!

【讨论】:

    【解决方案2】:

    标签应该是 feature_names(列维度或轴 = 1),以便通过不同的列划分在一个图(matplot)中绘制。 但是您的 labels_var 只是一列(容量)值的列表 - 这是不正确的。 您需要 pivot_table 您的数据框 ... 或 plt.boxplot (不是 ax.boxplot - 我没有调查原因)提供了使用 grouping_param 的机会,例如“按'容量'”(可能适合您的情况)...或者您可以尝试使用 seaborn 库 - 可能它提供了更多机会

    【讨论】:

      猜你喜欢
      • 2016-08-26
      • 2017-07-18
      • 2021-11-08
      • 1970-01-01
      • 2021-08-29
      • 2022-07-23
      • 2021-11-12
      • 2021-08-18
      • 2018-02-11
      相关资源
      最近更新 更多