【发布时间】:2021-04-28 18:54:01
【问题描述】:
我使用 Matplotlib 根据 NumPy 数组中唯一字符串的计数创建了一个条形图。现在我想在条形图中只显示前 10 个最常见的物种。我是 Python 新手,所以我很难弄清楚。这也是我在这里的第一个问题,所以如果我遗漏了任何重要信息,请告诉我
test_indices = numpy.where((obj.year == 2014) & (obj.native == "Native"))
SpeciesList2014 = numpy.append(SpeciesList2014, obj.species_code[test_indices])
labels, counts = numpy.unique(SpeciesList2014, return_counts=True)
indexSort = numpy.argsort(counts)
plt.bar(labels[indexSort][::-1], counts[indexSort][::-1], align='center')
plt.xticks(rotation=45)
plt.show()
【问题讨论】:
-
obj是熊猫数据框吗?直接在熊猫方面这样做会更直接 -
不,都是用numpy的。这只是我从老板那里得到的一大组代码中的一部分,我不想把它拆开,因为我是个菜鸟。如果有任何建议使用 numpy 数组,那将是首选。
标签: python matplotlib bar-chart