我正在努力制作描述竞争对手即将参加比赛的情节。目前,我有一个可以制作情节的工作代码,但我想让这些图有线而不是点和y限制要改变。这听起来很简单,但我一直遇到我不期望的错误。这可能是因为我的代码效率不高。
我目前的数据被保存到两个不同的数组中,其中一个按照4个部门的特征分为349个单独的部门:颜色,年龄,性别和重量分区,如下所示:
[u'BLUE', u'Master 1', u'Male', u'Heavy'],
[u'BLUE', u'Master 1', u'Male', u'Super-Heavy'],
[u'BLUE', u'Master 1', u'Male', u'Ultra-Heavy'],
[u'BLUE', u'Master 1', u'Female', u'Rooster'],
[u'BLUE', u'Master 1', u'Female', u'Light-Feather'],
[u'BLUE', u'Master 1', u'Female', u'Feather'],
[u'BLUE', u'Master 1', u'Female', u'Light'],
[u'BLUE', u'Master 1', u'Female', u'Middle'],
[u'BLUE', u'Master 1', u'Female', u'Medium-Heavy'],
然后我有一个349长度的数组,每个部门都有竞争对手的数量。
我最初的目标是制作一个面具,以便它选择相同的颜色,年龄和性别划分,然后按重量绘制它们。我还想制作一个情节,将相同颜色和性别的所有不同年龄段相加,并且按照颜色和性别的总重量注册人数。
代码不喜欢对字符串进行绘图(对于权重除法),所以我还将包含不同权重除法的字典作为x轴:
male_adult_divisions = {"Rooster":127.0, "Light-Feather":141.5, "Feather":154.5, "Light":168.0, "Middle":181.5, "Medium-Heavy":195.0, "Heavy":208.0, "Super-Heavy":222.0, "Ultra-Heavy":235.0}
male_juvenile1_divisions = {"Rooster":107.0, "Light-Feather":118.0, "Feather":129.0, "Light":141.0, "Middle":152.0, "Medium-Heavy":163.0, "Heavy":175.0, "Super-Heavy":186.0, "Ultra-Heavy":197.0}
male_juvenile2_divisions = {"Rooster":118.0, "Light-Feather":129.0, "Feather":141.5, "Light":152.5, "Middle":163.5, "Medium-Heavy":175.0, "Heavy":186.0, "Super-Heavy":197.0, "Ultra-Heavy":208.0}
female_adult_divisions = {"Rooster":107.0, "Light-Feather":118.0, "Feather":129.0, "Light":141.0, "Middle":152.0, "Medium-Heavy":163.0, "Heavy":175.0, "Super-Heavy":186.0}
female_juvenile_divisions = {"Rooster":98.0, "Light-Feather":106.5, "Feather":116.0, "Light":125.0, "Middle":133.5, "Medium-Heavy":143.5, "Heavy":152.0, "Super-Heavy":160.5}
这是我到目前为止所做的代码:
for gender in gender_divisions:
gender_index = numpy.where(numpy.transpose(divisions)[2] == gender)
for age in age_divisions:
age_index = numpy.where(numpy.transpose(divisions)[1] == age)
if (age == "Juvenile 1 "or age == "Juvenile 2 ") and gender == "Female":
weight_limits = female_juvenile_divisions
elif age == "Juvenile 1 "and gender == "Male":
weight_limits = male_juvenile1_divisions
elif age == "Juvenile 2 "and gender == "Male":
weight_limits = male_juvenile2_divisions
elif gender == "Female":
weight_limits = female_adult_divisions
else:
weight_limits = male_adult_divisions
mask = numpy.intersect1d(gender_index,age_index)
mask = numpy.intersect1d(mask, exclude_absolute)
if not mask.size:
break
for i,color in enumerate(belt_divisions):
boolean_mask = numpy.zeros(len(numpy.transpose(divisions)[0]))
if color == "WHITE":
white_mask = numpy.intersect1d(mask, numpy.where(color == numpy.transpose(divisions)[0]))
for x in white_mask:
plt.plot(weight_limits[numpy.transpose(divisions[x])[3]],competitors[x], marker = "o", c="white")
elif color == "BLUE":
blue_mask = numpy.intersect1d(mask, numpy.where(color == numpy.transpose(divisions)[0]))
for x in blue_mask:
plt.plot(weight_limits[numpy.transpose(divisions[x])[3]],competitors[x], marker = "o", c="blue")
elif color == "PURPLE":
purple_mask = numpy.intersect1d(mask, numpy.where(color == numpy.transpose(divisions)[0]))
for x in purple_mask:
plt.plot(weight_limits[numpy.transpose(divisions[x])[3]],competitors[x], marker = "o", c="violet")
elif color == "BROWN":
brown_mask = numpy.intersect1d(mask, numpy.where(color == numpy.transpose(divisions)[0]))
for x in brown_mask:
plt.plot(weight_limits[numpy.transpose(divisions[x])[3]],competitors[x], marker = "o", c="brown")
else:
black_mask = numpy.intersect1d(mask, numpy.where(color == numpy.transpose(divisions)[0]))
for x in black_mask:
plt.plot(weight_limits[numpy.transpose(divisions[x])[3]],competitors[x], marker = "o", c="black")
plt.title("Competitors in %s %s Divisions"%(gender,age))
plt.xlabel("Weight Category")
plt.ylabel("Number of Competitors")
plt.ylim(0,)
plt.show()
if age != "Juvenile 1" and age != "Juvenile 2":
if gender == "Female":
weight_limits = female_adult_divisions
mask = numpy.intersect1d(gender_index, exclude_absolute)
for i,color in enumerate(belt_divisions):
boolean_mask = numpy.zeros(len(numpy.transpose(divisions)[0]))
if color == "WHITE":
white_mask = numpy.intersect1d(mask, numpy.where(color == numpy.transpose(divisions)[0]))
for x in white_mask:
plt.plot(weight_limits[numpy.transpose(divisions[x])[3]],competitors[x], marker = "o", c="white")
elif color == "BLUE":
blue_mask = numpy.intersect1d(mask, numpy.where(color == numpy.transpose(divisions)[0]))
for x in blue_mask:
plt.plot(weight_limits[numpy.transpose(divisions[x])[3]],competitors[x], marker = "o", c="blue")
elif color == "PURPLE":
purple_mask = numpy.intersect1d(mask, numpy.where(color == numpy.transpose(divisions)[0]))
for x in purple_mask:
plt.plot(weight_limits[numpy.transpose(divisions[x])[3]],competitors[x], marker = "o", c="violet")
elif color == "BROWN":
brown_mask = numpy.intersect1d(mask, numpy.where(color == numpy.transpose(divisions)[0]))
for x in brown_mask:
plt.plot(weight_limits[numpy.transpose(divisions[x])[3]],competitors[x], marker = "o", c="brown")
else:
black_mask = numpy.intersect1d(mask, numpy.where(color == numpy.transpose(divisions)[0]))
for x in black_mask:
plt.plot(weight_limits[numpy.transpose(divisions[x])[3]],competitors[x], marker = "o", c="black")
plt.title("Competitors in All %s Divisions"%(gender))
plt.xlabel("Weight Category")
plt.ylabel("Number of Competitors")
plt.ylim(0,)
plt.show()
当我最初尝试写这篇文章的时候,我试图创建一个索引数组来挑选我想要的分区,但出于某种原因,我无法让它工作。
例如,如果我有black_mask = array([343, 344, 345, 346, 347, 348])
,那么我尝试divisions[black_mask]
,我得到:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-17-c1f99913b04a> in <module>()
----> 1 divisions[black_mask]
TypeError: only integer arrays with one element can be converted to an index
那么我想也许我需要制作一个布尔数组来将它用作掩码:
boolean_mask = numpy.zeros(len(numpy.transpose(divisions)[0]))
for x in black_mask:
boolean_mask[x] = 1
但是我得到了同样的错误,这就是我最终使用上面效率低下的代码的原因。
然后我想改变情节的y限制。我通过创建一个名为max_的变量并将其设置为零来完成此操作。每当代码输入带颜色的if语句之一时,我就放if competitors[x]> max_: max_ = competitors[x]
,但由于某种原因,这使得我的所有情节变成空白(即使我没有把它放入在y限制中,我认为。)
我也想把它作为折线图,但由于不得不逐点说明,我无法做到。
所以目前这些是我要走出的情节类型:
这就是我想要模仿的东西: