[英]In Python, For bar plot , How do I arrange X - axis values in ascending order?
Python 的新手。 下面是我寫給 plot 的代碼我擁有的數據的條形圖
K=df4.groupby('mnth_yr').sum()
K['sales'].plot(kind='bar', title='Monthly sales', ylabel='sales',xlabel='mnth_yr', figsize=(6, 5))
Output:條形圖
我需要從 19 年 1 月開始的 X 軸。 請幫忙
將mnth_yr
列轉換為日期時間 object - 現在是字符串類型,現在排序以字母順序進行,“A”是第一個元素。
matplotlib.pyplot bar function 將自動負責對 x_label 進行排序並繪制條形圖
舉個例子——
>>> x_val = [10., 7., 1., 8., 0., 6., 0., 3., 5., 2., 9., 20.]
>>> y_val = [ 3, 10, 15, 37, 67, 380, 2758, 10369, 17824, 7661, 728, 46]
>>> for item in zip(x_val, y_val):
print(item)
(10.0, 3)
(7.0, 10)
(1.0, 15)
(8.0, 37)
(0.0, 67)
(6.0, 380)
(0.0, 2758)
(3.0, 10369)
(5.0, 17824)
(2.0, 7661)
(9.0, 728)
(20.0, 46)
如果你看到,x 軸上的數據都是混亂的。 現在如果你跑
import matplotlib.pyplot as plt
plt.bar(x_val, y_val)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.