擅长:python、mysql、java
<p>从matplotlib 2.1开始,可以绘制分类变量。这允许绘制<code>plt.bar(["apple","cherry","banana"], [1,2,3])</code>。但是在matplotlib 2.1中,输出将按类别排序,因此按字母顺序排序。这被认为是错误,并在matplotlib 2.2中进行了更改(请参见<a href="https://github.com/matplotlib/matplotlib/pull/10212" rel="noreferrer">this PR</a>)。</p>
<p>因此,在matplotlib 2.2中,条形图将保持顺序。
在matplotlib 2.1中,您可以将数据打印为数字数据,就像在2.1之前的任何版本中一样。这意味着根据索引绘制数字并相应地设置标签。</p>
<pre><code>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()
</code></pre>
<p><a href="https://i.stack.imgur.com/1DqVo.png" rel="noreferrer"><img src="https://i.stack.imgur.com/1DqVo.png" alt="enter image description here"/></a></p>