Python matplotlib条的顺序不正确
我有一个条形图,其中y轴是从一月到十二月的月份列表,x轴值以相应的顺序存储在另一个列表中。 当我绘制图表时,月份的顺序被混淆了Python matplotlib条的顺序不正确,python,matplotlib,Python,Matplotlib,我有一个条形图,其中y轴是从一月到十二月的月份列表,x轴值以相应的顺序存储在另一个列表中。 当我绘制图表时,月份的顺序被混淆了 In: fig, ((ax1, ax2)) = plt.subplots(nrows=1, ncols=2, figsize=(10,5), sharex='row') fig.suptitle("Income from members and supporters", fontsize=14) ax1.barh(months, tag_max) ax1.se
In:
fig, ((ax1, ax2)) = plt.subplots(nrows=1, ncols=2, figsize=(10,5), sharex='row')
fig.suptitle("Income from members and supporters", fontsize=14)
ax1.barh(months, tag_max)
ax1.set_facecolor('white')
ax1.set_title("Maximum income from members")
ax2.barh(months, tam_max)
ax2.set_facecolor('white')
ax2.get_yaxis().set_visible(False)
ax2.set_title('Maximum income from supporters')
输出:
原因是什么?我如何解决?
谢谢 大卫的评论是正确的。你可以通过使用数值来解决这个问题 将月份指定为
yticklabels
from matplotlib import pyplot as plt
import numpy as np
months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
]
tag_max = np.random.rand(len(months))
tam_max = np.random.rand(len(months))
yticks = [i for i in range(len(months))]
fig, ((ax1, ax2)) = plt.subplots(nrows=1, ncols=2, figsize=(10,5), sharex='row')
fig.suptitle("Income from members and supporters", fontsize=14)
ax1.barh(yticks, tag_max)
ax1.set_facecolor('white')
ax1.set_title("Maximum income from members")
ax1.set_yticks(yticks)
ax1.set_yticklabels(months)
ax2.barh(yticks, tam_max)
ax2.set_facecolor('white')
ax2.get_yaxis().set_visible(False)
ax2.set_title('Maximum income from supporters')
plt.show()
这将提供以下输出:
原因是y轴是字符串。Matplotlib然后自动对这些字母进行排序哈,我没想到,非常感谢!
from matplotlib import pyplot as plt
import numpy as np
months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
]
tag_max = np.random.rand(len(months))
tam_max = np.random.rand(len(months))
yticks = [i for i in range(len(months))]
fig, ((ax1, ax2)) = plt.subplots(nrows=1, ncols=2, figsize=(10,5), sharex='row')
fig.suptitle("Income from members and supporters", fontsize=14)
ax1.barh(yticks, tag_max)
ax1.set_facecolor('white')
ax1.set_title("Maximum income from members")
ax1.set_yticks(yticks)
ax1.set_yticklabels(months)
ax2.barh(yticks, tam_max)
ax2.set_facecolor('white')
ax2.get_yaxis().set_visible(False)
ax2.set_title('Maximum income from supporters')
plt.show()