Pyplot 自动排序 y 值
Pyplot sorting y-values automatically
我对我最喜欢的节目剧集中所说的话进行了频率分析。我正在制作一个 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)
好的,您的示例中似乎有很多伪代码,这些代码与您描述的问题无关,但假设您不希望 y 轴按字母顺序排序,那么您需要压缩你的两个列表到一个数据框然后绘制数据框如下
df = pd.DataFrame(list(zip(s1e1_y,s1e1_x))).set_index(1)
df.plot.barh()
然后生成以下内容
从 matplotlib 2.1 开始,您可以绘制分类变量。这允许绘制 plt.bar(["apple","cherry","banana"], [1,2,3])
。然而,在 matplotlib 2.1 中,输出将按类别排序,因此按字母顺序排序。这被认为是错误,并在 matplotlib 2.2 中进行了更改(参见 this PR)。
在 matplotlib 2.2 中,条形图将因此保留顺序。
在 matplotlib 2.1 中,您可以像 2.1 之前的任何版本一样将数据绘制为数字数据。这意味着根据它们的索引绘制数字并相应地设置标签。
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()
我对我最喜欢的节目剧集中所说的话进行了频率分析。我正在制作一个 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)
好的,您的示例中似乎有很多伪代码,这些代码与您描述的问题无关,但假设您不希望 y 轴按字母顺序排序,那么您需要压缩你的两个列表到一个数据框然后绘制数据框如下
df = pd.DataFrame(list(zip(s1e1_y,s1e1_x))).set_index(1)
df.plot.barh()
然后生成以下内容
从 matplotlib 2.1 开始,您可以绘制分类变量。这允许绘制 plt.bar(["apple","cherry","banana"], [1,2,3])
。然而,在 matplotlib 2.1 中,输出将按类别排序,因此按字母顺序排序。这被认为是错误,并在 matplotlib 2.2 中进行了更改(参见 this PR)。
在 matplotlib 2.2 中,条形图将因此保留顺序。 在 matplotlib 2.1 中,您可以像 2.1 之前的任何版本一样将数据绘制为数字数据。这意味着根据它们的索引绘制数字并相应地设置标签。
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()