为了账号安全,请及时绑定邮箱和手机立即绑定

Pyplot自动对y值进行排序

Pyplot自动对y值进行排序

犯罪嫌疑人X 2020-02-02 14:35:13
我对我最喜欢的节目的片段中所说的单词进行了频率分析。我正在绘制plot.barh(s1e1_y,s1e1_x),但是它是按单词而不是值进行排序的。的输出>>> s1e1_y 是['know', 'go', 'now', 'here', 'gonna', 'can', 'them', 'think', 'come', 'time', 'got', 'elliot', 'talk', 'out', 'night', 'been', 'then', 'need', 'world', "what's"]和 >>>s1e1_x[42, 30, 26, 25, 24, 22, 20, 19, 19, 18, 18, 18, 17, 17, 15, 15, 14, 14, 13, 13]当实际绘制绘图时,即使绘图列表未排序,图表的y轴刻度也会按字母顺序排序...s1e1_wordlist = []s1e1_count = []for word, count in s1e01:    if((word[:-1] in excluded_words) == False):        s1e1_wordlist.append(word[:-1])        s1e1_count.append(int(count))s1e1_sorted = sorted(list(sorted(zip(s1e1_count, s1e1_wordlist))), reverse=True)s1e1_20 = []for i in range(0,20):    s1e1_20.append(s1e1_sorted[i])s1e1_x = []s1e1_y = []for count, word in s1e1_20:    s1e1_x.append(word)    s1e1_y.append(count)plot.figure(1, figsize=(20,20))plot.subplot(341)plot.title('Season1 : Episode 1')plot.tick_params(axis='y',labelsize=8)plot.barh(s1e1_x, s1e1_y)
查看完整描述

2 回答

?
慕少森

TA贡献2019条经验 获得超9个赞

好的,您的示例中似乎有很多虚假代码,与您所描述的问题无关,但是假设您不希望y轴按字母顺序排序,则需要将两个列表压缩为一个数据框,然后按如下所示绘制数据框


df = pd.DataFrame(list(zip(s1e1_y,s1e1_x))).set_index(1)


df.plot.barh()


查看完整回答
反对 回复 2020-02-02
  • 2 回答
  • 0 关注
  • 763 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信