问题描述
我从一项研究中收集数据,该研究按事故类别对美国12个最大的都市区的道路危险程度进行了排名。 到目前为止,我已经完成了条形图的代码,但是我需要反转“排名”轴上的刻度线顺序,而我找不到执行该操作的方法。 我是python和一般编码的新手。
Cities = ('Hou','Dal','Phx','Mia','Phi','Atl','Chi','LA','SF','NY','WDC','Bos')
y_pos = np.arange(len(Cities))
#DUI Rankings
performance = [1,3,2,5,4,10,7,9,8,11,6,12]
plt.bar(y_pos, performance, align='center', alpha=0.5)
plt.xticks(y_pos, Cities)
plt.ylabel('Ranking')
plt.title('DUI Involved Crashes')
plt.show()
我正在尝试颠倒刻度线的顺序,所以它将变为12-> 1而不是0-> 12
1楼
plt.bar(x, height, width, bottom, *, align='center', **kwargs)
函数采用bottom
参数。
不幸的是,您必须使用performance-13
来计算钢筋高度,因为该函数需要一个height
参数,而不是y
参数(您必须考虑这一点,因为bottom
不为零)。
plt.bar(y_pos, performance-13, bottom=13, align='center', alpha=0.5)
plt.ylim(13, 0)