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

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

问题描述

我对我最喜欢的节目的插曲中所说的单词进行了频率分析.我正在绘制plot.barh(s1e1_y,s1e1_x),但它是按单词而不是值进行排序的. >>> s1e1_y的输出 是

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"]

>>>s1e1_x

[42, 30, 26, 25, 24, 22, 20, 19, 19, 18, 18, 18, 17, 17, 15, 15, 14, 14, 13, 13] 当实际绘制绘图时,即使绘图列表未排序,图形的y轴刻度也会按字母顺序排序...

[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)

推荐答案

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

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).

在matplotlib 2.2中,条形图将因此保留顺序. 在matplotlib 2.1中,您将像2.1之前的任何版本一样将数据绘制为数字数据.这意味着将数字与它们的索引相对应并相应地设置标签.

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天全站免登陆