sortindex倒序
时间: 2023-11-24 12:07:27 浏览: 58
倒排索引表
你可以在 `data = data.set_index('time')` 这一行之后加上 `data = data.sort_index(ascending=False)` 来将索引按照倒序排序。`ascending=False` 表示按照逆序排序,也就是最新的日期排在最前面。完整的代码如下:
```
select_sqli ="SELECT time,SUM(CASE WHEN type_c='两厢车' THEN xiaoliang ELSE 0 END)/SUM(xiaoliang) AS ratio,1 - SUM(CASE WHEN type_c='两厢车' THEN xiaoliang ELSE 0 END)/SUM(xiaoliang) AS ratio_complement FROM sheji.sale GROUP BY time;"
cur.execute(select_sqli)
data = pd.DataFrame(cur.fetchall(), columns=['time', 'portion','r_portion'])
data = data.set_index('time')
data = data.sort_index(ascending=False) # 按照逆序排序
data = data.iloc[:5, :]
plt.figure(figsize=(9.42,1))
data.plot(kind='bar', stacked=True, x='time') # 指定横坐标为时间列
# 设置标题和标签
plt.title('Sales Ratio of Different Categories')
plt.xlabel('Category')
plt.ylabel('Ratio')
plt.xticks(rotation=90)
# 显示图形
plt.savefig('portion.png',dpi=300)
pix = QPixmap("portion.png")
new_width = self.Market.label_4.width()
new_height = int(pix.height() * new_width / pix.width())
scaled_pixmap = pix.scaled(QSize(new_width, new_height), Qt.AspectRatioMode.KeepAspectRatio,Qt.TransformationMode.SmoothTransformation)
self.Market.label_4.setPixmap(scaled_pixmap)
plt.close()
```
这样就可以按照时间的倒序显示图形了。
阅读全文