問題描述
在這個例子中:
import matplotlib.pyplot as plt顏色= ['白色','橙色','綠色','紫色']費率 = ['5','5','4','3']plt.bar(顏色,費率)
圖中的條形按字母順序排列.
如何使它們以與代碼中相同的順序顯示(從最高到最低的比率)?
Matplotlib 2.1 為
In this example:
import matplotlib.pyplot as plt
colors = ['white', 'orange', 'green', 'purple']
rates = ['5','5','4','3']
plt.bar(colors, rates)
the bars in the plot are ordered by the alphabetic order.
How do I make them to show in the same order as they are in the code (from highest to lowest rate)?
Matplotlib 2.1 provides the new option to use strings as input to its plotting functions - called "categoricals". This is a new feature and not yet completely working. One caveat is that strings are automatically sorted (alphabetically). This restriction will be removed in the next release.
Until then you would produce your plot just as in times when matplotlib did not even allow for strings to put in. Convert data to numbers, and plot against the list index. Afterwards set the ticklabels to their names.
import matplotlib.pyplot as plt
colors = ['white', 'orange', 'green', 'purple']
rates = ['5','5','4','3']
plt.bar(range(len(colors)), list(map(float,rates)) )
plt.xticks(range(len(colors)), colors)
plt.show()
這篇關于在 python 2.7 中更改 matplotlib.bar 順序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!