Pyplot 自动排序 y 值 [英] Pyplot sorting y-values automatically

查看:24
本文介绍了Pyplot 自动排序 y 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对我最喜欢的节目的剧集中所说的话进行了频率分析.我正在制作 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 = []例如,在 s1e01 中计数:if((word[:-1] in exclude_words) == False):s1e1_wordlist.append(word[:-1])s1e1_count.append(int(count))s1e1_sorted = sorted(list(sorted(zip(s1e1_count, s1e1_wordlist))),反向=真)s1e1_20 = []对于范围内的 i (0,20):s1e1_20.append(s1e1_sorted[i])s1e1_x = []s1e1_y = []对于计数,s1e1_20 中的单词:s1e1_x.append(word)s1e1_y.append(count)plot.figure(1, figsize=(20,20))plot.subplot(341)plot.title('第一季:第一集')plot.tick_params(axis='y',labelsize=8)plot.barh(s1e1_x, s1e1_y)

解决方案

从 matplotlib 2.1 开始,您可以绘制分类变量.这允许绘制 plt.bar(["apple","cherry","banana"], [1,2,3]).但是在 matplotlib 2.1 中,输出将按类别排序,因此按字母顺序排序.这被视为错误并在 matplotlib 2.2 中进行了更改(请参阅

I have a frequency analysis of words said in episodes of my favorite show. I'm making a plot.barh(s1e1_y, s1e1_x) but it's sorting by words instead of values. The output of >>> s1e1_y is

['know', 'go', 'now', 'here', 'gonna', 'can', 'them', 'think', 'come', 'time', 'got', 'elliot', 'talk', 'out', 'night', 'been', 'then', 'need', 'world', "what's"]

and >>>s1e1_x

[42, 30, 26, 25, 24, 22, 20, 19, 19, 18, 18, 18, 17, 17, 15, 15, 14, 14, 13, 13] When the plots are actually plotted, the graph's y axis ticks are sorted alphabetically even though the plotting list is unsorted...

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)

解决方案

From matplotlib 2.1 on you can plot categorical variables. This allows to plot plt.bar(["apple","cherry","banana"], [1,2,3]). However in matplotlib 2.1 the output will be sorted by category, hence alphabetically. This was considered as bug and is changed in matplotlib 2.2 (see this PR).

In matplotlib 2.2 the bar plot would hence preserve the order. In matplotlib 2.1, you would plot the data as numeric data as in any version prior to 2.1. This means to plot the numbers against their index and to set the labels accordingly.

w = ['know', 'go', 'now', 'here', 'gonna', 'can', 'them', 'think', 'come', 
 'time', 'got', 'elliot', 'talk', 'out', 'night', 'been', 'then', 'need', 
 'world', "what's"]
n = [42, 30, 26, 25, 24, 22, 20, 19, 19, 18, 18, 18, 17, 17, 15, 15, 14, 14, 13, 13]

import matplotlib.pyplot as plt
import numpy as np

plt.barh(range(len(w)),n)
plt.yticks(range(len(w)),w)

plt.show()

这篇关于Pyplot 自动排序 y 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆